zoukankan      html  css  js  c++  java
  • css background-size与背景图片填满div

    background-size与背景图片填满div

    在开发中,常有需要将一张图片作为一个div的背景图片充满div的需求

    background-size的取值及解释

    background-size共有三种属性,分别为

    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.

    这里的关键说明在于标红的两个区域,分别是它会保持图片的宽高比当图像和容器具有不同的尺寸时,图像被左/右或顶部/底部裁剪。之后会结合例子说明

    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. www.97yingyuan.org When the image and container have different dimensions, the empty areas (either top/bottom of left/right) are filled with the background-color.

    这里的关键说明在于标红的两个区域,分别是它会保持图片的宽高比当图像和容器具有不同的尺寸时,空区域(左/右/上/右)填充背景色。之后会结合例子说明

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

    分为固定大小百分比auto,固定大小就是写死;auto就是以背景图片的比例缩放背景图片。。

    百分比的的MDN文档解释说明<percentage> 值,指定背景图片相对背景区(background positioning area)的百分比。背景区由background-origin设置,默认为盒模型的内容区与内边距,也可设置为只有内容区,或者还包括边框。如果attachment 为fixed,背景区为浏览器可视区(即视口),不包括滚动条。不能为负值。
  • 相关阅读:
    学习linux之用户-文件-权限操作
    Hadoop--Hadoop的机架感知
    redhat 6.3 64位安装中文输入法全过程记录
    hdu 4619 Warm up 2(并查集)
    openGL 初试 绘制三角形 和添加鼠标键盘事件
    MySQL 启动服务报错解决方案
    20亿与20亿表关联优化方法(超级大表与超级大表join优化方法)
    50行python代码实现个代理server(你懂的)
    nginx+tomcat反复请求
    慢慢过渡到个人博客
  • 原文地址:https://www.cnblogs.com/tianshifu/p/8127861.html
Copyright © 2011-2022 走看看