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,背景区为浏览器可视区(即视口),不包括滚动条。不能为负值。

原文地址:https://www.cnblogs.com/tianshifu/p/8127861.html

时间: 2024-08-04 09:36:44

css background-size与背景图片填满div的相关文章

IE9以下通过css让html页面背景图片铺满整个屏幕

第一种方法不设为背景图片,通过css来控制样式,可兼容到IE6,代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="author" content="chaozhang5"> <meta name="mail" content=&qu

css网页中设置背景图片的方法详解

css网页中设置背景图片的方法详解 在css代码中设置背景图片的方法,包括背景图片.背景重复.背景固定.背景定位等 用css设置网页中的背景图片,主要有如下几个属性: 1,背景颜色 {background-color:数值}2,背景图片 {background-image: url(URL)|none}3,背景重复 {background-repeat:inherit|no-repeat|repeat|repeat-x|repeat-y}4,背景固定 {background-attachment

laravel项目中css样式表的背景图片不显示

刚学laravel,遇到了很多坑,感觉laravel是挺强大的. 建好后台项目,奈何css样式表的背景图片不显示 .mainhd { background: url(../images/sky/body_bg.png) repeat-x 0px 0px; } 按理上面的写法没错,因为是从别的后台搬过来的,但是图片一直不显示,访问绝对路径却又能显示图片,坑. 原因是因为图片路径要使用单引号或者双引号的,下面是正确的写法 .mainhd { background: url('../images/sk

2014年辛星解读CSS第八节 使用背景图片

这应该是系统CSS的教程的最后一节了,为什么呢,因为到这一节,我感觉基础知识就已经讲完了,接下来的就是无穷的实战,而实战是很难用知识去讲出来的,靠的是积累,拼的是经验,这些都不是讲出来的. 好,我们下面来说一下用CSS添加背景图片的方式把,它在background属性中指定,可以用url("图片路径")的方式来指定背景图片,如果是repeat,则图片在横向和纵向上平铺,如果是no-repeat,则背景图像不会平铺,只显示一次,如果是repeat-x,那么就在水平方向平铺,如果是repe

手机端背景图片撑满整个屏幕

HTML设置一个空div,用来放置背景图片. <body> <div class="main"></div> </body> (1)方法一:高度100% html,body{ margin:0; height:100%; } .main{ height:100%; background:url('./bg.png') no-repeat ; } (2)方法二:高度100vh .main{ height:100vh; width:100vw

【CSS学习笔记】背景图片

直接看代码: background: blue; /*页面背景为蓝色,很简单的代码*/background-image:url(small.jpg); /*页面背景是名字叫small.jpg的图片*/background-repeat: no-repeat; /*水平方向显示图片repeat-x 竖直方向显示图片repeat-y 不重复图片no-repeat*/ background-attachment: fixed; /*在页面特别长有滚动条时,可以固定图片*/ 相关知识点: 1.背景图与背

CSS中Backgroud-position(背景图片)定位问题详解

background-position的说明:    设置或检索对象的背景图像位置.必须先指定 background-image 属性.该属性定位不受对象的补丁属性( padding )设置影响.    默认值为: 0% 0% .此时背景图片将被定位于对象不包括补丁( padding )的内容区域的左上角.    如果只指定了一个值,该值将用于横坐标.纵坐标将默认为 50% .如果指定了两个值,第二个值将用于纵坐标.    如果设置值为 right center,因为 right 作为横坐标值将

CSS基础知识之背景图片样式属性、雪碧图、列表导航栏、超链接的伪类样式、盒子模型、overflow、圆角弧度

目录: 背景图片样式属性 雪碧图 列表导航栏 超链接的伪类样式 盒子模型. overflow. 圆角弧度 背景图片样式属性: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <style> #imgContainer { border: 20px dotted yellow; padding: 50px; height: 400p

背景图片铺满body

添加背景图片: 添加样式代码如下: <style type="text/css"> body { background: url("image/bg.jpg") top center no-repeat; background-size: cover; } </style> 最终效果如下图所示: