文章目录
一.热身阶段
css2知识点
background:背景,它是复合样式,拥有多个属性值。
background-image:背景图。
background-color:背景色。
background-position:背景图定位。
background-repeat:背景图重复。
二.新增的属性(css3知识点)
1.背景图起源
知识点
background-origin:背景图起源,它有3个属性。
border-box:背景图从外边框开始显示。
padding-box:背景图从内间距开始显示。
content-box:背景图从内部区域开始显示。
代码演示
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>背景图起源</title>
<style>
*{
margin: 0;
padding: 0;
}
div{
width: 400px;
height: 400px;
border: 10px solid rgba(0, 0, 0, 0.2);
margin: 100px auto;
padding: 10px;
background: url("./img/world.jpg") no-repeat;
/* 背景图起源的设置 */
background-origin: border-box;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
效果图1:border-box
效果图2:padding-box
效果图3:content-box
2.背景图修剪
知识点
background-clip:背景图裁切,它的属性值有4个。
border-box:外边框区域以外的被裁切 。
padding-box:内间距区域以外被裁切。
content-box:内容区域之外被裁切 。
text:文本区域以外被裁切。
①文本裁切是有兼容问题的,需要在background-clip属性之前加上私有前缀。
②私有前缀
谷歌:webkit
火狐:moz
IE:ms
③有了私有前缀之后,还需要给文字颜色加上透明度,接下来,我们才能看到裁切效果。
代码演示:图像裁剪 content-box
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>修剪</title>
<style>
*{
margin: 0;
padding: 0;
}
div{
width: 400px;
height: 400px;
border: 10px solid rgba(0, 0, 0, 0.2);
margin: 100px auto;
padding: 10px;
background: url("./img/world.jpg") no-repeat;
/* 背景图裁剪 */
background-clip:content-box;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
效果图1:图像裁剪 content-box
效果图2:图像裁剪 padding-box
效果图3:图像裁剪 border-box
代码演示:文本裁剪
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>修剪</title>
<style>
*{
margin: 0;
padding: 0;
}
div{
width: 400px;
height: 400px;
border: 10px solid rgba(0, 0, 0, 0.2);
margin: 100px auto;
padding: 10px;
background: url("./img/world.jpg") no-repeat;
/* 背景图裁剪 */
-Webkit-background-clip:text;
font-size: 50px;
/* 给文字颜色设置透明度 */
color:rgba(0, 0, 0, 0.2);
}
</style>
</head>
<body>
<div>11111111111111111111</div>
</body>
</html>
效果图:文本裁剪
3.背景图的尺寸大小
知识点
background-size:背景尺寸大小。
属性值写法:
①数字+像素(px)
②数字+百分比(%)
③cover
④contain
属性和属性值的写法:
background-size: 宽 高;
background-size:英文单词;
cover:覆盖。背景图的原图会被拉伸,导致一部分图像缺失。
contain:容纳。背景图的原图会原封不动地展示出来。
代码演示:cover
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>背景图的尺寸大小</title>
<style>
*{
margin: 0;
padding: 0;
}
div{
width: 400px;
height: 400px;
border: 2px solid black;
margin: 100px auto;
background: url(./img/world.jpg) no-repeat;
/* 设置背景图尺寸大小 */
background-size: cover;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
效果图1:cover
效果图2:contain
篇章
上一篇:css3教程4-阴影
下一篇:css3教程6-图像精灵