background

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

background 是一个 简写 CSS 属性,可以一次性设置所有的背景样式属性,例如颜色、图片、原点和尺寸,或重复方式。在 background 简写属性值声明中未设置的组件属性将被设为它们的默认值。

试一试

background: green;
background: content-box radial-gradient(crimson, skyblue);
background: no-repeat url("/shared-assets/images/examples/lizard.png");
background: left 5% / 15% 60% repeat-x
  url("/shared-assets/images/examples/star.png");
background:
  center / contain no-repeat
    url("/shared-assets/images/examples/firefox-logo.svg"),
  #eeeeee 35% url("/shared-assets/images/examples/lizard.png");
<section id="default-example">
  <div id="example-element"></div>
</section>
#example-element {
  min-width: 100%;
  min-height: 100%;
  padding: 10%;
}

构成属性

此属性是以下 CSS 属性的简写:

语法

css
/* Using a <background-color> */
background: green;

/* Using a <bg-image> and <repeat-style> */
background: url("test.jpg") repeat-y;

/* Using a <visual-box> and <'background-color'> */
background: border-box red;

/* A single image, centered and scaled */
background: no-repeat center/80% url("../img/image.png");

/* Global values */
background: inherit;
background: initial;
background: revert;
background: revert-layer;
background: unset;

background 属性可以指定为一个或多个背景层,以逗号分隔。

每一层的语法如下:

  • 每一层可以包含以下值的零次或一次出现:

    • <attachment>
    • <bg-image>
    • <bg-position>
    • <bg-size>
    • <repeat-style>
  • <bg-size> 值只能紧跟在 <bg-position> 之后,并用“/”字符分隔,例如:center/80%

  • <visual-box> 值可以出现零次、一次或两次。如果出现一次,它会同时设置 background-originbackground-clip。如果出现两次,第一次出现会设置 background-origin,第二次则设置 background-clip

  • <'background-color'> 值只能在指定的最后一层中包含。

<attachment>

background-attachment。默认值:scroll

<visual-box>

background-clipbackground-origin。默认值分别为:border-boxpadding-box

<'background-color'>

background-color。默认值:transparent

<bg-image>

background-image。默认值:none

<bg-position>

background-position。默认值:0% 0%。

<repeat-style>

background-repeat。默认值:repeat

<bg-size>

background-size。默认值:auto

以下三行 CSS 代码是等效的:

css
background: none;
background: transparent;
background: repeat scroll 0% 0% / auto padding-box border-box none transparent;

正式定义

初始值作为简写中的每个属性
应用于所有元素。也适用于 ::first-letter::first-line
继承性
百分比作为简写中的每个属性
  • background-position: 参照背景定位区域的尺寸减去背景图像的尺寸;尺寸对于水平偏移量指的是宽度,对于垂直偏移量指的是高度
  • background-size: 相对于背景定位区域
计算值作为简写中的每个属性
动画类型作为简写中的每个属性

正式语法

background = 
<bg-layer>#? , <final-bg-layer>

<bg-layer> =
<bg-image> ||
<bg-position> [ / <bg-size> ]? ||
<repeat-style> ||
<attachment> ||
<visual-box> ||
<visual-box>

<final-bg-layer> =
<bg-image> ||
<bg-position> [ / <bg-size> ]? ||
<repeat-style> ||
<attachment> ||
<visual-box> ||
<visual-box> ||
<'background-color'>

<bg-image> =
<image> |
none

<bg-position> =
<position> |
<position-three>

<bg-size> =
[ <length-percentage [0,∞]> | auto ]{1,2} |
cover |
contain

<repeat-style> =
repeat-x |
repeat-y |
<repetition>{1,2}

<attachment> =
scroll |
fixed |
local

<visual-box> =
content-box |
padding-box |
border-box

<background-color> =
<color>

<image> =
<url> |
<image()> |
<image-set()> |
<cross-fade()> |
<element()> |
<gradient>

<position> =
<position-one> |
<position-two> |
<position-four>

<position-three> =
[ left | center | right ] && [ [ top | bottom ] <length-percentage> ] |
[ [ left | right ] <length-percentage> ] && [ top | center | bottom ]

<length-percentage> =
<length> |
<percentage>

<repetition> =
repeat |
space |
round |
no-repeat

<image()> =
image( <image-tags>? [ <image-src>? , <color>? ]! )

<image-set()> =
image-set( <image-set-option># )

<cross-fade()> =
cross-fade( <cf-image># )

<element()> =
element( <id-selector> )

<position-one> =
left |
center |
right |
top |
bottom |
x-start |
x-end |
y-start |
y-end |
block-start |
block-end |
inline-start |
inline-end |
<length-percentage>

<position-two> =
[ left | center | right | x-start | x-end ] && [ top | center | bottom | y-start | y-end ] |
[ left | center | right | x-start | x-end | <length-percentage> ] [ top | center | bottom | y-start | y-end | <length-percentage> ] |
[ block-start | center | block-end ] && [ inline-start | center | inline-end ] |
[ start | center | end ]{2}

<position-four> =
[ [ left | right | x-start | x-end ] <length-percentage> ] && [ [ top | bottom | y-start | y-end ] <length-percentage> ] |
[ [ block-start | block-end ] <length-percentage> ] && [ [ inline-start | inline-end ] <length-percentage> ] |
[ [ start | end ] <length-percentage> ]{2}

<image-tags> =
ltr |
rtl

<image-src> =
<url> |
<string>

<image-set-option> =
[ <image> | <string> ] [ <resolution> || type( <string> ) ]?

<cf-image> =
[ <image> | <color> ] &&
<percentage [0,100]>?

<id-selector> =
<hash-token>

无障碍

浏览器不会向辅助技术提供任何有关背景图像的特殊信息。这对于屏幕阅读器来说尤其重要,因为屏幕阅读器不会播报背景图像的存在,因此也不会向其用户传达任何信息。如果图像包含对理解页面整体目的至关重要的信息,最好在文档中以语义化的方式对其进行描述。

示例

使用颜色关键字和图像设置背景

HTML

html
<p class="top-banner">
  Starry sky<br />
  Twinkle twinkle<br />
  Starry sky
</p>
<p class="warning">Here is a paragraph</p>
<p></p>

CSS

css
.warning {
  background: pink;
}

.top-banner {
  background: url("star-solid.gif") #9999ff repeat-y fixed;
}

结果

规范

规范
CSS Backgrounds and Borders Module Level 3
# background

浏览器兼容性

另见