关于css:关于CSS中的背景属性background简述

5次阅读

共计 3990 个字符,预计需要花费 10 分钟才能阅读完成。

像我之前提到的那样,文档树中的每个元素只是一个矩形盒子。这些盒子都有一个背景层,背景层能够是齐全通明或者其它色彩,也能够是一张图片。此背景层由 8 个 CSS 属性(加上 1 个简写的属性)管制。

background-color

background-color 属性设置元素的背景色彩。它的值能够是任意非法的色彩值或者是 transparent 关键字。

.left {background-color: #ffdb3a;}
.middle {background-color: #67b3dd;}
.right {background-color: transparent;}

背景色彩绘制在由 [background-clip](#backgroundclip)属性指定的盒模型的区域内。如果还设置了任何背景图像,则在它们前面绘制色彩层。与能够有多个的图像层不同,对于一个元素,咱们只能有一个色彩层。

background-image

background-image 属性定义元素的一个或多个背景图像。它的值通常是用 url()符号定义的图像的 url。也能够应用 none 作为它的值,但这样会生成一个空的背景层

.left {background-image: url('ire.png'); }
.right {background-image: none;}

咱们也能够指定多张背景图片并通过逗号分隔。前面的图片都会绘制在 Z 轴方向上前一个图片的前面。

.middle {background-image: url('khaled.png'), url('ire.png');
 /* Other styles */
 background-repeat: no-repeat; 
 background-size: 100px;
}

background-repeat

background-repeat 属性管制背景图片在被 [background-size](#backgroundsize)属性扭转了大小及被 [background-position](#backgroundposition)属性定位后如何平铺。

该属性的值能够是 repeat-x, repeat-y, repeat, space, round, no-repeat 关键字,除了 repeat- x 和 repeat-y, 其余值能够为 x 轴和 y 轴定义一次,也能够独自定义每个维。

.top-outer-left {background-repeat: repeat-x;}
.top-inner-left {background-repeat: repeat-y;}
.top-inner-right {background-repeat: repeat;}
.top-outer-right {background-repeat: space;}
.bottom-outer-left {background-repeat: round;}
.bottom-inner-left {background-repeat: no-repeat;}
.bottom-inner-right {background-repeat: space repeat;}
.bottom-outer-right {background-repeat: round space;}

background-size

background-size 属性定义背景图片的大小,它的值能够是关键字,长度或者百分比。

可用于此属性的关键字为“contains”和“cover”。contain 将等比缩放图像到最大的大小。另一方面,cover 将把图像缩放到尽可能小的尺寸,其中整个背景区域依然被笼罩。

.left { 
 background-size: contain;
 background-image: url('ire.png'); 
 background-repeat: no-repeat;
}
.right {background-size: cover; /* Other styles same as .left */}

对于长度和百分比,咱们能够同时指定背景图片的宽高,百分比值是依据元素的大小计算的。

.left {background-size: 50px; /* Other styles same as .left */}
.right {background-size: 50% 80%; /* Other styles same as .left */}

background-attachment

background-attachment 属性管制管制背景图像绝对于视口和元素的滚动形式。它有三个潜在的值。

fixed 意味着背景图片固定在视口并且不会挪动,即便用户正沿着视口滚动。local 意味着背景图片固定在它在元素中的地位。如果这个元素能够滚动并且背景图片定位在顶部,那么当用户向下滚动这个元素,背景图片将会从视图中滚出去。最初 scroll 意味着背景图片是固定的且不会随着元素内容的滚动而滚动。

.left { 
 background-attachment: fixed;
 background-size: 50%;
 background-image: url('ire.png'); 
 background-repeat: no-repeat;
 overflow: scroll;
}
.middle {background-attachment: local; /* Other styles same as .left */}
.right {background-attachment: scroll; /* Other styles same as .left */}

background-position

这个属性联合 background-origin 属性定义背景图片的起始地位应在何处。它的值能够是关键字,长度或者百分比,咱们能够指定沿 x 轴和 y 轴的地位。

可用于此属性的关键字为 top, right, bottom, left, 和 center,咱们能够任意组合这些关键字,如果只明确指定了一个关键字,那么另外一个默认就是 center。

.top-left { 
 background-position: top;
 background-size: 50%;
 background-image: url('ire.png'); 
 background-repeat: no-repeat;
}
.top-middle {background-position: right; /* Other styles same as .top-left */}
.top-right {background-position: bottom; /* Other styles same as .top-left */}
.bottom-left {background-position: left; /* Other styles same as .top-left */}
.bottom-right {background-position: center; /* Other styles same as .top-left */}

对于长度和百分比,咱们也能够指定沿 x 轴和 y 轴的地位。百分比值是按元素的大小计算的。

.left {background-position: 20px 70px; /* Others same as .top-left */}
.right {background-position: 50%; /* Others same as .top-left */}

background-origin

background-origin 属性指定背景图片应依据盒模型的哪个区域进行定位。

当值为 border-box 时,背景图片的地位依据边框区域定位,为 padding-box 时其地位依据边距区域定位,为 content-box 时其地位依据内容区域定位。

.left { 
 background-origin: border-box;
 background-size: 50%;
 background-image: url('ire.png'); 
 background-repeat: no-repeat;
 background-position: top left; 
 border: 10px dotted black; 
 padding: 20px;
}
.middle {background-origin: padding-box; /* Other styles same as .left*/}
.right {background-origin: content-box; /* Other styles same as .left*/}

background-clip

background-clip 属性确定背景绘制区域,这是背景能够被绘制的区域。和 background-origin 属性一样,它也 基于盒子模型的区域。

.left{ 
 background-clip: border-box;
 background-size: 50%;
 background-color: #ffdb3a; 
 background-repeat: no-repeat;
 background-position: top left; 
 border: 10px dotted black; 
 padding: 20px;
}
.middle {background-clip: padding-box; /* Other styles same as .left*/}
.right {background-clip: content-box; /* Other styles same as .left*/}

background

最初,background 属性是其余背景相干属性的简写。子属性的程序无关紧要,因为每个属性的数据类型不同。然而对于 background-origin 和 background-clip,如果只指定了一个盒模型区域,那么这两个属性都会利用这个值。如果指定了两个,那么第一个值将用于 background-origin 属性。

正文完
 0