-
background-size: 数值
-
px:设置背景图像的高度和宽度。第一个值设置宽度,第二个值设置高度。如果只设置一个值,则第二个 值会被设置为 “auto”。
-
percentage:以父元素的百分比来设置背景图像的宽度和高度。第一个值设置宽度,第二个值设置高度。如 果只设置一个值,则第二个值会被设置为 “auto”。
-
cover:覆盖,等比例缩放背景图片到铺满整个盒子,但是背景图可能会无法完整显示在盒子中(宽高给大点)
-
contain:包含,等比例缩放背景图片到完整显示在盒子中,有一边到达边界就停止放大, 可能导致另一边留白 但是背景图可能在区域范围内铺不满(宽高给大点)
-
.wrap div { width: 200px; height: 200px; border: 1px solid red; margin: 5px; } /* 显示越靠前的背景,书写顺序越靠前 */ .wrap .box { background-image: url(./img/bg1.jpg), url(./img/yd.jpg); } .wrap .box1 { background: url(./img/bg1.jpg), url(./img/7.jpg); } .wrap .box2 { background: url(./img/bg1.jpg) no-repeat, url(./img/7.jpg) no-repeat; } /* background-size: 数值 */ /* px */ .wrap .box3 { background: url(./img/yd.jpg) no-repeat; background-size: 100px 100px; background-size: 100px; } /* 百分比 */ .wrap .box4 { background: url(./img/7.jpg) no-repeat; background-size: 50% 50%; background-size: 50%; background-size: 20%; } /* cover:覆盖,等比例缩放背景图片到铺满整个盒子,但是背景图可能会无法完整显示在盒子中(宽高给大点) */ .wrap .box5 { width: 900px; height: 400px; background: url(./img/7.jpg) no-repeat; background-size: cover; } /* contain:包含,等比例缩放背景图片到完整显示在盒子中,但是背景图可能在区域范围内铺不满(宽高给大点) */ .wrap .box6 { width: 600px; height: 400px; background: url(./img/7.jpg) no-repeat; background-size: contain; }