CSS Backgrounds and Borders Module Level 3

Editor’s Draft,

More details about this document
This version:
https://drafts.csswg.org/css-backgrounds/
Latest published version:
https://www.w3.org/TR/css-backgrounds-3/
Previous Versions:
Implementation Report:
https://wpt.fyi/results/css/css-backgrounds
Feedback:
CSSWG Issues Repository
Editors:
Elika J. Etemad / fantasai (Apple)
(Invited Expert)
Former Editor:
(W3C)
Suggest an Edit for this Spec:
GitHub Editor
Test Suite:
https://wpt.fyi/results/css/css-backgrounds/

Abstract

This draft contains the features of CSS relating to borders and backgrounds. The main extensions compared to level 2 are borders consisting of images, boxes with multiple backgrounds, boxes with rounded corners and boxes with shadows.

CSS is a language for describing the rendering of structured documents (such as HTML and XML) on screen, on paper, etc.

Status of this document

This is a public copy of the editors’ draft. It is provided for discussion only and may change at any moment. Its publication here does not imply endorsement of its contents by W3C. Don’t cite this document other than as work in progress.

Please send feedback by filing issues in GitHub (preferred), including the spec code “css-backgrounds” in the title, like this: “[css-backgrounds] …summary of comment…”. All issues and comments are archived. Alternately, feedback can be sent to the (archived) public mailing list [email protected].

This document is governed by the 18 August 2025 W3C Process Document.

The following features are at-risk, and may be dropped during the CR period:

“At-risk” is a W3C Process term-of-art, and does not necessarily imply that the feature is in danger of being dropped or delayed. It means that the WG believes the feature may have difficulty being interoperably implemented in a timely manner, and marking it as such allows the WG to drop the feature if necessary when transitioning to the Proposed Rec stage, without having to publish a new Candidate Rec without the feature first.

1. Introduction

This subsection is not normative.

When elements are rendered according to the CSS box model [CSS-BOX-3], each element is either not displayed at all, or formatted as one or more rectangular boxes. Each box has a rectangular content area, a band of padding around the content, a border around the padding, and a margin outside the border. (The margin may actually be negative, but margins have no influence on the background and border.)

Diagram of a typical box, showing the content, padding, border and margin areas
The various areas and edges of a typical box. (This diagram is explained in the CSS Box Model Module [CSS-BOX-3].)

The properties of this module deal with the decoration of the border area and with the background of the content, padding, and border areas. Additionally the box may be given a “drop-shadow” effect with the box-shadow property.

If an element is broken into multiple box fragments, box-decoration-break [CSS-BREAK-3] defines how the borders and background are divided over the various fragments. (An element can result in more than one fragment if it is broken at the end of a line, at the end of a column or at the end of a page; and continued in the next line, column or page.)

The relative stacking order of backgrounds, borders, and shadows is given in this module. For how these layers interact with other rendered content, see Appendix E “Elaborate description of Stacking Contexts” in [CSS2].

1.1. Module Interactions

This module replaces and extends the background and border features defined in [CSS2] sections 8.5 and 14.2.

All properties in this module apply to the ::first-letter pseudo-element. The background properties and border-radius properties also apply to the ::first-line pseudo-element. The UA may (but is not required to) apply the border-image or box-shadow properties to ::first-line. The UA must not apply the border-color/style/width properties to ::first-line. [CSS2]

1.2. Value Definitions

This specification follows the CSS property definition conventions from [CSS2] using the value definition syntax from [CSS-VALUES-3]. Value types not defined in this specification are defined in CSS Values & Units [CSS-VALUES-3]. Combination with other CSS modules may expand the definitions of these value types. For example, combining with CSS Images allows for using CSS gradients as background-image or border-image values. [CSS-IMAGES-3]

In addition to the property-specific values listed in their definitions, all properties defined in this specification also accept the CSS-wide keywords as their property value. For readability they have not been repeated explicitly.

2. Backgrounds

Each box has a background layer that may be fully transparent (the default), or filled with a color and/or one or more images. The background properties specify what color (background-color) and images (background-image) to use, and how they are sized, positioned, tiled, etc.

The background properties are not inherited, but the parent box’s background will shine through by default because of the initial transparent value on background-color.

2.1. Layering Multiple Background Images

The background of a box can have multiple background image layers. The number of layers is determined by the number of comma-separated values in the background-image property. Note that a value of none still creates a layer.

Tests

Each of the background images is sized, positioned, and tiled according to the corresponding value in the other background properties. The lists are matched up from the first value: excess values at the end are not used. If a property doesn’t have enough comma-separated values to match the number of layers, the UA must calculate its used value by repeating the list of values until there are enough.

For example, this set of declarations:
background-image: url(flower.png), url(ball.png), url(grass.png);
background-position: center center, 20% 80%, top left, bottom right;
background-origin: border-box, content-box;
background-repeat: no-repeat;

has exactly the same effect as this set, with the extra position dropped and the missing values for background-origin and background-repeat filled in (emphasized for clarity):

background-image: url(flower.png), url(ball.png), url(grass.png);
background-position: center center, 20% 80%, top left;
background-origin: border-box, content-box, border-box;
background-repeat: no-repeat, no-repeat, no-repeat;

The first image in the list is the layer closest to the user, the next one is painted behind the first, and so on. The background color, if present, is painted below all of the other layers.

Note: The border-image properties can also define a background image, which, if present, is painted on top of the background layers created by the background properties.

2.2. Base Color: the background-color property

Name: background-color
Value: <color>
Initial: transparent
Applies to: all elements
Inherited: no
Percentages: N/A
Computed value: computed color
Canonical order: per grammar
Animation type: by computed value
Tests

This property sets the background color of a box. This color is drawn behind any background images.

Example:
h1 { background-color: #F00 } /* Sets background to red. */

The background color is clipped according to the background-clip value associated with the bottom-most background image layer.

2.3. Image Sources: the background-image property

Name: background-image
Value: <bg-image>#
Initial: none
Applies to: all elements
Inherited: no
Percentages: N/A
Computed value: list, each item either an <image> or the keyword none
Canonical order: per grammar
Animation type: discrete
Tests

This property specifies the background image(s) of an element. Images are drawn with the first specified one on top (closest to the user) and each subsequent image behind the previous one. The property’s value is given as a comma-separated list of <bg-image> values where

<bg-image> = <image> | none

A value of none counts as a background image layer but draws nothing. An image that is empty (zero width or zero height), that fails to download, or that cannot be displayed (e.g., because it is not in a supported image format) likewise counts as a layer but draws nothing.

See § 2.1 Layering Multiple Background Images for how background-image interacts with other comma-separated background properties to form each background image layer.

When setting a background image, authors should also specify a background-color that will preserve contrast with the text for when the image is unavailable.

For accessibility reasons, authors should not use background images as the sole method of conveying important information. See Web Content Accessibility Guideline F3 [WCAG20]. Images are not accessible in non-graphical presentations, and background images specifically might be turned off in high-contrast display modes.

Note: Stylistic foreground images can be provided in CSS with the content property. Semantically-important foreground images should be provided in the document markup, e.g. with the <img> tag in HTML.

Note: Media fragments can be used to display a portion of an image. The CSS Images module will provide fallback syntax for image formats and include additional controls for image display.

Some examples specifying background images:
html { background-image: url("marble.svg") }
p { background-image: none }
div { background-image: url(tl.png), url(tr.png) }
main { background-image: radial-gradient(at bottom right, transparent, white); }

Implementations may optimize by not downloading and drawing images that are not visible (e.g., because they are behind other, fully opaque images).

2.4. Tiling Images: the background-repeat property

Name: background-repeat
Value: <repeat-style>#
Initial: repeat
Applies to: all elements
Inherited: no
Percentages: N/A
Computed value: list, each item a pair of keywords, one per dimension
Canonical order: per grammar
Animation type: discrete
Tests

This property specifies how background images are tiled after they have been sized and positioned. The property’s value is given as a comma-separated list of <repeat-style> values where

<repeat-style> = repeat-x | repeat-y | [repeat | space | round | no-repeat]{1,2}

Single values for <repeat-style> have the following meanings:

repeat-x
Computes to repeat no-repeat.
repeat-y
Computes to no-repeat repeat.
repeat
Computes to repeat repeat.
space
Computes to space space
round
Computes to round round
no-repeat
Computes to no-repeat no-repeat

If a <repeat-style> value has two keywords, the first one applies to the horizontal axis, the second to the vertical one, as follows:

repeat
The image is repeated in this direction as often as needed to cover the background painting area.
space
The image is repeated as often as will fit within the background positioning area without being clipped, and then the images are spaced out to fill the area. The first and last images touch the edges of the area. If the background painting area is larger than the background positioning area, then the pattern repeats to fill the background painting area. The value of background-position for this direction is ignored unless there is not enough space for two copies of the image in this axis, in which case only one image is placed, and background-position determines its position in this axis.
round
The image is repeated as often as will fit within the background positioning area. If it doesn’t fit a whole number of times, it is rescaled so that it does. See the formula under background-size. If the background painting area is larger than the background positioning area, then the pattern repeats to fill the background painting area.
no-repeat
The image is placed once and not repeated in this direction.

Unless one of the two keywords is no-repeat, the whole background painting area will be tiled, i.e., not just one vertical strip and one horizontal strip.

body {
  background: white url("pendant.png");
  background-repeat: repeat-y;
  background-position: center;
}
A centered background image, with copies repeated up and down the border, padding, and content areas.
The effect of repeat-y: One copy of the background image is centered, and other copies are put above and below it to make a vertical band behind the element.
body {
  background-image: url(dot.png) white;
  background-repeat: space
}
Image of an element with a dotted background
The effect of space: the image of a dot is tiled to cover the whole background and the images are equally spaced.

See § 2.1 Layering Multiple Background Images for how background-repeat interacts with other comma-separated background properties to form each background image layer.

2.5. Affixing Images: the background-attachment property

Name: background-attachment
Value: <attachment>#
Initial: scroll
Applies to: all elements
Inherited: no
Percentages: N/A
Computed value: list, each item the keyword as specified
Canonical order: per grammar
Animation type: discrete
Tests

If background images are specified, this property specifies whether they are fixed with regard to the viewport (fixed) or scroll along with the box (scroll) or its contents (local). The property’s value is given as a comma-separated list of <attachment> keywords where

<attachment> = scroll | fixed | local
fixed
The background is fixed with regard to the viewport. In paged media where there is no viewport, a fixed background is fixed with respect to the page box and therefore replicated on every page.

Note: There is only one viewport per view. Even if an box is a scroll container, a fixed background doesn’t move with the box.

local
The background is fixed with regard to the box’s contents: if the box has a scrolling mechanism, the background scrolls with the box’s contents, and the background painting area and background positioning area are relative to the scrollable overflow area of the box rather than to the border framing them. Because the scrollable overflow area does not include the border area, for scroll containers the border-box value of background-clip may be treated the same as padding-box.
scroll
The background is fixed with regard to the box itself and does not scroll with its contents. (It is effectively attached to the box’s border.)

Even if the image is fixed, it is still only visible when it is in the background painting area of the box or otherwise unclipped. (See § 2.11 Backgrounds of Special Elements for the cases when background images are not clipped.) Thus, unless the image is tiled, it may be invisible.

This example creates an infinite vertical band that remains “glued” to the viewport when the document is scrolled.
body {
  background: red url("pendant.gif");
  background-repeat: repeat-y;
  background-attachment: fixed;
}

Note: User agents that do not support fixed backgrounds (for example due to limitations of the hardware platform) will ignore declarations with the keyword fixed. For example:

body {
  /* For all UAs: */
  background: white url(paper.png) scroll;
  /* For UAs that do fixed backgrounds: */
  background: white url(ledger.png) fixed;
}
h1 {
  /* For all UAs: */
  background: silver;
  /* For UAs that do fixed backgrounds: */
  background: url(stripe.png) fixed, white url(ledger.png) fixed;
}

See § 2.1 Layering Multiple Background Images for how background-attachment interacts with other comma-separated background properties to form each background image layer.

2.6. Positioning Images: the background-position property

Name: background-position
Value: <bg-position>#
Initial: 0% 0%
Applies to: all elements
Inherited: no
Percentages: refer to size of background positioning area minus size of background image; see text
Computed value: list, each item a pair of offsets (horizontal and vertical) from the top left origin each given as a computed <length-percentage> value
Canonical order: per grammar
Animation type: repeatable list
Tests

If background images have been specified, this property specifies their initial position (after any resizing) within their corresponding background positioning area.

The property’s value is given as a comma-separated list of <bg-position> values where

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

If only one value is specified, the second value is assumed to be center. If two values are given, a <length-percentage> as the first value represents the horizontal position (or offset) and a <length-percentage> as the second value represents the vertical position (or offset). The <length-percentage> values here represent an offset of the top left corner of the background image from the top left corner of the background positioning area.

Note: A pair of keywords can be reordered, while a combination of keyword and length or percentage cannot. So center left is valid while 50% left is not.

If three or four values are given, then each <length-percentage> represents an offset and must be preceded by a keyword, which specifies from which edge the offset is given. For example, background-position: bottom 10px right 20px represents a 10px vertical offset up from the bottom edge and a 20px horizontal offset leftward from the right edge. If three values are given, the missing offset is assumed to be zero.

Positive values represent an offset inward from the edge of the background positioning area. Negative values represent an offset outward from the edge of the background positioning area.

The following declarations give the stated (horizontal, vertical) offsets from the top left corner:
background-position: left 10px top 15px;   /* 10px, 15px */
background-position: left      top     ;   /*  0px,  0px */
background-position:      10px     15px;   /* 10px, 15px */
background-position: left          15px;   /*  0px, 15px */
background-position:      10px top     ;   /* 10px,  0px */
background-position: left      top 15px;   /*  0px, 15px */
background-position: left 10px top     ;   /* 10px,  0px */
<percentage>
A percentage for the horizontal offset is relative to (width of background positioning area - width of background image). A percentage for the vertical offset is relative to (height of background positioning area - height of background image), where the size of the image is the size given by background-size.
For example, with a value pair of 0% 0%, the upper left corner of the image is aligned with the upper left corner of, usually, the box’s padding edge. A value pair of 100% 100% places the lower right corner of the image in the lower right corner of the area. With a value pair of 75% 50%, the point 75% across and 50% down the image is to be placed at the point 75% across and 50% down the area.
Diagram of image position within element
Diagram of the meaning of background-position: 75% 50%.
<length>
A length value gives a fixed length as the offset. For example, with a value pair of 2cm 1cm, the upper left corner of the image is placed 2cm to the right and 1cm below the upper left corner of the background positioning area.
top
Computes to 0% for the vertical position if one or two values are given, otherwise specifies the top edge as the origin for the next offset.
right
Computes to 100% for the horizontal position if one or two values are given, otherwise specifies the right edge as the origin for the next offset.
bottom
Computes to 100% for the vertical position if one or two values are given, otherwise specifies the bottom edge as the origin for the next offset.
left
Computes to 0% for the horizontal position if one or two values are given, otherwise specifies the left edge as the origin for the next offset.
center
Computes to 50% (left 50%) for the horizontal position if the horizontal position is not otherwise specified, or 50% (top 50%) for the vertical position if it is.
The following background shorthand declarations use keywords to set background-position to the stated percentage values.
body { background: url("banner.jpeg") right top }    /* 100%   0% */
body { background: url("banner.jpeg") top center }   /*  50%   0% */
body { background: url("banner.jpeg") center }       /*  50%  50% */
body { background: url("banner.jpeg") bottom }       /*  50% 100% */
In the example below, the (single) image is placed in the lower-right corner of the viewport.
body {
  background-image: url("logo.png");
  background-attachment: fixed;
  background-position: 100% 100%;
  background-repeat: no-repeat;
}
Background positions can also be relative to other corners than the top left. For example, the following puts the background image 10px from the bottom and 3em from the right:
background-position: right 3em bottom 10px

See § 2.1 Layering Multiple Background Images for how background-position interacts with other comma-separated background properties to form each background image layer.

2.6.1. Serialization of background-position values

The specified value and computed value of the <bg-position> type serialize exactly as defined in [CSS-VALUES-4] for <position>. For 3-value productions (which are not valid in <position>), the specified value serialization is identical to the equivalent 4-value syntax except that the omitted offset remains omitted.

2.7. Painting Area: the background-clip property

Name: background-clip
Value: <visual-box>#
Initial: border-box
Applies to: all elements
Inherited: no
Percentages: N/A
Computed value: list, each item a keyword as specified
Canonical order: per grammar
Animation type: repeatable list
Tests

Determines the background painting area, which determines the area within which the background is painted. Values have the following meanings:

border-box
The background is painted within (clipped to) the border box.
padding-box
The background is painted within (clipped to) the padding box.
content-box
The background is painted within (clipped to) the content box.

Note: The root element has a different background painting area and thus the background-clip property has no effect when specified on it. See § 2.11 Backgrounds of Special Elements.

Note: The background is always drawn behind the border, if any. See “Elaborate description of Stacking Contexts” in [CSS2] Appendix E.

See § 4.2 Corner Shaping for how border-radius affects the shape of the background painting area.

See § 2.1 Layering Multiple Background Images for how background-clip interacts with other comma-separated background properties to form each background image layer.

2.8. Positioning Area: the background-origin property

Name: background-origin
Value: <visual-box>#
Initial: padding-box
Applies to: all elements
Inherited: no
Percentages: N/A
Computed value: list, each item a keyword as specified
Canonical order: per grammar
Animation type: repeatable list
Tests