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,背景区为浏览器可视区(即视口),不包括滚动条。不能为负值。
  • 相关阅读:
    CodeIgniter 2.X 于 PHP5.6 兼容错误
    解决 TextMate 2 无法安装 Emmet 插件
    Windows 10 KMS 激活方法
    Sublime Text 3 如何修改默认快捷键
    Grunt快速使用笔记
    CSS3字体发光效果
    CSS3使用盒模型实现三栏布局
    CSS3Transition添加多个过渡效果
    Javascript 判断网页横竖屏
    【iOS知识汇】OC点语法的坑
  • 原文地址:https://www.cnblogs.com/tianshifu/p/8127861.html
Copyright © 2011-2022 走看看