[go: up one dir, main page]

W3C

CSS Cascading and Inheritance Level 3

W3C Working Draft 3 January 2013

This version:
http://www.w3.org/TR/2013/WD-css3-cascade-20130103/
Latest version:
http://www.w3.org/TR/css3-cascade/
Editor's draft:
http://dev.w3.org/csswg/css3-cascade/
Previous version:
http://www.w3.org/TR/2005/WD-css3-cascade-20051215
Editors:
(Opera Software)
Elika J. Etemad (Mozilla)
Tab Atkins Jr. (Google)

Abstract

This CSS module describes how to collate style rules and assign values to all properties on all elements. By way of cascading and inheritance, values are propagated for all properties on all elements. CSS is a language for describing the rendering of structured documents (such as HTML and XML) on screen, on paper, in speech, etc.

Status of this document

This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.

Publication as a Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.

The (archived) public mailing list www-style@w3.org (see instructions) is preferred for discussion of this specification. When sending e-mail, please put the text “css3-cascade” in the subject, preferably like this: “[css3-cascade] …summary of comment…

This document was produced by the CSS Working Group (part of the Style Activity).

This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

Table of contents


1. Introduction

One of the fundamental design principles of CSS is cascading, which allows several style sheets to influence the presentation of a document. When different declarations try to set a value for the same element/property combination, the conflicts must somehow be resolved.

The opposite problem arises when no declarations try to set a the value for an element/property combination. In this case, a value must be found by way of inheritance or by looking at the property's initial value.

The rules for finding the specified value for all properties on all elements in the document are described in this specification. The rules for finding the specified values in the page context and its margin boxes are described in [CSS3PAGE].

2. Importing style sheets: the ‘@import’ rule

The @import rule allows users to import style rules from other style sheets. Any ‘@import’ rules must follow all ‘@charset’ rules and precede all other at-rules and rule sets in a style sheet. The ‘@import’ keyword must be followed by the URI of the style sheet to include. A string is also allowed; it will be interpreted as if it had url(…) around it.

The following lines are equivalent in meaning and illustrate both ‘@import’ syntaxes (one with ‘url()’ and one with a bare string):

@import "mystyle.css";
@import url("mystyle.css");

So that user agents can avoid retrieving resources for unsupported media types, authors may specify media-dependent ‘@import’ rules. Such media-dependent imports include a comma-separated list of “media queries” after the URI. In the absence of any media query, the import is unconditional. (Specifying ‘all’ for the medium has the same effect.) The UA must import the style sheet if (and only if) the media condition evaluates to true.

The following rules illustrate how ‘@import’ rules can be made media-dependent:

@import url("fineprint.css") print;
@import url("bluish.css") projection, tv;
@import url("narrow.css") handheld and (max-width: 400px);

The evaluation and full syntax of the expressions after the URL is defined by the Media Queries specification [MEDIAQ].

When the same style sheet is imported or linked to a document in multiple places, user agents must process (or act as though they do) each link as though the link were to a separate style sheet.

2.1. Content-Type of imported stylesheets

The processing of imported stylesheets depends on the actual type of the linked resource. If the resource does not have Content-Type metadata, or the host document is in quirks mode and has the same origin as the imported stylesheet, the type of the linked resource is text/css. Otherwise, the type is determined from its Content-Type metadata.

If the linked resource's type is text/css, it must be interpreted as a CSS stylesheet. Otherwise, it must be interpreted as a network error.

3. Shorthand Properties

Some properties are shorthand properties, meaning that they allow authors to specify the values of several properties with a single property. A shorthand property sets all of its longhand sub-properties, exactly as if expanded in place.

For example, the CSS 2.1 ‘font’ property is a shorthand property for setting ‘font-style’, ‘font-variant’, ‘font-weight’, ‘font-size’, ‘line-height’, and ‘font-family’ all at once. The multiple declarations of this example:

h1 { 
	font-weight: bold; 
	font-size: 12pt;
	line-height: 14pt; 
	font-family: Helvetica; 
	font-variant: normal;
	font-style: normal;
}

can therefore be rewritten as

h1 { font: bold 12pt/14pt Helvetica }

When values are omitted from a shorthand form, unless otherwise defined, each "missing" sub-property is assigned its initial value.

This means that a shorthand property declaration always sets all of its subproperties, even those that are not explicitly set. Carelessly used, this might result in inadvertently resetting some sub-properties. Carefully used, a shorthand can guarantee a "blank slate" by resetting sub-properties inadvertently cascaded from other sources. For example, writing ‘background: green’ rather than ‘background-color: green’ ensures that the background color overrides any earlier declarations that might have set the background to an image.

In some cases, a shorthand might have different syntax or special keywords that don't directly correspond to values of its sub-properties. (In such cases, the shorthand will explicitly define the expansion of its values.)

If a shorthand is specified as one of the CSS-wide keywords, it sets all of its sub-properties to that keyword. (Note that these keywords cannot be combined with other values in a single declaration, not even in a shorthand.)

Declaring a shorthand property to be ‘!important’ is equivalent to declaring all of its sub-properties to be "!important".

3.1. Resetting All Properties: the ‘all’ property

Name: all
Value: initial | inherit | default
Initial: See individual properties
Applies to: See individual properties
Inherited: See individual properties
Percentages: See individual properties
Media: See individual properties
Computed value: See individual properties
Animatable: See individual properties

The ‘all’ property is a shorthand that resets all CSS properties. It only accepts the CSS-wide keywords.

For example, if an author specifies ‘all: default’ on an element it will block all inheritance and reset all properties, as if no rules appeared in the author level of the cascade.

This can be useful for the root element of a "widget" included in a page, which does not wish to inherit the styles of the outer page.

4. Cascading and Inheritance

The cascading and inheritance process takes a set of declarations as input, and outputs a specified value for each property on each element.

4.1. Filtering Declarations

In order to find the specified values, implementations must first identify all declarations that apply to each element. A declaration applies to an element if:

The declarations that apply form, for each property on each element, a list of potential values. The next section, the cascade, prioritizes these lists.

4.2. Cascading Declarations

The cascade algorithm takes an unordered list of declarations for a given property on an element and sorts it by precedence as determined below.

User agents must sort declarations according to the following criteria, in descending order of priority:

Origin
The origin of a declaration is based on where it comes from and whether or not it is declared ‘!important’ (see below). The precedence of the various origins is, in descending order:
  1. Transition declarations [CSS3-TRANSITIONS]
  2. Important user agent declarations
  3. Important user declarations
  4. Important override declarations [DOM-LEVEL-2-STYLE]
  5. Important author declarations
  6. Animation declarations [CSS3-ANIMATIONS]
  7. Normal override declarations [DOM-LEVEL-2-STYLE]
  8. Normal author declarations
  9. Normal user declarations
  10. Normal user agent declarations

Declarations from origins earlier in this list win over declarations from later origins.

Scope
A declaration can be scoped to a subtree of the document so that it only affects its scoping element and that element's descendants. For example, [HTML5] defines scoped <style> elements, whose style sheets are scoped to the element's parent.

If the scoping elements of two declarations have an ancestor/descendant relationship, then for normal rules the declaration whose scoping element is the descendant wins, and for important rules the declaration whose scoping element is the ancestor wins.

In this definition, an inner scope wins over an outer one for normal rules, but for important rules the outer one gets the last word, similar to how the UA/user/author levels relate to each other. This allows outer scopes to set defaults that are then overridden, but to override inner scopes in !important cases. An alternative definition would be to repeat the same ordering for important rules, similar to how override rules work. This means !important simply jumps everything up over Animations.

For the purpose of this step, all unscoped declarations are considered to be scoped to the root element. Normal declarations from style attributes are considered to be scoped to the element with the attribute, whereas important declarations from style attributes are considered to be scoped to the root element. [CSSSTYLEATTR]

This odd handling of !important style attribute declarations is to match the behavior defined in CSS Levels 1 and 2, where style attributes simply have higher specificity than any other author rules. [CSS21]

Specificity
The Selectors module [SELECT] describes how to compute the specificity of a selector. For the purpose of this step, a ruleset without a selector (such as the contents of a style attribute) is considered to have a specificity higher than any selector. The declaration belonging to a ruleset whose selector has the highest specificity wins.
Order of appearance.
Rules from imported style sheets are treated as if their stylesheets were substituted in place of the ‘@import’ rule. Stylesheets independently linked by the originating document are treated as if they were concatenated in linking order, as determined by the host document language. Style attributes are treated as if their rulesets were concatenated in document order, after any stylesheets. The last declaration wins.

The output of the cascade is a (potentially empty) sorted list of declarations for each property on each element.

4.2.1. Cascading Origins

Each style rule has an origin, which determines where it enters the cascade. CSS defines three core origins:

Author
The author specifies style sheets for a source document according to the conventions of the document language. For instance, in HTML, style sheets may be included in the document or linked externally.
User
The user may be able to specify style information for a particular document. For example, the user may specify a file that contains a style sheet or the user agent may provide an interface that generates a user style sheet (or behaves as if it did).
User agent
Conforming user agents must apply a default style sheet (or behave as if they did). A user agent's default style sheet should present the elements of the document language in ways that satisfy general presentation expectations for the document language (e.g., for visual browsers, the EM element in HTML is presented using an italic font). See e.g. the HTML user agent style sheet.

Extensions to CSS define the following additional origins:

Override
DOM Level 2 Style [DOM-LEVEL-2-STYLE] defines an interface for "override" styles, which enter the cascade at a higher level than other author rules.
Animation
CSS Animations [CSS3-ANIMATIONS] generate "virtual" rules representing their effects when running.
Transition
Like CSS Animations, CSS Transitions [CSS3-TRANSITIONS] generate "virtual" rules representing their effects when running.

4.2.2. !important’ rules

CSS attempts to create a balance of power between author and user style sheets. By default, rules in an author's style sheet override those in a user's style sheet.

However, for balance, an "!important" declaration takes precedence over a normal declaration. Author and user style sheets may contain "!important" declarations, with user "!important" rules overriding author "!important" rules. This CSS feature improves accessibility of documents by giving users with special requirements (large fonts, color combinations, etc.) control over presentation.

A declaration is "!important" if the last two tokens in its value are the delimiter token ‘!’ followed by the identifier token ‘important’. Declaring a shorthand property (e.g., ‘background’) to be "!important" is equivalent to declaring all of its sub-properties to be "!important".

User agent stylesheets may also contain "!important" rules. These override all author and user rules.

The first rule in the user's style sheet in the following example contains an "!important" declaration, which overrides the corresponding declaration in the author's style sheet. The second declaration will also win due to being marked "!important". However, the third rule in the user's style sheet is not "!important" and will therefore lose to the second rule in the author's style sheet (which happens to set style on a shorthand property). Also, the third author rule will lose to the second author rule since the second rule is "!important". This shows that "!important" declarations have a function also within author style sheets.

/* From the user's style sheet */
p { text-indent: 1em !important }
p { font-style: italic !important }
p { font-size: 18pt }

/* From the author's style sheet */
p { text-indent: 1.5em !important }
p { font: normal 12pt sans-serif !important }
p { font-size: 24pt }

4.2.3. Precedence of Non-CSS Presentational Hints

The UA may choose to honor presentational hints in a source document's markup, for example the bgcolor attribute or <s> element in [[HTML]]. All document language-based styling must be translated to corresponding CSS rules and either enter the cascade at the user agent level or be treated as author level rules with a specificity of zero placed at the start of the author style sheet. A document language may define whether a presentational hint enters at the UA or author level of the cascade; if so, the UA must behave accordingly. For example, [SVG11] maps its presentation attributes into the author level.

Presentational hints entering the cascade at the UA level can be overridden by user styles. Presentational hints entering the cascade at the author level can be overridden by the author's use of the ‘default’ keyword. Host languages should choose the appropriate level for presentational hints with these considerations in mind.

4.2.4. The Cascaded Value

The cascaded value represents the result of the cascade. It is the value of the highest declaration in the output of the cascade (after any rollback steps have been performed, see below). If the output of the cascade is an empty list, the cascaded value is the ‘inherit’ keyword if the property is an inherited property (as defined by its property definition table) or the ‘initial’ keyword otherwise.

4.3. Inheritance and Defaulting

In most cases, the specified value is the cascaded value. However, the ‘initial’, ‘inherit’, and ‘default’ values are handled specially when they are the cascaded value of a property.

4.3.1. Initial value: the ‘initial’ keyword

Each property has an initial value, defined in the property's definition table. If the cascaded value is the initial keyword, the property's initial value becomes its specified value.

4.3.2. Inheritance: the ‘inherit’ keyword

Some properties are inherited properties, as defined in their property definition table. This means that, unless the cascade results in a value, the value will be determined by inheritance.

Inheritance propagates property values from parent elements to their children. The inherited value of a property on an element is the computed value of the property on the element's parent element. For the root element, which has no parent element, the inherited value is the initial value of the property.

Pseudo-elements inherit according to a fictional tag sequence described for each pseudo-element [SELECT].

Note that inheritance follows the document tree and is not intercepted by anonymous boxes, or otherwise affected by manipulations of the box tree.

If the cascaded value of a property is the inherit keyword, the inherited value becomes the property's specified and computed values.

4.3.3. Rolling back the cascade: the ‘default’ keyword

CSS Cascading and Inheritance Level 3 introduces the default keyword as a CSS-wide keyword accepted by all properties.

If the cascaded value is the ‘default’ keyword, then the origin level to which it belongs is discarded from the cascade, resulting in a new cascaded value. For this purpose, all author-originating styles (author and override, both normal and important) are treated as belonging to the same origin level. This continues until the cascaded value is not ‘default’ or until the output of the cascade is empty.

This definition might need tweaking to sensibly handle animations. See dbaron's message.

5. Stages of Value Computation

Once a user agent has parsed a document and constructed a document tree, it must assign, to every element in the tree, and correspondingly to every box in the formatting structure, a value to every property that applies to the target media type.

The final value of a CSS3 property for a given element or box is the result of a four-step calculation:

5.1. Finding the specified value

The specified value is the output of the cascading and inheritance process.

The cascading and inheritance process guarantees that a specified value exists for every property on every element.

5.2. Finding the computed value

A specified value can be either absolute (i.e., not relative to another value, as in ‘red’ or ‘2mm’) or relative (i.e., relative to another value, as in ‘auto’, ‘2em’).

For absolute values, no extra processing is needed to find the computed value. For relative values, on the other hand, computation is necessary to find the computed value: percentages must be multiplied by a reference value (each property defines which value that is), values with relative units (em, ex, vh, vw) must be made absolute by multiplying with the appropriate reference size, certain keywords (e.g., ‘smaller’, ‘bolder’) must be replaced according to their definitions, and valid relative URLs must be resolved to become absolute. See examples (f), (g) and (h) in the table below.

The computed value is the result of resolving the specified value as defined in the "Computed Value" line of the property definition table. Generally, this means resolving the specified value insofar as possible without formatting the document.

The computed value is the value that is transferred from parent to child during inheritance.

The computed value exists even when the property does not apply (as defined by the "Applies To" line). However, some properties may define the computed value of a property for an element to depend on whether the property applies to that element.

5.3. Finding the used value

Computed values are processed as far as possible without formatting the document. Some values, however, can only be determined when the document is being laid out. For example, if the width of an element is set to be a certain percentage of its containing block, the width cannot be determined until the width of the containing block has been determined. The used value is the result of taking the computed value and resolving any remaining dependencies into an absolute value.

5.4. Finding the actual value

A used value is in principle ready to be used, but a user agent may not be able to make use of the value in a given environment. For example, a user agent may only be able to render borders with integer pixel widths and may therefore have to approximate the computed width. Also, the font size of an element may need adjustment based on the availability of fonts or the value of the ‘font-size-adjust’ property. The actual value is the used value after any such approximations have been made.

By probing the actual values of elements, much can be learned about how the document is laid out. However, not all information is recorded in the actual values. For example, the actual value of the ‘page-break-after’ property does not reflect whether there is a page break or not after the element. Similarly, the actual value of ‘orphans’ does not reflect how many orphan lines there is in a certain element. See examples (j) and (k) in the table below.

5.5. Examples

Property Winning declaration Specified value Computed value Used value Actual value
(a) text-aligntext-align: left leftleftleftleft
(b) border-top-width’, ‘border-right-width’, ‘border-bottom-width’, ‘border-left-widthborder-width: inherit 4.2px4.2px4.2px4px
(c) width(none) auto(initial value) auto120px120px
(d) list-style-positionlist-style-position: inherit insideinsideinsideinside
(e) list-style-positionlist-style-position: initial outside(initial value) outsideoutsideoutside
(f) font-sizefont-size: 1.2em 1.2em14.1px14.1px14px
(g) widthwidth: 80% 80%80%354.2px354px
(h) widthwidth: auto autoauto134px134px
(i) heightheight: auto autoauto176px176px
(j) page-break-after(none) auto(initial value) autoautoauto
(k) orphansorphans: 3 3333

6. Conformance

[TBD]

7. Changes

7.1. Changes since the 15 December 2005 Working Draft

The following major changes were made to this specification since the 15 December 2005 Working Draft:

Acknowledgments

David Baron and Boris Zbarsky contributed to this specification.

References

Normative references

[CSS3-ANIMATIONS]
Dean Jackson; et al. CSS Animations. 3 April 2012. W3C Working Draft. (Work in progress.) URL: http://www.w3.org/TR/2012/WD-css3-animations-20120403/
[CSS3-CONDITIONAL]
L. David Baron. CSS Conditional Rules Module Level 3. 11 September 2012. W3C Working Draft. (Work in progress.) URL: http://www.w3.org/TR/2012/WD-css3-conditional-20120911/
[CSS3-TRANSITIONS]
Dean Jackson; et al. CSS Transitions. 3 April 2012. W3C Working Draft. (Work in progress.) URL: http://www.w3.org/TR/2012/WD-css3-transitions-20120403/
[CSSSTYLEATTR]
Tantek Çelik; Elika J. Etemad. CSS Style Attributes. 12 October 2010. W3C Candidate Recommendation. (Work in progress.) URL: http://www.w3.org/TR/2010/CR-css-style-attr-20101012/
[DOM-LEVEL-2-STYLE]
Chris Wilson; Philippe Le Hégaret; Vidur Apparao. Document Object Model (DOM) Level 2 Style Specification. 13 November 2000. W3C Recommendation. URL: http://www.w3.org/TR/2000/REC-DOM-Level-2-Style-20001113/
[MEDIAQ]
Florian Rivoal. Media Queries. 19 June 2012. W3C Recommendation. URL: http://www.w3.org/TR/2012/REC-css3-mediaqueries-20120619/
[SELECT]
Tantek Çelik; et al. Selectors Level 3. 29 September 2011. W3C Recommendation. URL: http://www.w3.org/TR/2011/REC-css3-selectors-20110929/

Other references

[CSS21]
Bert Bos; et al. Cascading Style Sheets Level 2 Revision 1 (CSS 2.1) Specification. 7 June 2011. W3C Recommendation. URL: http://www.w3.org/TR/2011/REC-CSS2-20110607/
[CSS3PAGE]
Håkon Wium Lie; Melinda Grant. CSS3 Module: Paged Media. 10 October 2006. W3C Working Draft. (Work in progress.) URL: http://www.w3.org/TR/2006/WD-css3-page-20061010
[HTML5]
Ian Hickson. HTML5. 25 May 2011. W3C Working Draft. (Work in progress.) URL: http://www.w3.org/TR/2011/WD-html5-20110525/
[SVG11]
Erik Dahlström; et al. Scalable Vector Graphics (SVG) 1.1 (Second Edition). 16 August 2011. W3C Recommendation. URL: http://www.w3.org/TR/2011/REC-SVG11-20110816/

Index

Property index

Property Values Initial Applies to Inh. Percentages Media
all See individual properties See individual properties See individual properties See individual properties See individual properties