CSS Backgrounds Module Level 4

Editor’s Draft,

More details about this document
This version:
https://drafts.csswg.org/css-backgrounds-4/
Issue Tracking:
CSSWG Issues Repository
Inline In Spec
Editors:
(W3C)
Elika J. Etemad / fantasai (Apple)
Lea Verou (Invited Expert)
(Invited Expert)
Suggest an Edit for this Spec:
GitHub Editor
Not Ready For Implementation

This spec is not yet ready for implementation. It exists in this repository to record the ideas and promote discussion.

Before attempting to implement this spec, please contact the CSSWG at [email protected].


Abstract

This module contains the features of CSS relating to the backgrounds of boxes on the page.

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 03 November 2023 W3C Process Document.

1. Introduction

This module is currently maintained as a diff against the parts related to backgrounds of CSS Backgrounds and Borders Module Level 3 [CSS3BG]. We will fold in the text once it’s all formatted up and in CR again, as this will reduce the effort of keeping them in sync (source diffs will be accurate in reflecting the differences).

2. Backgrounds

2.1. Tiling Images: the background-repeat-x, background-repeat-y, background-repeat-block, and background-repeat-inline properties

Name: background-repeat-x, background-repeat-y, background-repeat-block, background-repeat-inline
Value: <repetition>#
Initial: repeat
Applies to: all elements
Inherited: no
Percentages: N/A
Computed value: as specified
Canonical order: per grammar
Animation type: discrete
<repetition> = repeat | space | round | no-repeat

These properties specify whether and how background images are tiled along one axis after they have been sized and positioned.

repeat
The image is repeated in the given direction as often as needed to cover the background painting area.
space
The image is repeated in the given direction as often as will fit within the background positioning area without being clipped and then the repeated 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 direction, in which case only one image is placed and background-position determines its position in this direction.
round
The image is repeated in the given direction 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 the given direction.

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

Example(s):

body {
  background: white url("pendant.png");
  background-repeat-y: repeat;
  background-position: center;
}

A centered background image, with
			copies repeated up and down the border, padding and content
			areas.

The effect of repeat: 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.

See the section “Layering multiple background images” for how background-repeat-x, background-repeat-y, background-repeat-block, and background-repeat-inline interact with other comma-separated background properties to form each background image layer.

2.2. Tiling Images Shorthand: 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

This shorthand sets the values for the background-repeat-x and background-repeat-y longhand properties. Where

<repeat-style> = repeat-x | repeat-y | <repetition>{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 is for the horizontal direction, the second for the vertical one.

Example(s):

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 the section “Layering multiple background images” for how background-repeat interacts with other comma-separated background properties to form each background image layer.

Should a 'background-repeat: extend' be added?

2.3. Background Positioning: the background-position shorthand 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: a list, each item a pair of offsets (horizontal and vertical) from the top left origin, each offset given as a computed <length-percentage> value
Canonical order: per grammar
Animation type: repeatable list

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

This property is a shorthand property that sets background-position-x, background-position-y, background-position-block, and background-position-inine in a single declaration.

Its value is given as a comma-separated list of <bg-position> values, which are interpreted as <position> values with the resized background image as the alignment subject and the background positioning area as the alignment container.

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

The omitted <length-percentage> in the background-position-specific <position-three> syntax variant defaults to 0%.

Specify how the longhand properties are set. [Issue #9690]

2.3.1. Background Positioning Longhands: the background-position-x, background-position-y, background-position-inline, and background-position-block properties

This section is still being worked out. The tricky thing is making all the start/end keywords work sanely.

Name: background-position-x
Value: [ center | [ [ left | right | x-start | x-end ]? <length-percentage>? ]! ]#
Initial: 0%
Applies to: all elements
Inherited: no
Percentages: refer to width of background positioning area minus width of background image
Computed value: A list, each item consisting of: an offset given as a computed <length-percentage> value, plus an origin keyword
Canonical order: per grammar
Animation type: repeatable list
Logical property group: background-position

This property specifies the background position’s horizontal component. An omitted origin keyword is assumed to be left.

Name: background-position-y
Value: [ center | [ [ top | bottom | y-start | y-end ]? <length-percentage>? ]! ]#
Initial: 0%
Applies to: all elements
Inherited: no
Percentages: refer to height of background positioning area minus height of background image
Computed value: A list, each item consisting of: an offset given as a computed <length-percentage> value, plus an origin keyword
Canonical order: per grammar
Animation type: repeatable list
Logical property group: background-position

This property specifies the background position’s vertical component. An omitted origin keyword is assumed to be top.

Name: background-position-inline
Value: [ center | [ [ start | end ]? <length-percentage>? ]! ]#
Initial: 0%
Applies to: all elements
Inherited: no
Percentages: refer to inline-size of background positioning area minus inline-size of background image
Computed value: A list, each item consisting of: an offset given as a computed <length-percentage> value, plus an origin keyword
Canonical order: per grammar
Animation type: repeatable list
Logical property group: background-position

This property specifies the background position’s inline-axis component. An omitted origin keyword is assumed to be start.

Name: background-position-block
Value: [ center | [ [ start | end ]? <length-percentage>? ]! ]#
Initial: 0%
Applies to: all elements
Inherited: no
Percentages: refer to size of background positioning area minus size of background image
Computed value: A list, each item consisting of: an offset given as a computed <length-percentage> value, plus an origin keyword
Canonical order: per grammar
Animation type: repeatable list
Logical property group: background-position

This property specifies the background position’s block-axis component. An omitted origin keyword is assumed to be start.

2.4. Painting Area: the background-clip property

Name: background-clip
Value: <bg-clip>#
Initial: border-box
Applies to: all elements
Inherited: no
Percentages: n/a
Computed value: as specified
Canonical order: per grammar
Animation type: repeatable list

Determines the background painting area, which determines the area within which the background is painted. The syntax of the property is given with

<bg-clip> = <visual-box> | border-area| text

Or should this be defining the -webkit-background-clip property, saying that all the values are identical, with this additional text value?

<visual-box>
The background is painted within (clipped to) the specified box of the element.
text
The background is painted within (clipped to) the intersection of the border box and the geometry of the text in the element and its in-flow and floated descendants.
border-area
The background is clipped to the area painted by the border, taking border-width and border-style into account but ignoring any transparency introduced by border-color.

2.5. Background Image Layers: the background-tbd shorthand property

Name: background-tbd
Value: <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

The background-tbd property is a shorthand property that sets all the same properties as the background shorthand except for background-color, allowing authors to easily declare and position background images while letting background-color cascade through independently.

The name of this property is discussed in issue 9083.

This example sets two background layers later in the cascade. By using background-tbd, the previously set background-color won’t be overridden.
p {
  background-color: green;
}

p {
  background-tbd:
    url(a.png) top left,
    url(b.png) top left no-repeat;
}
This example tries to set the background color in addition to the background image. But for that to work, background needs to be used instead of background-tbd. So the background-tbd declaration will be dropped.
p {
  background: url(pass.png) green;   /* valid */
  background-tbd: url(fail.png) red; /* invalid */
}

3. Changes

3.1. Additions since [CSS3BG]

4. Acknowledgments

In addition to the many contributors to the [CSS1], [CSS21], and [CSS3BG] predecessors to this module, the editors would like to thank Tab Atkins, and Håkon Wium Lie for their suggestions and feedback specifically for this Level 4.

Privacy Considerations

No new privacy considerations have been reported on this specification.

Security Considerations

No new security considerations have been reported on this specification.

Conformance

Document conventions

Conformance requirements are expressed with a combination of descriptive assertions and RFC 2119 terminology. The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in the normative parts of this document are to be interpreted as described in RFC 2119. However, for readability, these words do not appear in all uppercase letters in this specification.

All of the text of this specification is normative except sections explicitly marked as non-normative, examples, and notes. [RFC2119]

Examples in this specification are introduced with the words “for example” or are set apart from the normative text with class="example", like this:

This is an example of an informative example.

Informative notes begin with the word “Note” and are set apart from the normative text with class="note", like this:

Note, this is an informative note.

Advisements are normative sections styled to evoke special attention and are set apart from other normative text with <strong class="advisement">, like this: UAs MUST provide an accessible alternative.

Tests

Tests relating to the content of this specification may be documented in “Tests” blocks like this one. Any such block is non-normative.


Conformance classes

Conformance to this specification is defined for three conformance classes:

style sheet
A CSS style sheet.
renderer
A UA that interprets the semantics of a style sheet and renders documents that use them.
authoring tool
A UA that writes a style sheet.

A style sheet is conformant to this specification if all of its statements that use syntax defined in this module are valid according to the generic CSS grammar and the individual grammars of each feature defined in this module.

A renderer is conformant to this specification if, in addition to interpreting the style sheet as defined by the appropriate specifications, it supports all the features defined by this specification by parsing them correctly and rendering the document accordingly. However, the inability of a UA to correctly render a document due to limitations of the device does not make the UA non-conformant. (For example, a UA is not required to render color on a monochrome monitor.)

An authoring tool is conformant to this specification if it writes style sheets that are syntactically correct according to the generic CSS grammar and the individual grammars of each feature in this module, and meet all other conformance requirements of style sheets as described in this module.

Partial implementations

So that authors can exploit the forward-compatible parsing rules to assign fallback values, CSS renderers must treat as invalid (and ignore as appropriate) any at-rules, properties, property values, keywords, and other syntactic constructs for which they have no usable level of support. In particular, user agents must not selectively ignore unsupported component values and honor supported values in a single multi-value property declaration: if any value is considered invalid (as unsupported values must be), CSS requires that the entire declaration be ignored.

Implementations of Unstable and Proprietary Features

To avoid clashes with future stable CSS features, the CSSWG recommends following best practices for the implementation of unstable features and proprietary extensions to CSS.

Non-experimental implementations

Once a specification reaches the Candidate Recommendation stage, non-experimental implementations are possible, and implementors should release an unprefixed implementation of any CR-level feature they can demonstrate to be correctly implemented according to spec.

To establish and maintain the interoperability of CSS across implementations, the CSS Working Group requests that non-experimental CSS renderers submit an implementation report (and, if necessary, the testcases used for that implementation report) to the W3C before releasing an unprefixed implementation of any CSS features. Testcases submitted to W3C are subject to review and correction by the CSS Working Group.

Further information on submitting testcases and implementation reports can be found from on the CSS Working Group’s website at http://www.w3.org/Style/CSS/Test/. Questions should be directed to the [email protected] mailing list.

Index

Terms defined by this specification

Terms defined by reference

References

Normative References

[CSS-ALIGN-3]
Elika Etemad; Tab Atkins Jr.. CSS Box Alignment Module Level 3. URL: https://drafts.csswg.org/css-align/
[CSS-BORDERS-4]
CSS Borders and Box Decorations Module Level 4. Editor's Draft. URL: https://drafts.csswg.org/css-borders-4/
[CSS-BOX-4]
Elika Etemad. CSS Box Model Module Level 4. URL: https://drafts.csswg.org/css-box-4/
[CSS-CASCADE-5]
Elika Etemad; Miriam Suzanne; Tab Atkins Jr.. CSS Cascading and Inheritance Level 5. URL: https://drafts.csswg.org/css-cascade-5/
[CSS-VALUES-4]
Tab Atkins Jr.; Elika Etemad. CSS Values and Units Module Level 4. URL: https://drafts.csswg.org/css-values-4/
[CSS-VALUES-5]
Elika Etemad; Tab Atkins Jr.; Miriam Suzanne. CSS Values and Units Module Level 5. URL: https://drafts.csswg.org/css-values-5/
[CSS3BG]
Elika Etemad; Brad Kemper. CSS Backgrounds and Borders Module Level 3. URL: https://drafts.csswg.org/css-backgrounds/
[RFC2119]
S. Bradner. Key words for use in RFCs to Indicate Requirement Levels. March 1997. Best Current Practice. URL: https://datatracker.ietf.org/doc/html/rfc2119

Informative References

[CSS1]
Håkon Wium Lie; Bert Bos. Cascading Style Sheets, level 1. 13 September 2018. REC. URL: https://www.w3.org/TR/CSS1/
[CSS21]
Bert Bos; et al. Cascading Style Sheets Level 2 Revision 1 (CSS 2.1) Specification. URL: https://drafts.csswg.org/css2/

Property Index

Name Value Initial Applies to Inh. %ages Anim­ation type Canonical order Com­puted value Logical property group
background-clip <bg-clip># border-box all elements no n/a repeatable list per grammar as specified
background-position <bg-position># 0% 0% all elements no refer to size of background positioning area minus size of background image; see text repeatable list per grammar a list, each item a pair of offsets (horizontal and vertical) from the top left origin, each offset given as a computed <length-percentage> value
background-position-block [ center | [ [ start | end ]? <length-percentage>? ]! ]# 0% all elements no refer to size of background positioning area minus size of background image repeatable list per grammar A list, each item consisting of: an offset given as a computed <length-percentage> value, plus an origin keyword background-position
background-position-inline [ center | [ [ start | end ]? <length-percentage>? ]! ]# 0% all elements no refer to inline-size of background positioning area minus inline-size of background image repeatable list per grammar A list, each item consisting of: an offset given as a computed <length-percentage> value, plus an origin keyword background-position
background-position-x [ center | [ [ left | right | x-start | x-end ]? <length-percentage>? ]! ]# 0% all elements no refer to width of background positioning area minus width of background image repeatable list per grammar A list, each item consisting of: an offset given as a computed <length-percentage> value, plus an origin keyword background-position
background-position-y [ center | [ [ top | bottom | y-start | y-end ]? <length-percentage>? ]! ]# 0% all elements no refer to height of background positioning area minus height of background image repeatable list per grammar A list, each item consisting of: an offset given as a computed <length-percentage> value, plus an origin keyword background-position
background-repeat <repeat-style># repeat all elements no N/A discrete per grammar list, each item a pair of keywords, one per dimension
background-repeat-block <repetition># repeat all elements no N/A discrete per grammar as specified
background-repeat-inline <repetition># repeat all elements no N/A discrete per grammar as specified
background-repeat-x <repetition># repeat all elements no N/A discrete per grammar as specified
background-repeat-y <repetition># repeat all elements no N/A discrete per grammar as specified
background-tbd <bg-layer># see individual properties all elements no see individual properties see individual properties per grammar see individual properties

Issues Index

This module is currently maintained as a diff against the parts related to backgrounds of CSS Backgrounds and Borders Module Level 3 [CSS3BG]. We will fold in the text once it’s all formatted up and in CR again, as this will reduce the effort of keeping them in sync (source diffs will be accurate in reflecting the differences).
Should a 'background-repeat: extend' be added?
Specify how the longhand properties are set. [Issue #9690]
This section is still being worked out. The tricky thing is making all the start/end keywords work sanely.
Or should this be defining the -webkit-background-clip property, saying that all the values are identical, with this additional text value?
The name of this property is discussed in issue 9083.
MDN

background-position-x

In all current engines.

Firefox49+Safari1+Chrome1+
Opera?Edge79+
Edge (Legacy)12+IE6+
Firefox for Android?iOS Safari?Chrome for Android?Android WebView37+Samsung Internet?Opera Mobile18+

background-position-y

In all current engines.

Firefox49+Safari1+Chrome1+
Opera?Edge79+
Edge (Legacy)12+IE6+
Firefox for Android?iOS Safari?Chrome for Android?Android WebView37+Samsung Internet?Opera Mobile?