2020.01.16
1884
fish
MDN 中对其的定义如下: Background 是一种 CSS 简写属性,一次性定义了所有的背景属性,包括 color, image, origin 还有 size, repeat 方式等等。
我们首先讲一下 Background 的日常语法:
值 | 说明 | 默认值 | 版本 |
---|---|---|---|
background-color | 指定要使用的背景颜色 | transparent | CSS2.1 |
background-position | 指定背景图像的位置 | 0%, 0% | CSS2.1 |
background-image | 指定要使用的一个或多个背景图像 | none | CSS2.1 |
background-repeat | 指定如何重复背景图像 | repeat | CSS2.1 |
background-attachment | 设置背景图像是否固定或者随着页面的其余部分滚动。 | scroll | CSS2.1 |
background-size | 指定背景图片的大小 | auto | CSS3 |
background-origin | 指定背景图像的定位区域 | padding-box | CSS3 |
background-clip | 指定背景图像的绘画区域 | border-box | CSS3 |
这里给大家展示一下几个常见的background的属性的用法:
<style> .div1 { width: 100px; height: 100px; background-color: black; background-image: url('img1'); background-size: 50%; background-repeat: no-repeat;
} </style> <body> <div class="div1"></div> </body> 复制代码
(1)单词:background-color: black;
(2)十六进制:background-color: #000;
(3)RGB 色彩模式:background-color: rgb(0, 0, 0);
(1)百分比:background-size: 100%;
(2)像素值:background-size: 100px;
(1) repeat (重复):background-repeat: repeat;
(2) repeat-x (横向重复):background-repeat: repeat-x;
(3) repeat-y (纵向重复):background-repeat: repeat-y;
(4) no-repeat (不重复):background-repeat: no-repeat;
在 CSS2.1 中,元素只能添加一张背景图片。然而在 CSS3 中,我们可以给元素添加多张背景图片。其兼容性如下图所示:
<style> .div1 { width: 100px; height: 100px; background-color: black; background-image: url('img1'), url('img2'); background-size: 50%, 100%; background-repeat: repeat-x, no-repeat;
} </style> <body> <div class="div1"></div> </body> 复制代码
<style> .div1 { width: 100px; height: 100px; background-color: black; background-image: url('img1'), url('img2'), url('img3'); background-size: 50%, 30%; background-repeat: repeat-y, no-repeat;
} </style> <body> <div class="div1"></div> </body> 复制代码
多背景图片总结:
背景渐变是基于background-image来设置的。具体语法详见 MDN。其兼容性如下图所示:
linear-gradient( < angle > | to < side-or-corner > ,]? < color-stop > [, < color-stop >]+ )
< angle >:用角度值指定渐变的方向
< side-or-corne r>: [left | right] || [top | bottom]
< color-stop >:< color > [ < percentage > | < length > ]?
<style> .div1 {
background-image: linear-gradient(#71c9ce, #e3fdfd);;
} </style> <body> <div class="div1"></div> </body> 复制代码
radial-gradient( [ [ellipse | circle] || [ < extent-keyword > | < precentage > ] [ at < position > ]? ] ? < color-stop > [ , < color-stop > ]+ )
< extent-keyword > = closest-corner | closest-side | farthest-corner | farthest-side
< color-stop >:< color > [ < percentage > | < length > ]?
<style> .div1 {
background-image: radial-gradient( #71c9ce, #e3fdfd);;
} </style> <body> <div class="div1"></div> </body> 复制代码
<style> .div1 { background-image: repeating-linear-gradient(45deg, #71c9ce 20px, #a6e3e9 30px, #e3fdfd 40px);
} </style> <body> <div class="div1"></div> </body> 复制代码
<style> .div1 { width: 100px; height: 100px; background-color: black; background-image: repeating-radial-gradient(circle, #90ade4 ,#3350ba 20%);
} </style> <body> <div class="div1"></div> </body> 复制代码
在讲以下内容之前,我们先科普一下一个元素所涉及的三个盒子,请看图↓
上图三个盒子分别为content-box(内容盒子)、padding-box(内边距盒子)和border-box(边框盒子)。
background-position默认的定位为padding-box盒子的左上角。
(1) 百分比(%)
(2) 像素(px)
(3) 位置(top | right | bottom | left | center)
<style> .div1 { width: 100px; height: 100px; background-image: url('img1'); background-size: 50%; background-repeat: no-repeat; background-position: right;
} </style> <body> <div class="div1"></div> </body> 复制代码
background-repeat除了常见的几个 repeat、repeat-x,repeat-y 以及 no-repeat 以外,还在CSS3 中新加了两个值:space和round。其兼容性如下图所示:
背景图片小于容器时
background-repeat:space在保证不缩放的前提下尽可能多的重复图片,并等分图片中间的空隙
background-repeat:round在尽可能多的重复图片的前提下,拉伸图片以铺满容器
背景图片大于容器时
background-origin属性规定background-position属性相对于什么位置来定位。属性值有content-box、padding-box、border-box三个,默认为padding-box。其兼容性如下:
background-origin: content-box(下图为设置 padding: 20px )
background-origin: padding-box
background-origin: border-box
background-clip属性规定背景的绘制区域。默认值为border-box,其属性值同background-origin一样,不过表现大不相同。其兼容性如下:
background-clip: content-box
background-clip: padding-box
background-clip: border-box`
感觉这个属性很常见吧,其实它也是 CSS3 中新加的属性。 CSS2.1 中,背景图片大小是无法设置的。background-size除了常见的设置大小和百分比之外,还有两个特殊的属性:contain和cover
background-size: contain图片长宽不相同时,把图片按比例缩小至较长的一方完全适应内容区域为止,多用于背景图片比元素大的情况。
background-size: cover图片长宽不相同时,把图片按比例放大至较短的一方完全适应内容区域为止,以使背景图像完全覆盖背景区域,多用于背景图片比元素小的情况。
有时候在一些网站上会看到,滚动页面的时候,背景图片是固定的。那就是使用background-attachment: fixed做到的。
一个特殊的扩展属性,可以将某个元素设置为另一元素的背景。惊不惊喜,意不意外!不过这个属性只有 FireFox 4+ 的浏览器可以使用,并且需要加上浏览器前缀。
<style> .div { width: 200px; height: 200px; background: element(#button) no-repeat; background: -moz-element(#button) no-repeat;
} #button { width: 150px; height: 20px; margin: 50px; color: #0470f4;
} </style> <body> <div class="div1"> <button id='button'>这是按钮</button> </div> </body> 复制代码
<style> .div { width: 200px; height: 200px; border: 10px dashed #0ff; background: element(#img1); background: -moz-element(#img1);
} #img1 { width: 50px;
} </style> <body> <div class="div1"> <img id='img1' src='img1' /> </div> </body> 复制代码
CSS 中还有许许多多的我们未知的东西,我们正在一点点探索,期待与你同行。如果你也有什么新发现,欢迎与我们一起讨论~
免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。