CSS属性
一、背景属性
1.background-color(背景颜色):red;
2.background-image
(背景图片):background-image:url
(图片地址);
3.background-repeat(背景图片平铺)
:no-repeat(不平铺)/repeat(平铺)/repeat-x(x轴平铺)/repeat-y(y轴平铺);
4.background-attachment
(背景图片的固定):scroll(滚动)/fixed(固定),固定就是固定在浏览器窗口里面,如果没有设置盒子,在整个页面窗口滑动始终是这个图片,如果设置在盒子里面,那么滑动到下一个盒子的时候就会显示下一个盒子的内容或者下一个盒子没有设置内容,那么就会没有,所以它(fixed)是基于background-image的,(scroll)就是基础的滚动;
5.background-position(
图片位置):三种写法:数值,百分比,英文方位(left(左边)/bottom(顶部)/right(右边)/center(中间)/top(顶部)) 比如:background-position:
20px 20px;这两个值分别表示距离左边和距离上边的距离。左负值会往左,数值可以是百分比;
6.background-size
(图片大小):数值,百分比,英文(contain、cover)
比如:
background-size:
400px 400px ;
修改图片大小 100% 100%可以改成一样的 ;
background-size: cover;
把背景图像扩展至足够大,以使背景图像完全覆盖背景区域,也许无法显示在背景定位区域中;
background-size: contain;
把图像扩展至最大尺寸,以使其宽度和高度完全适应内容区域,铺不满盒子,留白;
7.background
复合书写:复合写法:空格隔开,顺序可以换,可以只去一个值,放在后面能覆盖前面的值(指的颜色)
二、代码演示:
1.background-color
<!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>Document</title>
<style>
.box1{
width: 200px;
height: 200px;
/* background-color: yellow;
*/
/* background-color: rgb(255,0,0); */
/* background-color: rgba(255,0,0, 0.5);/* backgound-color表示方法 */
background-color: #00ff00;
}
.box2{
width: 100px;
height: 100px;
background-color: rgba(0,0,0,0.5);/* 0.5百分之五十透明度 */
}
</style>
</head>
<body>
<div class="box1">
大家好
<div class="box2">
</div>
</div>
</body>
</html>
说明:rgba:red、green、blue、a代表透明的意思;0.5代表透明度为百分之五十。
结果:
2.background-image、background-reapeat
<!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>Document</title>
<style>
.box1{
width:400px;
height:400px;
background-color: yellow;
/* background-image:url(../9BEBDEDA925B7017F50910F191E0362F.jpg); */
background-image:url(../img/1.jpg);/* 默认平铺,小的图片会复制成好几份然后铺满整个 */
/* 写在后面的会掩盖前面写的图片 */
/* background-repeat: repeat-x;/* 平铺的X轴 */
/* background-repeat:repeat-y; 平铺的y轴*/
/* background-repeat: no-repeat; */
/* background-position: 20px 20px;负值会往左,数值可以是百分比 */
/* background-position: center center; */
/* background-size: 400px 400px ;修改图片大小 100% 100%可以改成一样的 */
/* background-size: cover; */ /* 把背景图像扩展至足够大,以使背景图像完全覆盖背景区域,也许无法显示在背景定位区域中 */
/* background-size: contain;把图像扩展至最大尺寸,以使其宽度和高度完全适应内容区域,铺不满盒子,留白 */
}
.box2{
width: 400px;
height: 400px;
background-image: url(../70A523A143C73657A5371964059B1D3F.jpg);
/* 大的图片盒子太小,只会显示一部分 */
background-repeat: repeat;/* 默认平铺 */
/* background-size:400px 400px ; */
background-size: contain;
}
</style>
</head>
<body>
<div class="box1">
</div>
<div class="box2">
</div>
</body>
</html>
默认平铺结果:
平铺x轴展示:
<!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>Document</title>
<style>
.box1{
width:400px;
height:400px;
background-color: yellow;
/* background-image:url(../9BEBDEDA925B7017F50910F191E0362F.jpg); */
background-image:url(../img/1.jpg);/* 默认平铺,小的图片会复制成好几份然后铺满整个 */
/* 写在后面的会掩盖前面写的图片 */
/* background-repeat: repeat-x;平铺的X轴
/* background-repeat:repeat-y; 平铺的y轴*/
/* background-repeat: no-repeat; */
/* background-position: 20px 20px;负值会往左,数值可以是百分比 */
/* background-position: center center; */
/* background-size: 400px 400px ;修改图片大小 100% 100%可以改成一样的 */
/* background-size: cover; */ /* 把背景图像扩展至足够大,以使背景图像完全覆盖背景区域,也许无法显示在背景定位区域中 */
/* background-size: contain;把图像扩展至最大尺寸,以使其宽度和高度完全适应内容区域,铺不满盒子,留白 */
}
.box2{
width: 400px;
height: 400px;
background-image: url(../70A523A143C73657A5371964059B1D3F.jpg);
/* 大的图片盒子太小,只会显示一部分 */
background-repeat: repeat;/* 默认平铺 */
/* background-size:400px 400px ; */
background-size: contain;
}
</style>
</head>
<body>
<div class="box1">
</div>
<div class="box2">
</div>
</body>
</html>
结果:
读者可以自行试一下y轴;
3.background-position、background-size(图片大小)
background-position
<!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>Document</title>
<style>
.box1{
width:400px;
height:400px;
background-color: yellow;
/* background-image:url(../9BEBDEDA925B7017F50910F191E0362F.jpg); */
background-image:url(../img/1.jpg);/* 默认平铺,小的图片会复制成好几份然后铺满整个 */
/* 写在后面的会掩盖前面写的图片 */
/* background-repeat: repeat-x; */ /* 平铺的X轴 */
/* background-repeat:repeat-y; 平铺的y轴*/
background-repeat: no-repeat;
/* background-position: 20px 20px;这两个值分别表示距离左边和距离上边的距离。左负值会往左,数值可以是百分比 三种写法:1.数值,百分比,英文方位*/
background-position: center center;
/* background-size: 400px 400px ;修改图片大小 100% 100%可以改成一样的 同上三种写法 */
/* background-size: cover; */ /* 把背景图像扩展至足够大,以使背景图像完全覆盖背景区域,也许无法显示在背景定位区域中*/
/* background-size: contain; */ /* 把图像扩展至最大尺寸,以使其宽度和高度完全适应内容区域,铺不满盒子,留白 */
}
.box2{
width: 400px;
height: 400px;
background-image: url(../70A523A143C73657A5371964059B1D3F.jpg);
/* 大的图片盒子太小,只会显示一部分 */
background-repeat: repeat;/* 默认平铺 */
/* background-size:400px 400px ; */
background-size: contain;
}
</style>
</head>
<body>
<div class="box1">
</div>
<div class="box2">
</div>
</body>
</html>
结果:
background-size
我会演示三种表现图片,但只会给一份代码,其他的可以去掉注释自行运算
代码:
<!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>Document</title>
<style>
.box1{
width:400px;
height:400px;
background-color: yellow;
/* background-image:url(../9BEBDEDA925B7017F50910F191E0362F.jpg); */
background-image:url(../img/1.jpg);/* 默认平铺,小的图片会复制成好几份然后铺满整个 */
/* 写在后面的会掩盖前面写的图片 */
/* background-repeat: repeat-x; */ /* 平铺的X轴 */
/* background-repeat:repeat-y; 平铺的y轴*/
background-repeat: no-repeat;
/* background-position: 20px 20px;这两个值分别表示距离左边和距离上边的距离。左负值会往左,数值可以是百分比 三种写法:1.数值,百分比,英文方位*/
/* background-position: center center; */
/* background-size: 400px 400px ;修改图片大小 100% 100%可以改成一样的 同上三种写法 */
/* background-size: cover; */ /* 把背景图像扩展至足够大,以使背景图像完全覆盖背景区域,也许无法显示在背景定位区域中*/
/* background-size: contain; */ /* 把图像扩展至最大尺寸,以使其宽度和高度完全适应内容区域,铺不满盒子,留白 */
}
.box2{
width: 400px;
height: 400px;
background-image: url(../70A523A143C73657A5371964059B1D3F.jpg);
/* 大的图片盒子太小,只会显示一部分 */
background-repeat: repeat;/* 默认平铺 */
/* background-size:400px 400px ; */
background-size: contain;
}
</style>
</head>
<body>
<div class="box1">
</div>
<div class="box2">
</div>
</body>
</html>
数值结果:
contain结果:
cover结果:
4.background-attachment
<!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>Document</title>
<style>
div{
width: 300px;
height: 1000px;
}
.box1{
background-image: url(../img/2.jpg);
background-attachment: fixed;/* background-attachment*/
background-repeat: no-repeat;
background-color: yellow;
}
.box2{
/* background-image: url(../img/1.jpg); */
background-attachment: fixed;/* 根据盒子的范围来显示的 */
background-repeat: no-repeat;
background-color: orange;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
</body>
</html>
结果读者可以自己运行,因为设计到滚动不好截图,如下:
5.background复合写法
<!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>Document</title>
<style>
.box2{
width:600px;
height: 600px;
/* background-color: aqua;
background-image: url(../img/0.jpg);
background-repeat: no-repeat;
background-attachment:fixed;
background-position: center;/* fixed是整个窗口,而且为我们设置盒子在中间(center),所以显示不出来 */
background: yellow url(../img/0.jpg) no-repeat center fixed;/* 复合写法 */
/* background-size: ; 不能复合使用 */
}/* 复合写法:空格隔开,顺序可以换,可以只去一个值,放在后面能覆盖前面的值 */
.box1{
width: 300px;
height: 300px;
background-color: yellow;
background: url(../img/0.jpg) no-repeat center top;/* center top位置不能瞎写,位置不能更换,后面的颜色默认是白色,会覆盖前面的值 */
}
</style>
</head>
<body>
<div class="box2"></div>
<div class="box1"></div>
</body>
</html>
<!-- background复合写法 -->
结果:
注意:复合写法的时候一定要注意,不然容易搞错,你看上面的代码,一个标准的复合写法,另一个简写就出现了问题,默认的白色覆盖了设置的黄色。
总结:
这一节我们学习了CSS背景属性:
1.background-color(背景颜色):red;
2.background-image
(背景图片):background-image:url
(图片地址);
3.background-repeat(背景图片平铺)
:no-repeat(不平铺)/repeat(平铺)/repeat-x(x轴平铺)/repeat-y(y轴平铺);
4.background-attachment
(背景图片的固定):scroll(滚动)/fixed(固定),固定就是固定在浏览器窗口里面,如果没有设置盒子,在整个页面窗口滑动始终是这个图片,如果设置在盒子里面,那么滑动到下一个盒子的时候就会显示下一个盒子的内容或者下一个盒子没有设置内容,那么就会没有,所以它(fixed)是基于background-image的,(scroll)就是基础的滚动;
5.background-position(
图片位置):三种写法:数值,百分比,英文方位(left(左边)/bottom(顶部)/right(右边)/center(中间)/top(顶部)) 比如:background-position:
20px 20px;这两个值分别表示距离左边和距离上边的距离。左负值会往左,数值可以是百分比;
6.background-size
(图片大小):数值,百分比,英文(contain、cover)
比如:
background-size:
400px 400px ;
修改图片大小 100% 100%可以改成一样的 ;
background-size: cover;
把背景图像扩展至足够大,以使背景图像完全覆盖背景区域,也许无法显示在背景定位区域中;
background-size: contain;
把图像扩展至最大尺寸,以使其宽度和高度完全适应内容区域,铺不满盒子,留白;
7.background
复合书写:复合写法:空格隔开,顺序可以换,可以只去一个值,放在后面能覆盖前面的值(指的颜色),center top位置不能乱写,要小心复合的写法容易出错
最后放置一个有意思的页面背景background-attachment/background-position/background-size的应用,很漂亮好玩哦,我会放置背景图片和代码放在下面,读者自取即可。
<!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>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
div{
height: 780px;
background-position: center;
background-size: cover;
background-attachment: fixed;
}
.box1{
background-image: url(../img/bg1.png);
background-position: center;
background-size: cover;
}
.box2{
background-image: url(../img/bg2.png);
background-position: center;
background-size: cover;
}
.box3{
background-image: url(../img/bg3.png);
background-position: center;
background-size: cover;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>
</html>