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.)
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.
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
- background-color-animation-backdrop-infinite-duration-crash.html (live test) (source)
- background-color-animation-element-not-visible-at-current-viewport.html (live test) (source)
- background-color-animation-fallback-additive-keyframe.html (live test) (source)
- background-color-animation-fallback-missing-0-percent.html (live test) (source)
- background-color-animation-fallback-missing-100-percent.html (live test) (source)
- background-color-animation-fallback-replace.html (live test) (source)
- background-color-animation-field-crash.html (live test) (source)
- background-color-animation-fragmented.html (live test) (source)
- background-color-animation-half-opaque.html (live test) (source)
- background-color-animation-in-body.html (live test) (source)
- background-color-animation-non-empty-no-draw-crash.html (live test) (source)
- background-color-animation-non-zero-size-element-change-to-zero.html (live test) (source)
- background-color-animation-pseudo-element.html (live test) (source)
- background-color-animation-removed-element-crash.html (live test) (source)
- background-color-animation-single-keyframe.html (live test) (source)
- background-color-animation-three-keyframes1.html (live test) (source)
- background-color-animation-three-keyframes2.html (live test) (source)
- background-color-animation-three-keyframes3.html (live test) (source)
- background-color-animation-will-change-contents.html (live test) (source)
- background-color-animation-with-blur.html (live test) (source)
- background-color-animation-with-images.html (live test) (source)
- background-color-animation-with-mask.html (live test) (source)
- background-color-animation-with-table1.html (live test) (source)
- background-color-animation-with-table2.html (live test) (source)
- background-color-animation-with-table3.html (live test) (source)
- background-color-animation-with-table4.html (live test) (source)
- background-color-animation-with-zero-playbackRate.html (live test) (source)
- background-color-animation-zero-size-element-change-to-non-zero.html (live test) (source)
- background-color-animation-zero-size-element.html (live test) (source)
- background-color-animation.html (live test) (source)
- background-color-interpolation.html (live test) (source)
- background-color-scroll-into-viewport.html (live test) (source)
- background-color-transition-colormix.html (live test) (source)
- background-color-transition-currentcolor.html (live test) (source)
- background-color-transition.html (live test) (source)
- background-color-transparent-animation-in-body.html (live test) (source)
- background-color-animation-with-zero-alpha.html (live test) (source)
- background-color-transition-obscured.html (live test) (source)
- background-color-transition-with-delay.html (live test) (source)
- background-color-transition-with-initially-transparent.html (live test) (source)
- two-background-color-animation-diff-length1.html (live test) (source)
- two-background-color-animation-diff-length2.html (live test) (source)
- two-background-color-animation-diff-length3.html (live test) (source)
- background-none-none-and-color.html (live test) (source)
- background-color-body-propagation-001.html (live test) (source)
- background-color-body-propagation-002.html (live test) (source)
- background-color-body-propagation-003.html (live test) (source)
- background-color-body-propagation-004.html (live test) (source)
- background-color-body-propagation-005.html (live test) (source)
- background-color-body-propagation-006.html (live test) (source)
- background-color-body-propagation-007.html (live test) (source)
- background-color-body-propagation-008.html (live test) (source)
- background-color-body-propagation-009.html (live test) (source)
- background-color-clip.html (live test) (source)
- background-color-root-propagation-001.html (live test) (source)
- background-color-root-propagation-002.html (live test) (source)
- bg-color-with-gradient.html (live test) (source)
- child-move-reveals-parent-background.html (live test) (source)
- color-mix-currentcolor-background-repaint-parent.html (live test) (source)
- color-mix-currentcolor-background-repaint.html (live test) (source)
- simple-bg-color.html (live test) (source)
- inheritance.sub.html (live test) (source)
- inline-background-rtl-001.html (live test) (source)
- background-color-computed.html (live test) (source)
- background-color-invalid.html (live test) (source)
- background-color-valid.html (live test) (source)
- color-behind-images.htm (visual test) (source)
This property sets the background color of a box. This color is drawn behind any background images.
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
- background-image-001.html (live test) (source)
- background-image-002.html (live test) (source)
- background-image-003.html (live test) (source)
- background-image-004.html (live test) (source)
- background-image-005.html (live test) (source)
- background-image-006.html (live test) (source)
- background-image-007.html (live test) (source)
- background-image-centered-with-border-radius.html (live test) (source)
- background-image-centered.html (live test) (source)
- background-image-cors-no-reload.html (live test) (source)
- background-image-cover-zoomed-1.html (live test) (source)
- background-image-first-letter.html (live test) (source)
- background-image-first-line.html (live test) (source)
- background-image-gradient-currentcolor-conic-repaint.html (live test) (source)
- background-image-gradient-currentcolor-linear-repaint.html (live test) (source)
- background-image-gradient-currentcolor-radial-repaint.html (live test) (source)
- background-image-gradient-currentcolor-visited.html (live test) (source)
- background-image-gradient-interpolation-repaint.html (live test) (source)
- background-image-large-with-auto.html (live test) (source)
- background-image-none-gradient-repaint.html (live test) (source)
- background-image-shared-stylesheet.html (live test) (source)
- background-image-table-cells-straddling-no-repeat.html (live test) (source)
- background-image-table-cells-zoomed.html (live test) (source)
- background-image-with-border-radius-fidelity.html (live test) (source)
- background-image-interpolation.html (live test) (source)
- inheritance.sub.html (live test) (source)
- background-image-computed.sub.html (live test) (source)
- background-image-invalid.html (live test) (source)
- background-image-valid.html (live test) (source)
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.
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
- discrete-no-interpolation.html (live test) (source)
- background-repeat-round-1a.html (live test) (source)
- background-repeat-round-1b.html (live test) (source)
- background-repeat-round-1c.html (live test) (source)
- background-repeat-round-1d.html (live test) (source)
- background-repeat-round-1e.html (live test) (source)
- background-repeat-round-2.html (live test) (source)
- background-repeat-round-3.html (live test) (source)
- background-repeat-round-4.html (live test) (source)
- background-repeat-space-10.html (live test) (source)
- background-repeat-space-1a.html (live test) (source)
- background-repeat-space-1b.html (live test) (source)
- background-repeat-space-1c.html (live test) (source)
- background-repeat-space-2.html (live test) (source)
- background-repeat-space-3.html (live test) (source)
- background-repeat-space-4.html (live test) (source)
- background-repeat-space-5.html (live test) (source)
- background-repeat-space-6.html (live test) (source)
- background-repeat-space-7.html (live test) (source)
- background-repeat-space-8.html (live test) (source)
- background-repeat-space-9.html (live test) (source)
- background-repeat-no-repeat.xht (live test) (source)
- background-repeat-repeat-x.xht (live test) (source)
- background-repeat-repeat-y.xht (live test) (source)
- background-repeat-round-roundup.xht (live test) (source)
- background-repeat-round.xht (live test) (source)
- background-repeat-space.xht (live test) (source)
- gradient-repeat-spaced-with-borders.html (live test) (source)
- inheritance.sub.html (live test) (source)
- background-repeat-computed.html (live test) (source)
- background-repeat-invalid.html (live test) (source)
- background-repeat-valid.html (live test) (source)
- subpixel-repeat-no-repeat-mix.html (live test) (source)
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;
}
body {
background-image: url(dot.png) white;
background-repeat: space
}
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
- discrete-no-interpolation.html (live test) (source)
- background-attachment-350.html (live test) (source)
- background-attachment-353.html (live test) (source)
- background-attachment-fixed-block-002.html (live test) (source)
- background-attachment-fixed-border-radius-offset.html (live test) (source)
- background-attachment-fixed-inline-002.html (live test) (source)
- background-attachment-fixed-inline-scrolled.html (live test) (source)
- background-attachment-fixed-inside-transform-1.html (live test) (source)
- background-attachment-local-block-002.html (live test) (source)
- background-attachment-local-hidden.html (live test) (source)
- attachment-local-clipping-color-1.html (live test) (source)
- attachment-local-clipping-color-2.html (live test) (source)
- attachment-local-clipping-color-3.html (live test) (source)
- attachment-local-clipping-color-4.html (live test) (source)
- attachment-local-clipping-color-5.html (live test) (source)
- attachment-local-clipping-color-6.html (live test) (source)
- attachment-local-clipping-image-1.html (live test) (source)
- attachment-local-clipping-image-2.html (live test) (source)
- attachment-local-clipping-image-3.html (live test) (source)
- attachment-local-clipping-image-4.html (live test) (source)
- attachment-local-clipping-image-5.html (live test) (source)
- attachment-local-clipping-image-6.html (live test) (source)
- attachment-local-positioning-2.html (live test) (source)
- attachment-local-positioning-3.html (live test) (source)
- attachment-local-positioning-4.html (live test) (source)
- attachment-local-positioning-5.html (live test) (source)
- attachment-scroll-positioning-1.html (live test) (source)
- background-attachment-margin-root-001.html (live test) (source)
- background-attachment-margin-root-002.html (live test) (source)
- inheritance.sub.html (live test) (source)
- local-attachment-content-box-scroll.html (live test) (source)
- background-attachment-computed.html (live test) (source)
- background-attachment-invalid.html (live test) (source)
- background-attachment-valid.html (live test) (source)
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.
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
- background-position-composition.html (live test) (source)
- background-position-interpolation.html (live test) (source)
- background-position-calc-minmax-001.html (live test) (source)
- background-position-negative-percentage-comparison-002.html (live test) (source)
- background-position-negative-percentage-comparison.html (live test) (source)
- background-position-three-four-values.html (live test) (source)
- background-position-bottom-right-repeat-round.html (live test) (source)
- background-position-right-in-body.html (live test) (source)
- inheritance.sub.html (live test) (source)
- background-position-computed.html (live test) (source)
- background-position-invalid.html (live test) (source)
- background-position-valid.html (live test) (source)
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.
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 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.
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% */
body {
background-image: url("logo.png");
background-attachment: fixed;
background-position: 100% 100%;
background-repeat: no-repeat;
}
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
- discrete-no-interpolation.html (live test) (source)
- background-clip-001.html (live test) (source)
- background-clip-002.html (live test) (source)
- background-clip-003.html (live test) (source)
- background-clip-004.html (live test) (source)
- background-clip-005.html (live test) (source)
- background-clip-006.html (live test) (source)
- background-clip-007.html (live test) (source)
- background-clip-008.html (live test) (source)
- background-clip-009.html (live test) (source)
- background-clip-010.html (live test) (source)
- background-clip-color-repaint.html (live test) (source)
- background-clip-color.html (live test) (source)
- background-clip-content-box-001.html (live test) (source)
- background-clip-content-box-002.html (live test) (source)
- background-clip-content-box-with-border-radius-002.html (live test) (source)
- background-clip-content-box-with-border-radius-003.html (live test) (source)
- background-clip-padding-box-with-border-radius-002.html (live test) (source)
- background-clip-padding-box-with-border-radius-003.html (live test) (source)
- background-clip-padding-box-001.html (live test) (source)
- background-clip-padding-box-with-border-radius.html (live test) (source)
- clip-border-area-background-geometry.html (live test) (source)
- clip-border-area-border-image.html (live test) (source)
- clip-border-area-border-on-top.html (live test) (source)
- clip-border-area-box-decoration-break.html (live test) (source)
- clip-border-area-multiple-backgrounds.html (live test) (source)
- clip-border-area-on-body-not-propagated-to-root.html (live test) (source)
- clip-border-area-on-body-propagated-to-root.html (live test) (source)
- clip-border-area-on-root.html (live test) (source)
- clip-text-ellipsis.html (live test) (source)
- clip-text-on-body-not-propagated-to-root.html (live test) (source)
- clip-text-on-body-propagated-to-root.html (live test) (source)
- clip-text-on-root.html (live test) (source)
- clip-text-out-of-flow-child.html (live test) (source)
- clip-text-text-decorations.html (live test) (source)
- clip-text-text-emphasis.html (live test) (source)
- clip-rounded-corner.html (live test) (source)
- clip-text-animated-text.html (live test) (source)
- clip-text-dynamic-2.html (live test) (source)
- clip-text-flex.html (live test) (source)
- clip-text-multi-line.html (live test) (source)
- background-clip_padding-box.html (live test) (source)
- background-paint-order-001.html (live test) (source)
- background-rounded-image-clip-001.html (live test) (source)
- background-rounded-image-clip-002.html (live test) (source)
- css3-background-clip-border-box.html (live test) (source)
- css3-background-clip-content-box.html (live test) (source)
- css3-background-clip-padding-box.html (live test) (source)
- css3-background-clip.html (live test) (source)
- inheritance.sub.html (live test) (source)
- local-attachment-content-box-scroll.html (live test) (source)
- background-clip-computed.html (live test) (source)
- background-clip-invalid.html (live test) (source)
- background-clip-valid.html (live test) (source)
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
- discrete-no-interpolation.html (live test) (source)
- background-position-origin-interpolation.html (live test) (source)
- background-gradient-subpixel-fills-area.html (live test) (source)
- background-origin-001.html (live test) (source)
- background-origin-002.html (live test) (source)
- background-origin-003.html (live test) (source)
- background-origin-004.html (live test) (source)
- background-origin-005.html (live test) (source)
- background-origin-006.html (live test) (source)
- background-origin-007.html (live test) (source)
- background-origin-008.html (live test) (source)
- origin-border-box.html (live test) (source)
- origin-border-box_with_position.html (live test) (source)
- origin-border-box_with_radius.html (live test) (source)
- origin-border-box_with_size.html (live test) (source)
- origin-content-box.html (live test) (source)
- origin-content-box_with_position.html (live test) (source)
- origin-content-box_with_radius.html (live test) (source)
- origin-content-box_with_size.html (live test) (source)
- origin-padding-box.html (live test) (source)
- origin-padding-box_with_position.html (live test) (source)
- origin-padding-box_with_radius.html (live test) (source)
- origin-padding-box_with_size.html (live test) (source)
- css3-background-origin-border-box.html (live test) (source)
- css3-background-origin-content-box.html (live test) (source)
- css3-background-origin-padding-box.html (live test) (source)
- inheritance.sub.html (live test) (source)
- background-origin-computed.html (live test) (source)
- background-origin-invalid.html (live test) (source)
- background-origin-valid.html (live test) (source)
This property determines the background positioning area: the area within which any background images are positioned. For elements rendered as multiple box fragments (e.g., inline boxes on several lines, boxes on several pages), specifies which boxes box-decoration-break [CSS-BREAK-3] operates on to determine the background positioning area(s).
- padding-box
- The position is relative to the padding box. (For single boxes 0 0 is the upper left corner of the padding edge, 100% 100% is the lower right corner.)
- border-box
- The position is relative to the border box.
- content-box
- The position is relative to the content box.
If the background-attachment value for this layer is fixed, then this property has no effect: in this case the background positioning area is the initial containing block.
Note: If background-clip is padding-box, background-origin is border-box, background-position is top left (the initial value), and the element has a non-zero border, then the top and left edges of the background image will be clipped.
See § 2.1 Layering Multiple Background Images for how background-origin interacts with other comma-separated background properties to form each background image layer.
2.9. Sizing Images: the background-size property
| Name: | background-size |
|---|---|
| Value: | <bg-size># |
| Initial: | auto |
| Applies to: | all elements |
| Inherited: | no |
| Percentages: | see text |
| Computed value: | list, each item a pair of sizes (one per axis) each represented as either a keyword or a computed <length-percentage> value |
| Canonical order: | per grammar |
| Animation type: | repeatable list |
Tests
- background-size-composition.html (live test) (source)
- background-size-interpolation.html (live test) (source)
- background-size-001.html (live test) (source)
- background-size-002.html (live test) (source)
- background-size-005.html (live test) (source)
- background-size-006.html (live test) (source)
- background-size-007.html (live test) (source)
- background-size-008.html (live test) (source)
- background-size-009.html (live test) (source)
- background-size-010.html (live test) (source)
- background-size-011.html (live test) (source)
- background-size-012.html (live test) (source)
- background-size-013.html (live test) (source)
- background-size-014.html (live test) (source)
- background-size-015.html (live test) (source)
- background-size-016.html (live test) (source)
- background-size-017.html (live test) (source)
- background-size-018.html (live test) (source)
- background-size-019.html (live test) (source)
- background-size-020.html (live test) (source)
- background-size-021.html (live test) (source)
- background-size-025.html (live test) (source)
- background-size-026.html (live test) (source)
- background-size-027.html (live test) (source)
- background-size-028.html (live test) (source)
- background-size-029.html (live test) (source)
- background-size-030.html (live test) (source)
- background-size-031.html (live test) (source)
- background-size-034.html (live test) (source)
- background-size-041.html (live test) (source)
- background-size-042.html (live test) (source)
- background-size-043.html (live test) (source)
- background-size-044.html (live test) (source)
- background-size-contain-001.html (live test) (source)
- background-size-contain-002.html (live test) (source)
- background-size-cover-001.html (live test) (source)
- background-size-cover-002.html (live test) (source)
- background-size-cover-003.html (live test) (source)
- background-size-one-value-1x1-image.html (live test) (source)
- background-size-percentage-root.html (live test) (source)
- background-size-with-negative-value.html (live test) (source)
- background-size-contain-svg-view.html (live test) (source)
- background-size-contain.xht (live test) (source)
- background-size-cover-contain-001.xht (live test) (source)
- background-size-cover-contain-002.xht (live test) (source)
- background-size-cover-svg-view.html (live test) (source)
- background-size-cover-svg.html (live test) (source)
- background-size-cover.xht (live test) (source)
- background-size-near-zero-color.html (live test) (source)
- background-size-near-zero-gradient.html (live test) (source)
- background-size-near-zero-png.html (live test) (source)
- background-size-near-zero-svg.html (live test) (source)
- background-size-vector-001.html (live test) (source)
- background-size-vector-002.html (live test) (source)
- background-size-vector-003.html (live test) (source)
- background-size-vector-004.html (live test) (source)
- background-size-vector-005.html (live test) (source)
- background-size-vector-006.html (live test) (source)
- background-size-vector-007.html (live test) (source)
- background-size-vector-008.html (live test) (source)
- background-size-vector-009.html (live test) (source)
- background-size-vector-010.html (live test) (source)
- background-size-vector-011.html (live test) (source)
- background-size-vector-012.html (live test) (source)
- background-size-vector-013.html (live test) (source)
- background-size-vector-014.html (live test) (source)
- background-size-vector-015.html (live test) (source)
- background-size-vector-016.html (live test) (source)
- background-size-vector-017.html (live test) (source)
- background-size-vector-018.html (live test) (source)
- background-size-vector-019.html (live test) (source)
- background-size-vector-020.html (live test) (source)
- background-size-vector-021.html (live test) (source)
- background-size-vector-022.html (live test) (source)
- background-size-vector-023.html (live test) (source)
- background-size-vector-024.html (live test) (source)
- background-size-vector-025.html (live test) (source)
- background-size-vector-026.html (live test) (source)
- background-size-vector-027.html (live test) (source)
- background-size-vector-028.html (live test) (source)
- background-size-vector-029.html (live test) (source)
- diagonal-percentage-vector-background.html (live test) (source)
- tall--auto--omitted-width-percent-height.html (live test) (source)
- tall--auto--percent-width-nonpercent-height-viewbox.html (live test) (source)
- tall--auto--percent-width-nonpercent-height.html (live test) (source)
- tall--auto--percent-width-omitted-height-viewbox.html (live test) (source)
- tall--auto--percent-width-omitted-height.html (live test) (source)
- tall--auto--percent-width-percent-height-viewbox.html (live test) (source)
- tall--auto--percent-width-percent-height.html (live test) (source)
- tall--auto-32px--nonpercent-width-nonpercent-height-viewbox.html (live test) (source)
- tall--auto-32px--nonpercent-width-nonpercent-height.html (live test) (source)
- tall--auto-32px--nonpercent-width-omitted-height-viewbox.html (live test) (source)
- tall--auto-32px--nonpercent-width-omitted-height.html (live test) (source)
- tall--auto-32px--nonpercent-width-percent-height-viewbox.html (live test) (source)
- tall--auto-32px--nonpercent-width-percent-height.html (live test) (source)
- tall--auto-32px--omitted-width-nonpercent-height-viewbox.html (live test) (source)
- tall--auto-32px--omitted-width-nonpercent-height.html (live test) (source)
- tall--auto-32px--omitted-width-omitted-height-viewbox.html (live test) (source)
- tall--auto-32px--omitted-width-omitted-height.html (live test) (source)
- tall--auto-32px--omitted-width-percent-height-viewbox.html (live test) (source)
- tall--auto-32px--omitted-width-percent-height.html (live test) (source)
- tall--auto-32px--percent-width-nonpercent-height-viewbox.html (live test) (source)
- tall--auto-32px--percent-width-nonpercent-height.html (live test) (source)
- tall--auto-32px--percent-width-omitted-height-viewbox.html (live test) (source)
- tall--auto-32px--percent-width-omitted-height.html (live test) (source)
- tall--auto-32px--percent-width-percent-height-viewbox.html (live test) (source)
- tall--auto-32px--percent-width-percent-height.html (live test) (source)
- tall--contain--height.html (live test) (source)
- tall--contain--nonpercent-width-nonpercent-height-viewbox.html (live test) (source)
- tall--contain--nonpercent-width-nonpercent-height.html (live test) (source)
- tall--contain--nonpercent-width-omitted-height-viewbox.html (live test) (source)
- tall--contain--nonpercent-width-omitted-height.html (live test) (source)
- tall--contain--nonpercent-width-percent-height-viewbox.html (live test) (source)
- tall--contain--nonpercent-width-percent-height.html (live test) (source)
- tall--contain--omitted-width-nonpercent-height-viewbox.html (live test) (source)
- tall--contain--omitted-width-nonpercent-height.html (live test) (source)
- tall--contain--omitted-width-omitted-height-viewbox.html (live test) (source)
- tall--contain--omitted-width-omitted-height.html (live test) (source)
- tall--contain--omitted-width-percent-height-viewbox.html (live test) (source)
- tall--contain--omitted-width-percent-height.html (live test) (source)
- tall--contain--percent-width-nonpercent-height-viewbox.html (live test) (source)
- tall--contain--percent-width-nonpercent-height.html (live test) (source)
- tall--contain--percent-width-omitted-height-viewbox.html (live test) (source)
- tall--contain--percent-width-omitted-height.html (live test) (source)
- tall--contain--percent-width-percent-height-viewbox.html (live test) (source)
- tall--contain--percent-width-percent-height.html (live test) (source)
- tall--contain--width.html (live test) (source)
- tall--cover--height.html (live test) (source)
- tall--cover--nonpercent-width-nonpercent-height--crisp.html (live test) (source)
- tall--cover--nonpercent-width-nonpercent-height-viewbox--crisp.html (live test) (source)
- tall--cover--nonpercent-width-nonpercent-height-viewbox.html (live test) (source)
- tall--cover--nonpercent-width-nonpercent-height.html (live test) (source)
- tall--cover--nonpercent-width-omitted-height-viewbox.html (live test) (source)
- tall--cover--nonpercent-width-omitted-height.html (live test) (source)
- tall--cover--nonpercent-width-percent-height-viewbox.html (live test) (source)
- tall--cover--nonpercent-width-percent-height.html (live test) (source)
- tall--cover--omitted-width-nonpercent-height-viewbox.html (live test) (source)
- tall--cover--omitted-width-nonpercent-height.html (live test) (source)
- tall--cover--omitted-width-omitted-height-viewbox.html (live test) (source)
- tall--cover--omitted-width-omitted-height.html (live test) (source)
- tall--cover--omitted-width-percent-height-viewbox.html (live test) (source)
- tall--cover--omitted-width-percent-height.html (live test) (source)
- tall--cover--percent-width-nonpercent-height-viewbox.html (live test) (source)
- tall--cover--percent-width-nonpercent-height.html (live test) (source)
- tall--cover--percent-width-omitted-height-viewbox.html (live test) (source)
- tall--cover--percent-width-omitted-height.html (live test) (source)
- tall--cover--percent-width-percent-height-viewbox.html (live test) (source)
- tall--cover--percent-width-percent-height.html (live test) (source)
- tall--cover--width.html (live test) (source)
- wide--12px-auto--nonpercent-width-nonpercent-height-viewbox.html (live test) (source)
- wide--12px-auto--nonpercent-width-nonpercent-height.html (live test) (source)
- wide--12px-auto--nonpercent-width-omitted-height-viewbox.html (live test) (source)
- wide--12px-auto--nonpercent-width-omitted-height.html (live test) (source)
- wide--12px-auto--nonpercent-width-percent-height-viewbox.html (live test) (source)
- wide--12px-auto--nonpercent-width-percent-height.html (live test) (source)
- wide--12px-auto--omitted-width-nonpercent-height-viewbox.html (live test) (source)
- wide--12px-auto--omitted-width-nonpercent-height.html (live test) (source)
- wide--12px-auto--omitted-width-omitted-height-viewbox.html (live test) (source)
- wide--12px-auto--omitted-width-omitted-height.html (live test) (source)
- wide--12px-auto--omitted-width-percent-height-viewbox.html (live test) (source)
- wide--12px-auto--omitted-width-percent-height.html (live test) (source)
- wide--12px-auto--percent-width-nonpercent-height-viewbox.html (live test) (source)
- wide--12px-auto--percent-width-nonpercent-height.html (live test) (source)
- wide--12px-auto--percent-width-omitted-height-viewbox.html (live test) (source)
- wide--12px-auto--percent-width-omitted-height.html (live test) (source)
- wide--12px-auto--percent-width-percent-height-viewbox.html (live test) (source)
- wide--12px-auto--percent-width-percent-height.html (live test) (source)
- wide--auto--nonpercent-width-nonpercent-height-viewbox.html (live test) (source)
- wide--auto--nonpercent-width-nonpercent-height.html (live test) (source)
- wide--auto--nonpercent-width-omitted-height-viewbox.html (live test) (source)
- wide--auto--nonpercent-width-omitted-height.html (live test) (source)
- wide--auto--nonpercent-width-percent-height-viewbox.html (live test) (source)
- wide--auto--nonpercent-width-percent-height.html (live test) (source)
- wide--auto--omitted-width-nonpercent-height-viewbox.html (live test) (source)
- wide--auto--omitted-width-nonpercent-height.html (live test) (source)
- wide--auto--omitted-width-omitted-height-viewbox.html (live test) (source)
- wide--auto--omitted-width-omitted-height.html (live test) (source)
- wide--auto--omitted-width-percent-height-viewbox.html (live test) (source)
- wide--auto--omitted-width-percent-height.html (live test) (source)
- wide--auto--percent-width-nonpercent-height-viewbox.html (live test) (source)
- wide--auto--percent-width-nonpercent-height.html (live test) (source)
- wide--auto--percent-width-omitted-height-viewbox.html (live test) (source)
- wide--auto--percent-width-omitted-height.html (live test) (source)
- wide--auto--percent-width-percent-height-viewbox.html (live test) (source)
- wide--auto--percent-width-percent-height.html (live test) (source)
- wide--auto-32px--nonpercent-width-nonpercent-height-viewbox.html (live test) (source)
- wide--auto-32px--nonpercent-width-nonpercent-height.html (live test) (source)
- wide--auto-32px--nonpercent-width-omitted-height-viewbox.html (live test) (source)
- wide--auto-32px--nonpercent-width-omitted-height.html (live test) (source)
- wide--auto-32px--nonpercent-width-percent-height-viewbox.html (live test) (source)
- wide--auto-32px--nonpercent-width-percent-height.html (live test) (source)
- wide--auto-32px--omitted-width-nonpercent-height-viewbox.html (live test) (source)
- wide--auto-32px--omitted-width-nonpercent-height.html (live test) (source)
- wide--auto-32px--omitted-width-omitted-height-viewbox.html (live test) (source)
- wide--auto-32px--omitted-width-omitted-height.html (live test) (source)
- wide--auto-32px--omitted-width-percent-height-viewbox.html (live test) (source)
- wide--auto-32px--omitted-width-percent-height.html (live test) (source)
- wide--auto-32px--percent-width-nonpercent-height-viewbox.html (live test) (source)
- wide--auto-32px--percent-width-nonpercent-height.html (live test) (source)
- wide--auto-32px--percent-width-omitted-height-viewbox.html (live test) (source)
- wide--auto-32px--percent-width-omitted-height.html (live test) (source)
- wide--auto-32px--percent-width-percent-height-viewbox.html (live test) (source)
- wide--auto-32px--percent-width-percent-height.html (live test) (source)
- wide--contain--height.html (live test) (source)
- wide--contain--nonpercent-width-nonpercent-height-viewbox.html (live test) (source)
- wide--contain--nonpercent-width-nonpercent-height.html (live test) (source)
- wide--contain--nonpercent-width-omitted-height-viewbox.html (live test) (source)
- wide--contain--nonpercent-width-omitted-height.html (live test) (source)
- wide--contain--nonpercent-width-percent-height-viewbox.html (live test) (source)
- wide--contain--nonpercent-width-percent-height.html (live test) (source)
- wide--contain--omitted-width-nonpercent-height-viewbox.html (live test) (source)
- wide--contain--omitted-width-nonpercent-height.html (live test) (source)
- wide--contain--omitted-width-omitted-height-viewbox.html (live test) (source)
- wide--contain--omitted-width-omitted-height.html (live test) (source)
- wide--contain--omitted-width-percent-height-viewbox.html (live test) (source)
- wide--contain--omitted-width-percent-height.html (live test) (source)
- wide--contain--percent-width-nonpercent-height-viewbox.html (live test) (source)
- wide--contain--percent-width-nonpercent-height.html (live test) (source)
- wide--contain--percent-width-omitted-height-viewbox.html (live test) (source)
- wide--contain--percent-width-omitted-height.html (live test) (source)
- wide--contain--percent-width-percent-height-viewbox.html (live test) (source)
- wide--contain--percent-width-percent-height.html (live test) (source)
- wide--contain--width.html (live test) (source)
- wide--cover--height.html (live test) (source)
- wide--cover--nonpercent-width-nonpercent-height-viewbox.html (live test) (source)
- wide--cover--nonpercent-width-nonpercent-height.html (live test) (source)
- wide--cover--nonpercent-width-omitted-height-viewbox.html (live test) (source)
- wide--cover--nonpercent-width-omitted-height.html (live test) (source)
- wide--cover--nonpercent-width-percent-height-viewbox.html (live test) (source)
- wide--cover--nonpercent-width-percent-height.html (live test) (source)
- wide--cover--omitted-width-nonpercent-height-viewbox.html (live test) (source)
- wide--cover--omitted-width-nonpercent-height.html (live test) (source)
- wide--cover--omitted-width-omitted-height-viewbox.html (live test) (source)
- wide--cover--omitted-width-omitted-height.html (live test) (source)
- wide--cover--omitted-width-percent-height-viewbox.html (live test) (source)
- wide--cover--omitted-width-percent-height.html (live test) (source)
- wide--cover--percent-width-nonpercent-height-viewbox.html (live test) (source)
- wide--cover--percent-width-nonpercent-height.html (live test) (source)
- wide--cover--percent-width-omitted-height-viewbox.html (live test) (source)
- wide--cover--percent-width-omitted-height.html (live test) (source)
- wide--cover--percent-width-percent-height-viewbox.html (live test) (source)
- wide--cover--percent-width-percent-height.html (live test) (source)
- wide--cover--width.html (live test) (source)
- zero-height-ratio-5px-auto.html (live test) (source)
- zero-height-ratio-auto-5px.html (live test) (source)
- zero-height-ratio-auto-auto.html (live test) (source)
- zero-height-ratio-contain.html (live test) (source)
- zero-height-ratio-cover.html (live test) (source)
- zero-ratio-no-dimensions-5px-auto.html (live test) (source)
- zero-ratio-no-dimensions-auto-5px.html (live test) (source)
- zero-ratio-no-dimensions-auto-auto.html (live test) (source)
- zero-ratio-no-dimensions-contain.html (live test) (source)
- zero-ratio-no-dimensions-cover.html (live test) (source)
- zero-width-ratio-5px-auto.html (live test) (source)
- zero-width-ratio-auto-5px.html (live test) (source)
- zero-width-ratio-auto-auto.html (live test) (source)
- zero-width-ratio-contain.html (live test) (source)
- zero-width-ratio-cover.html (live test) (source)
- css3-background-size-001.html (live test) (source)
- css3-background-size-contain.html (live test) (source)
- css3-background-size.html (live test) (source)
- inheritance.sub.html (live test) (source)
- subpixel-repeat-no-repeat-mix.html (live test) (source)
- background-size-computed.html (live test) (source)
- background-size-invalid.html (live test) (source)
- background-size-valid.html (live test) (source)
This property specifies the size of each background image. The property’s value is given as a comma-separated list of <bg-size> values where
<bg-size> = [ <length-percentage [0,∞]> | auto ]{1,2} | cover | contain
Values have the following meanings:
- contain
- Scale the image, while preserving its natural aspect ratio (if any), to the largest size such that both its width and its height can fit inside the background positioning area.
- cover
- Scale the image, while preserving its natural aspect ratio (if any), to the smallest size such that both its width and its height can completely cover the background positioning area.
- [ <length-percentage [0,∞]> | auto ]{1,2}
-
The first value gives the width of the corresponding image,
the second value its height.
If only one value is given
the second is assumed to be auto.
A <percentage> is relative to the background positioning area.
An auto value for one dimension is resolved by using the image’s natural aspect ratio and the size of the other dimension, or failing that, using the image’s natural size, or failing that, treating it as 100%.
If both values are auto then the natural width and/or height of the image should be used, if any, the missing dimension (if any) behaving as auto as described above. If the image has neither natural size, its size is determined as for contain.
Negative values are invalid.
div {
background-image: url(plasma.png);
background-repeat: no-repeat;
background-size: 100% 100%;
background-origin: content-box }
The second example stretches the image so that exactly two copies fit horizontally. The aspect ratio is preserved:
p {
background-image: url(tubes.png);
background-size: 50% auto;
background-origin: border-box }
This example forces the background image to be 15 by 15 pixels:
p {
background-size: 15px 15px;
background-image: url(tile.png)}
This example uses the image’s natural size. Note that this is the only possible behavior in CSS level 1 and 2.
body {
background-size: auto; /* default */
background-image: url(flower.png) }
The following example rounds the height of the image to 33.3%, up from the specified value of 30%. At 30%, three images would fit entirely and a fourth only partially. After rounding, three images fit exactly. The width of the image is 20% of the background positioning area width and is not rounded.
p {
background-image: url(chain.png);
background-repeat: no-repeat round;
background-size: 20% 30% }
If background-repeat is round for one (or both) dimensions, there is a second step. The UA must scale the image in that dimension (or both dimensions) so that it fits a whole number of times in the background positioning area. In the case of the width (height is analogous):
If X ≠ 0 is the width of the image after step one and W is the width of the background positioning area, then the rounded width X' = W / round(W / X) where round() is a function that returns the nearest natural number (integer greater than zero).
If background-repeat is round for one dimension only and if background-size is auto for the other dimension, then there is a third step: that other dimension is scaled so that the original aspect ratio is restored.
div {
background-image: url(image1.png);
background-repeat: repeat; /* default */
background-size: auto } /* default */
In the following example, the background is shown with a width of 3em and its height is scaled proportionally to keep the original aspect ratio:
div {
background-image: url(image2.png);
background-repeat: repeat; /* default */
background-size: 3em } /* = '3em auto' */
In the following example, the background is shown with a width of approximately 3em: scaled so that it fits a whole number of times in the width of the background. The height is scaled proportionally to keep the original aspect ratio:
div {
background-image: url(image3.png);
background-repeat: round repeat;
background-size: 3em auto }
In the following example, the background image is shown with a width of 3em and a height that is either the height corresponding to that width at the original aspect ratio or slightly less:
div {
background-image: url(image4.png);
background-repeat: repeat round;
background-size: 3em auto }
In the following example, the background image is shown with a height of approximately 4em: scaled slightly so that it fits a whole number of times in the background height. The width is the approximately the width that corresponds to a 4em height at the original aspect ratio: scaled slightly so that it fits a whole number of times in the background width.
div {
background-image: url(image5.png);
background-repeat: round;
background-size: auto 4em }
If the background image’s width or height resolves to zero, this causes the image not to be displayed. (The effect is the same as if it had been a transparent image.)
See § 2.1 Layering Multiple Background Images for how background-size interacts with other comma-separated background properties to form each background image layer.
2.9.1. Serialization of background-size values
The specified value and computed value of the <bg-size> type always serialize as two values, even when the second value is auto, due to the shortest, most backwards-compatible serialization principle.
2.10. Backgrounds Shorthand: the background property
| Name: | background |
|---|---|
| Value: | <bg-layer>#? , <final-bg-layer> |
| Initial: | see individual properties |
| Applies to: | all elements |
| Inherited: | no |
| Percentages: | see individual properties |
| Computed value: | see individual properties |
| Canonical order: | per grammar |
| Animation type: | see individual properties |
Tests
- background-331.html (live test) (source)
- background-332.html (live test) (source)
- background-333.html (live test) (source)
- background-334.html (live test) (source)
- background-335.html (live test) (source)
- background-336.html (live test) (source)
- background-computed.html (live test) (source)
- background-invalid.html (live test) (source)
- background-shorthand-serialization.html (live test) (source)
- background-valid.html (live test) (source)
The background property is a shorthand property for setting most background properties at the same place in the style sheet. The number of comma-separated items defines the number of background image layers. Given a valid declaration, for each layer the shorthand first sets the corresponding value of each of background-image, background-position, background-size, background-repeat, background-origin, background-clip and background-attachment to that property’s initial value, then assigns any explicit values specified for this layer in the declaration. Finally background-color is set to the specified color, if any, else set to its initial value.
This property’s value is given as a comma-separated list of values where
<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'>
Note: A color is permitted in <final-bg-layer>, but not in <bg-layer>.
If one <visual-box> value is present then it sets both background-origin and background-clip to that value. If two values are present, then the first sets background-origin and the second background-clip.
body { background: red }
p { background: url("chess.png") 40% / 10em gray
round fixed border-box; }
The first rule is equivalent to:
body {
background-color: red;
background-position: 0% 0%;
background-size: auto;
background-repeat: repeat;
background-clip: border-box;
background-origin: padding-box;
background-attachment: scroll;
background-image: none }
The second is equivalent to:
p {
background-color: gray;
background-position: 40% 50%;
background-size: 10em auto;
background-repeat: round;
background-clip: border-box;
background-origin: border-box;
background-attachment: fixed;
background-image: url(chess.png) }
E { background: #CCC url("metal.jpg") top left / 100% auto no-repeat}
div { background: padding-box url(paper.jpg) white center }
div {
background-color: white;
background-image: url(paper.jpg);
background-repeat: repeat;
background-attachment: scroll;
background-position: center;
background-clip: padding-box;
background-origin: padding-box;
background-size: auto auto }
background: url(a.png) top left no-repeat,
url(b.png) center / 100% 100% no-repeat,
url(c.png) white;
is equivalent to
background-image: url(a.png), url(b.png), url(c.png); background-position: top left, center, top left; background-repeat: no-repeat, no-repeat, repeat; background-clip: border-box, border-box, border-box; background-origin: padding-box, padding-box, padding-box; background-size: auto auto, 100% 100%, auto auto; background-attachment: scroll, scroll, scroll; background-color: white;
2.11. Backgrounds of Special Elements
The document canvas is the infinite surface over which the document is rendered. [CSS2] Since no element corresponds to the canvas, in order to allow styling of the canvas CSS propagates the background of the root element (or, in the case of HTML, the <body> element) as described below. However, if the element whose background would be used for the canvas is display: none, then the canvas background is transparent.
If the canvas background is not opaque, the canvas surface below it shows through. The texture of the canvas surface is UA-dependent (but is typically an opaque white).
2.11.1. The Canvas Background and the Root Element
The background of the root element becomes the canvas background and its background painting area extends to cover the entire canvas. However, any images are sized and positioned relative to the root element’s box as if they were painted for that element alone. (In other words, the background positioning area is determined as for the root element.) The root element does not paint this background again, i.e., the used value of its background is transparent.
Tests
2.11.2. The Canvas Background and the HTML <body> Element
For documents whose root element is
an HTML HTML element
or an XHTML html element [HTML]:
if the computed value of background-image on the root element
is none and its background-color is transparent,
user agents must instead propagate
the computed values of the background properties
from that element’s first
HTML BODY or XHTML body
child element.
The used values
of that BODY element’s background properties
are their initial values,
and the propagated values are treated
as if they were specified on the root element.
It is recommended that authors of HTML documents specify the canvas background
using the BODY element
rather than the HTML element.
Note: Using containment disables
this special handling of the HTML body element.
See the CSS Containment 1 § 2 Strong Containment: the contain property for details.
<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.0//EN'
>
<html>
<head>
<title>Setting the canvas background</title>
<style type="text/css">
body { background: url("http://example.org/marble.png") }
</style>
</head>
<body>
<p>My background is marble.</p>
</body>
</html>
2.11.3. The ::first-line Pseudo-element‘s Background
The ::first-line pseudo-element is like an inline-level element for the purposes of the background (see section 5.12.1 of [CSS2]). That means, e.g., that in a left-justified first line, the background does not necessarily extend all the way to the right edge.
3. Borders
The border can either be a predefined style (solid line, double line, dotted line, pseudo-3D border, etc.) or it can be an image. In the former case, various properties define the style (border-style), color (border-color), and thickness (border-width) of the border.
3.1. Line Colors: the border-color properties
| Name: | border-top-color, border-right-color, border-bottom-color, border-left-color |
|---|---|
| Value: | <color> |
| Initial: | currentColor |
| Applies to: | all elements except ruby base containers and ruby annotation containers |
| Inherited: | no |
| Percentages: | N/A |
| Computed value: | computed color |
| Canonical order: | per grammar |
| Animation type: | by computed value |
| Logical property group: | border-color |
| Name: | border-color |
|---|---|
| Value: | <color>{1,4} |
| Initial: | see individual properties |
| Applies to: | all elements except ruby base containers and ruby annotation containers |
| Inherited: | no |
| Percentages: | N/A |
| Computed value: | see individual properties |
| Canonical order: | per grammar |
| Animation type: | see individual properties |
Tests
- border-color-interpolation.html (live test) (source)
- border-image-displayed-with-transparent-border-color.html (live test) (source)
- color-mix-currentcolor-border-repaint-parent.html (live test) (source)
- color-mix-currentcolor-border-repaint.html (live test) (source)
- currentcolor-border-repaint-parent.html (live test) (source)
- inheritance.sub.html (live test) (source)
- border-color-computed.html (live test) (source)
- border-color-invalid.html (live test) (source)
- border-color-shorthand.html (live test) (source)
- border-color-valid.html (live test) (source)
These properties set the foreground color of the border specified by the border-style properties.
The border-color property is a shorthand property for setting border-top-color, border-right-color, border-bottom-color, and border-left-color in a single declaration.
If there is only one component value, it applies to all sides. If there are two values, the top and bottom are set to the first value and the right and left are set to the second. If there are three values, the top is set to the first value, the left and right are set to the second, and the bottom is set to the third. If there are four values they apply to the top, right, bottom, and left, respectively.
3.2. Line Patterns: the border-style properties
| Name: | border-top-style, border-right-style, border-bottom-style, border-left-style |
|---|---|
| Value: | <line-style> |
| Initial: | none |
| Applies to: | all elements except ruby base containers and ruby annotation containers |
| Inherited: | no |
| Percentages: | N/A |
| Computed value: | specified keyword |
| Canonical order: | per grammar |
| Animation type: | discrete |
| Logical property group: | border-style |
| Name: | border-style |
|---|---|
| Value: | <line-style>{1,4} |
| Initial: | see individual properties |
| Applies to: | all elements except ruby base containers and ruby annotation containers |
| Inherited: | no |
| Percentages: | N/A |
| Computed value: | see individual properties |
| Canonical order: | per grammar |
| Animation type: | see individual properties |
Tests
These properties control whether a border appears, and if it does what style it’s drawn in (if it is not overridden by a border image).
The border-style property is a shorthand property for setting border-top-style, border-right-style, border-bottom-style, and border-left-style in a single declaration.
If there is only one component value, it applies to all sides. If there are two values, the top and bottom are set to the first value and the right and left are set to the second. If there are three values, the top is set to the first value, the left and right are set to the second, and the bottom is set to the third. If there are four values they apply to the top, right, bottom, and left, respectively.
The style is specified as a <line-style> keyword, where
<line-style> = none | hidden | dotted | dashed | solid | double | groove | ridge | inset | outset
Values have the following meanings:
- none
- No border. Color and width are ignored (i.e., the border has width 0). Note this means that the initial value of border-image-width will also resolve to zero.
- hidden
- Same as none, but has different behavior in the border conflict resolution rules for border-collapsed tables [CSS2].
- dotted
- A series of round dots.
- dashed
- A series of square-ended dashes.
- solid
- A single line segment.
- double
- Two parallel solid lines with some space between them. (The thickness of the lines is not specified, but the sum of the lines and the space must equal border-width.)
- groove
- Looks as if it were carved in the canvas. (This is typically achieved by creating a “shadow” from two colors that are slightly lighter and darker than the specified border-color.)
- ridge
- Looks as if it were coming out of the canvas.
- inset
- Looks as if the content on the inside of the border is sunken into the canvas. Treated as ridge in the collapsing border model. [CSS2]
- outset
- Looks as if the content on the inside of the border is raised out of the canvas. Treated as groove in the collapsing border model. [CSS2]
Borders are drawn in front of the element’s background, but behind the element’s content (in case it overlaps).
Note: Border colors close to black or white may need different color calculations than colors in between in order to create the required “3D” effect of groove, ridge, inset, or outset.
Note: There is no control over the spacing of the dots and dashes, nor over the length of the dashes. Implementations are encouraged to choose a spacing that makes the corners symmetrical.
Note: This specification does not define how borders of different styles should be joined in the corner. Also note that rounded corners may cause the corners and the contents to overlap, if the padding is less than the radius of the corner.
3.3. Line Thickness: the border-width properties
| Name: | border-top-width, border-right-width, border-bottom-width, border-left-width |
|---|---|
| Value: | <line-width> |
| Initial: | medium |
| Applies to: | all elements except ruby base containers and ruby annotation containers |
| Inherited: | no |
| Percentages: | N/A |
| Computed value: | absolute length, snapped as a border width |
| Canonical order: | per grammar |
| Animation type: | by computed value |
| Logical property group: | border-width |
| Name: | border-width |
|---|---|
| Value: | <line-width>{1,4} |
| Initial: | see individual properties |
| Applies to: | all elements except ruby base containers and ruby annotation containers |
| Inherited: | no |
| Percentages: | see individual properties |
| Computed value: | see individual properties |
| Canonical order: | per grammar |
| Animation type: | see individual properties |
Tests
- border-bottom-width-composition.html (live test) (source)
- border-image-width-composition.html (live test) (source)
- border-image-width-interpolation.html (live test) (source)
- border-left-width-composition.html (live test) (source)
- border-right-width-composition.html (live test) (source)
- border-top-width-composition.html (live test) (source)
- border-width-interpolation.html (live test) (source)
- border-bottom-width-medium.html (live test) (source)
- border-bottom-width-thick.html (live test) (source)
- border-bottom-width-thin.html (live test) (source)
- border-left-width-medium.html (live test) (source)
- border-left-width-thick.html (live test) (source)
- border-left-width-thin.html (live test) (source)
- border-right-width-medium.html (live test) (source)
- border-right-width-thick.html (live test) (source)
- border-right-width-thin.html (live test) (source)
- border-top-width-medium.html (live test) (source)
- border-top-width-thick.html (live test) (source)
- border-top-width-thin.html (live test) (source)
- border-width-cssom.html (live test) (source)
- border-width-pixel-snapping-001-a.html (live test) (source)
- border-width-pixel-snapping-001-b.html (live test) (source)
- border-width-small-values-001-a.html (live test) (source)
- border-width-small-values-001-b.html (live test) (source)
- border-width-small-values-001-c.html (live test) (source)
- border-width-small-values-001-d.html (live test) (source)
- border-width-small-values-001-e.html (live test) (source)
- inheritance.sub.html (live test) (source)
- border-width-computed.html (live test) (source)
- border-width-invalid.html (live test) (source)
- border-width-shorthand.html (live test) (source)
- border-width-valid.html (live test) (source)
These properties specify the thickness of the border, i.e. the border width. Where
<line-width> = <length [0,∞]> | thin | medium | thick
Negative values are invalid. The thin, medium, and thick keywords are equivalent to 1px, 3px, and 5px, respectively.
The border-width property is a shorthand property for setting border-top-width, border-right-width, border-bottom-width, and border-left-width in a single declaration.
If there is only one component value, it applies to all sides. If there are two values, the top and bottom are set to the first value and the right and left are set to the second. If there are three values, the top is set to the first value, the left and right are set to the second, and the bottom is set to the third. If there are four values they apply to the top, right, bottom, and left, respectively.
The resolved value for the border-width properties is the used value.
If the border-style corresponding to a given border-width is none or , then the used width is 0.
Note: Although the initial width is medium, the initial style is none; therefore the used initial width is 0.
3.4. Border Shorthand Properties
| Name: | border-top, border-right, border-bottom, border-left |
|---|---|
| Value: | <line-width> || <line-style> || <color> |
| Initial: | See individual properties |
| Applies to: | all elements except ruby base containers and ruby annotation containers |
| Inherited: | no |
| Percentages: | N/A |
| Computed value: | see individual properties |
| Canonical order: | per grammar |
| Animation type: | see individual properties |
These shorthand properties set the border-width, border-color, and border-style of the top, right, bottom, and left borders of a box. Omitted values are set to their initial values.
| Name: | border |
|---|---|
| Value: | <line-width> || <line-style> || <color> |
| Initial: | See individual properties |
| Applies to: | all elements except ruby base containers and ruby annotation containers |
| Inherited: | no |
| Percentages: | N/A |
| Computed value: | see individual properties |
| Canonical order: | per grammar |
| Animation type: | see individual properties |
Tests
The border property is a shorthand property for setting the same border-width, border-color, and border-style for all four borders of a box. Unlike the shorthand margin and padding properties, the border property cannot set different values on the four borders. To do so, one or more of the other border properties must be used.
The border shorthand also resets border-image to its initial value. It is therefore recommended that authors use the border shorthand, rather than other shorthands or the individual properties, to override any border settings earlier in the cascade. This will ensure that border-image has also been reset to allow the new styles to take effect.
Note: The CSS Working Group intends for the border shorthand to reset all border properties in future levels of CSS as well. For example, if a border-characters property is introduced in the future to allow glyphs as borders, it will also be reset by the border shorthand. By using the border shorthand to reset borders, authors can be guaranteed a “blank canvas” no matter what properties are introduced in the future.
p { border: solid red }
p {
border-top: solid red;
border-right: solid red;
border-bottom: solid red;
border-left: solid red;
border-image: none;
}
Since, to some extent, the properties have overlapping functionality, the order in which the rules are specified is important.
blockquote {
border-color: red;
border-left: double;
color: black
}
In the above example, the color of the left border is black, while the other borders are red. This is due to border-left setting the width, style, and color. Since the color value is not given by the border-left property, it will be taken from the color property. The fact that the color property is set after the border-left property is not relevant.
4. Rounded Corners
4.1. Curve Radii: the border-radius properties
| Name: | border-top-left-radius, border-top-right-radius, border-bottom-right-radius, border-bottom-left-radius |
|---|---|
| Value: | <length-percentage [0,∞]>{1,2} |
| Initial: | 0 |
| Applies to: | all elements (but see prose) |
| Inherited: | no |
| Percentages: | Refer to corresponding dimension of the border box. |
| Computed value: | pair of computed <length-percentage> values |
| Canonical order: | per grammar |
| Animation type: | by computed value |
| Logical property group: | border-radius |
| Name: | border-radius |
|---|---|
| Value: | <length-percentage [0,∞]>{1,4} [ / <length-percentage [0,∞]>{1,4} ]? |
| Initial: | see individual properties |
| Applies to: | all elements (but see prose) |
| Inherited: | no |
| Percentages: | Refer to corresponding dimension of the border box. |
| Computed value: | see individual properties |
| Canonical order: | per grammar |
| Animation type: | see individual properties |
Tests
- border-bottom-left-radius-composition.html (live test) (source)
- border-bottom-right-radius-composition.html (live test) (source)
- border-radius-interpolation.html (live test) (source)
- border-top-left-radius-composition.html (live test) (source)
- border-top-right-radius-composition.html (live test) (source)
- border-bottom-left-radius-001.xht (live test) (source)
- border-bottom-left-radius-004.xht (live test) (source)
- border-bottom-left-radius-005.xht (live test) (source)
- border-bottom-left-radius-010.xht (live test) (source)
- border-bottom-left-radius-011.xht (live test) (source)
- border-bottom-left-radius-014.xht (live test) (source)
- border-bottom-right-radius-001.xht (live test) (source)
- border-bottom-right-radius-004.xht (live test) (source)
- border-bottom-right-radius-005.xht (live test) (source)
- border-bottom-right-radius-010.xht (live test) (source)
- border-bottom-right-radius-011.xht (live test) (source)
- border-bottom-right-radius-014.xht (live test) (source)
- border-radius-001.xht (live test) (source)
- border-radius-002.xht (live test) (source)
- border-radius-003.xht (live test) (source)
- border-radius-004.xht (live test) (source)
- border-radius-005.xht (live test) (source)
- border-radius-006.xht (live test) (source)
- border-radius-007.xht (live test) (source)
- border-radius-008.xht (live test) (source)
- border-radius-009.xht (live test) (source)
- border-radius-010.xht (live test) (source)
- border-radius-011.xht (live test) (source)
- border-radius-012.html (live test) (source)
- border-radius-013.html (live test) (source)
- border-radius-clip-001.html (live test) (source)
- border-radius-clip-002.htm (live test) (source)
- border-radius-clipping-002.html (live test) (source)
- border-radius-clipping-with-transform-001.html (live test) (source)
- border-radius-css-text.html (live test) (source)
- border-radius-dynamic-from-no-radius.html (live test) (source)
- border-radius-horizontal-value-is-zero.html (live test) (source)
- border-radius-shorthand-002.html (live test) (source)
- border-top-left-radius-001.xht (live test) (source)
- border-top-left-radius-004.xht (live test) (source)
- border-top-left-radius-005.xht (live test) (source)
- border-top-left-radius-010.xht (live test) (source)
- border-top-left-radius-011.xht (live test) (source)
- border-top-left-radius-014.xht (live test) (source)
- border-top-right-radius-001.xht (live test) (source)
- border-top-right-radius-004.xht (live test) (source)
- border-top-right-radius-005.xht (live test) (source)
- border-top-right-radius-010.xht (live test) (source)
- border-top-right-radius-011.xht (live test) (source)
- border-top-right-radius-014.xht (live test) (source)
- box-shadow-border-radius-001.html (live test) (source)
- css-border-radius-001.html (live test) (source)
- inheritance.sub.html (live test) (source)
- inner-border-non-renderable.html (live test) (source)
- border-radius-computed.html (live test) (source)
- border-radius-invalid.html (live test) (source)
- border-radius-valid.html (live test) (source)
- webkit-border-radius-valid.html (live test) (source)
The two <length-percentage> values of the border-*-radius properties define the radii of a quar