css background-size与背景图片填满div研究包括cover contain属性测试

2019-10-2410:08:20网页制作Comments2,275 views字数 1729阅读模式

background-size与背景图片填满div

在开发中,常有需要将一张图片作为一个div的背景图片充满div的需求文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/17019.html

background-size的取值及解释

background-size共有三种属性,分别为文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/17019.html

background-size: cover

MDN文档解释说明:缩放背景图片以完全覆盖背景区,可能背景图片部分看不见。A keyword that is the inverse of contain. Scales the image as large as possible and maintains image aspect ratio (image doesn't get squished). The image "covers" the entire width or height of the container. When the image and container have different dimensions, the image is clipped either left/right or top/bottom.文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/17019.html

这里的关键说明在于标红的两个区域,分别是它会保持图片的宽高比当图像和容器具有不同的尺寸时,图像被左/右或顶部/底部裁剪。之后会结合例子说明文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/17019.html

background-size: contain

MDN文档解释说明:缩放背景图片以完全装入背景区,可能背景区部分空白。A keyword that scales the image as large as possible and maintains image aspect ratio (image doesn't get squished). Image is letterboxed within the container. When the image and container have different dimensions, the empty areas (either top/bottom of left/right) are filled with the background-color.文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/17019.html

这里的关键说明在于标红的两个区域,分别是它会保持图片的宽高比当图像和容器具有不同的尺寸时,空区域(左/右/上/右)填充背景色。之后会结合例子说明文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/17019.html

background-size: width-value,height-value;

分为固定大小百分比auto,固定大小就是写死;auto就是以背景图片的比例缩放背景图片。。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/17019.html

百分比的的MDN文档解释说明<percentage> 值,指定背景图片相对背景区(background positioning area)的百分比。背景区由background-origin设置,默认为盒模型的内容区与内边距,也可设置为只有内容区,或者还包括边框。如果attachment 为fixed,背景区为浏览器可视区(即视口),不包括滚动条。不能为负值。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/17019.html

实验及声明

这次选用鲁殿作为背景图,这张图的尺寸是260*234,宽高比为260/234 ≈ 1.11
css background-size与背景图片填满div研究包括cover contain属性测试文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/17019.html


假设一div的宽高为200*200,下面测试中上为表现图,下为原图文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/17019.html

background-size: contain
css background-size与背景图片填满div研究包括cover contain属性测试文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/17019.html

css background-size与背景图片填满div研究包括cover contain属性测试文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/17019.html

background-size: cover
css background-size与背景图片填满div研究包括cover contain属性测试文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/17019.html

css background-size与背景图片填满div研究包括cover contain属性测试文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/17019.html

background-size: auto (auto)
css background-size与背景图片填满div研究包括cover contain属性测试文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/17019.html

css background-size与背景图片填满div研究包括cover contain属性测试文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/17019.html

background-size: 100% 100%
css background-size与背景图片填满div研究包括cover contain属性测试文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/17019.html

css background-size与背景图片填满div研究包括cover contain属性测试文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/17019.html

分析及解释:首先contain是不行的,原理在于contain要保持宽高比将图片完全放入div中,div为200×200。原图为260×234,所以为了放入div,宽260→200,那么高就得200/宽高比(1.11) = 180,所以会有下面的空白。
再其次,cover在这个时候也是不符合要求的,虽然看起来貌似符合要求,但是与原图是有差别的吗,原因在于cover与contain完全放入相反,它要求完全覆盖。所以要覆盖就要从更小的高计算。高234→200宽就等于200×1.11 = 222,图像就会被右部裁剪掉一部分。
再再其次,auto是原图大小也是不符合的。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/17019.html

所以,background-size: 100%, 100%;是符合需求的。按照div的大小进行计算文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/17019.html

其他各种大小div与各种大小的图片搭配请参照上述说明自行分析。
个人建议这种需求都使用background-size: 100%, 100%;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/17019.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/zhizuo/17019.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定