HTML Dog
跳至导航

CSS 属性:background

背景颜色和图像的特性。

一个简写属性,用于组合

可能的值

背景特性子值列表。可能的子值遵循与之前相应属性的可能值相同的规则。子值列表遵循特定的格式

[image] [position] / [size] [repeat] [attachment] [origin] [clip] [color]

完整示例:url("bg.jpg") 20% 0% / contain repeat-y fixed content-box padding-box #fff

并非所有值都是必需的

基本示例:url("bg.jpg") 20% 0% repeat-y fixed #fff

最小示例:url("bg.jpg") #fff

inheritinitialunset 也可以单独使用。

background: url("bg.png") top right no-repeat #ccc

多个背景

可以通过用逗号分隔每个背景值列表来应用多个背景。第一个背景将位于最上方,“最接近”用户。后续图像将应用于其前面的背景下方。

background-color 部分应仅应用于指定的最后一个背景。

示例:url("upperImage.png") 0 0 no-repeat, url("lowerImage.png") right repeat-y #fff;

重复的背景图像、单次背景图像,以及将它们组合在同一个框中。

示例


.shorthand1 { url("bg.jpg") 20% 0% / 99px 33px repeat-y fixed content-box padding-box #fff; }
/* ...is the equivalent of... */
.longhand1 {
    background-image: url("bg.jpg");
    background-position: 20% 0%;
    background-size: 99px 33px;
    background-repeat: repeat-y;
    background-attachment: fixed;
    background-origin: content-box;
    background-clip: padding-box;
    background-color: #fff;
}

.shorthand2 { background: fuchsia; }
/* ...is the equivalent of... */
.longhand2 {
    background-color: fuchsia;
    background-image: initial; /* (none) */
    background-position: initial; /* (0% 0%) */
    background-size: initial; /* (auto) */
    background-repeat: initial; /* (repeat) */
    background-attachment: initial; /* (scroll) */
    background-origin: initial; /* (padding-box) */
    background-clip: initial; /* (border-box) */
}