Contents
Once a user agent has parsed a document and constructed adocument tree, it must assign, forevery element in the tree, a value to every property that applies to thetargetmedia type.
The final value of a property is the result of a four-stepcalculation: the value is determined through specification (the"specified value"), then resolved into a value that is used forinheritance (the "computed value"), then converted into an absolutevalue if necessary (the "used value"), and finally transformedaccording to the limitations of the local environment (the "actualvalue").
User agents must first assign a specified value to each property basedon the following mechanisms (in order of precedence):
Specified values are resolved to computed values during the cascade;for example URIs are made absolute and 'em' and 'ex'units are computed to pixel or absolute lengths. Computing a valuenever requires the user agent to render the document.
The computed value of URIs that the UA cannot resolve to absoluteURIs is the specified value.
The computed value of aproperty is determined as specified by the Computed Value line in thedefinition of theproperty. See the section oninheritancefor the definition of computed values when the specified value is'inherit'.
The computed value exists even when the property does not apply, asdefined by the'Applies To' line.However, someproperties may define the computed value of a property for an elementto depend on whether the property applies to that element.
Computed values are processed as far as possible without formattingthe document. Some values, however, can only be determined when thedocument is being laid out. For example, if the width of an element isset to be a certain percentage of its containing block, the widthcannot be determined until the width of the containing block has beendetermined. Theused value is the result oftaking the computed value and resolving any remaining dependenciesinto an absolute value.
A used value is in principle the value used for rendering, but auser agent may not be able to make use of the value in a givenenvironment. For example, a user agent may only be able to renderborders with integer pixel widths and may therefore have toapproximate the computed width, or the user agent may be forced touse only black and white shades instead of full color. The actualvalue is the used value after any approximations have been applied.
Some values are inherited by the children of an element in thedocument tree, as describedabove. Each propertydefines whether it is inherited ornot.
Suppose there is an H1 element with an emphasizing element (EM)inside:
<H1>The headline <EM>is</EM> important!</H1>
If no color has been assigned to the EM element, the emphasized"is" will inherit the color of the parent element, so if H1 has thecolor blue, the EM element will likewise be in blue.
When inheritance occurs, elements inherit computed values. Thecomputed value from the parent element becomes both the specifiedvalue and the computed value on the child.
Example(s):
For example, given the following style sheet:
body { font-size: 10pt }h1 { font-size: 130% }
and this document fragment:
<BODY> <H1>A <EM>large</EM> heading</H1></BODY>
the'font-size' propertyfor the H1 element will have the computed value '13pt' (130% times10pt, the parent's value). Since the computed value of
Note that inheritance follows the document tree and isnot intercepted byanonymous boxes.
Each property may also have a cascaded value of 'inherit', whichmeans that, for a given element, the property takes as specifiedvalue the computed value of the element's parent. The 'inherit' valuecan be used to enforce inheritance of values, and it can also be used onproperties that are not normally inherited.
If the 'inherit' value is set on the root element, the property isassigned its initial value.
Example(s):
In the example below, the
body { color: black !important; background: white !important;}* { color: inherit !important; background: transparent !important;}
The
Example(s):
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 unsupportedmedia types, authors may specifymedia-dependent
Example(s):
The following rules illustrate how @import rules can be made media-dependent:
@import url("fineprint.css") print;@import url("bluish.css") projection, tv;
In the absence of any media types, the import isunconditional. Specifying 'all' for the medium has the same effect.The import only takes effect if the target medium matches the medialist.
A target medium matches a media list if one of the items in themedia list is the target medium or 'all'.
Note that Media Queries[MEDIAQ] extends the syntax ofmedia lists and the definition of matching.
When the same style sheet is imported or linked to a document inmultiple places, user agents must process (or act as though they do)each link as though the link were to a separate style sheet.
Style sheets may have three different origins: author, user, anduser agent.
Note that the user may modify system settings (e.g., system colors) that affect the default style sheet. However, some user agent implementations make it impossible to change the values in the default style sheet.
Style sheets from these three origins will overlap in scope, andthey interact according to the cascade.
The CSScascadeassigns a weight to each style rule. When several rules apply, the onewith the greatest weight takes precedence.
By default, rules in author style sheets have more weight thanrules in user style sheets. Precedence is reversed, however, for"!important" rules. All user and author rules have more weightthan rules in the UA's default style sheet.
To find the value for an element/property combination, user agentsmust apply the following sorting order:
Apart from the "!important" setting on individual declarations,this strategy gives author's style sheets higher weight than those ofthe reader. User agents must give the user the ability to turn off theinfluence of specific author style sheets, e.g., through a pull-downmenu. Conformance to UAAG 1.0 checkpoint 4.14 satisfies this condition[UAAG10].
CSS attempts to create a balance of power between authorand user style sheets. By default, rules in an author's stylesheet override those in a user's style sheet (see cascaderule 3).
However, for balance, an "!important" declaration (the delimiter token "!" and keyword "important" follow the declaration) takes precedence over a normal declaration. Both author and user style sheets may contain"!important" declarations, and user "!important" rules override author"!important" rules. This CSS feature improves accessibilityof documents by giving users with special requirements (largefonts, color combinations, etc.) control over presentation.
Declaring ashorthand property (e.g.,
Example(s):
The first rule in the user's style sheet in the following examplecontains an "!important" declaration, which overrides the correspondingdeclaration in the author's style sheet. The second declarationwill also win due to being marked "!important". However, the thirdrule in the user's style sheet is not "!important" and will thereforelose to the second rule in the author's style sheet (which happens toset style on a shorthand property). Also, the third author rule willlose to the second author rule since the second rule is"!important". This shows that "!important" declarations have afunction 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 }
A selector's specificity is calculated as follows:
The specificity is based only on the form of the selector. Inparticular, a selector of the form "[id=p33]" is counted as anattribute selector (a=0, b=0, c=1, d=0), even if the id attribute isdefined as an "ID" in the source document's DTD.
Concatenating the four numbers a-b-c-d (in a number system with a largebase) gives the specificity.
Example(s):
Some examples:
* {} /* a=0 b=0 c=0 d=0 -> specificity = 0,0,0,0 */ li {} /* a=0 b=0 c=0 d=1 -> specificity = 0,0,0,1 */ li:first-line {} /* a=0 b=0 c=0 d=2 -> specificity = 0,0,0,2 */ ul li {} /* a=0 b=0 c=0 d=2 -> specificity = 0,0,0,2 */ ul ol+li {} /* a=0 b=0 c=0 d=3 -> specificity = 0,0,0,3 */ h1 + *[rel=up]{} /* a=0 b=0 c=1 d=1 -> specificity = 0,0,1,1 */ ul ol li.red {} /* a=0 b=0 c=1 d=3 -> specificity = 0,0,1,3 */ li.red.level {} /* a=0 b=0 c=2 d=1 -> specificity = 0,0,2,1 */ #x34y {} /* a=0 b=1 c=0 d=0 -> specificity = 0,1,0,0 */ /* a=1 b=0 c=0 d=0 -> specificity = 1,0,0,0 */
<HEAD><STYLE type="text/css"> #x97z { color: red }</STYLE></HEAD><BODY><P ID=x97z></BODY>
In the above example, the color of the P element would begreen. The declaration in the "style" attribute will override the one inthe STYLE element because of cascading rule 3, since it has a higherspecificity.
The UA may choose to honor presentational attributes in an HTML sourcedocument. If so, these attributes are translated to the correspondingCSS rules with specificity equal to 0, and are treated as if they wereinserted at the start of the author style sheet. They may therefore beoverridden by subsequent style sheet rules. In a transition phase,this policy will make it easier for stylistic attributes to coexistwith style sheets.
For HTML, any attribute that is not in the following list should beconsidered presentational: abbr, accept-charset, accept, accesskey,action, alt, archive, axis, charset, checked, cite, class, classid,code, codebase, codetype, colspan, coords, data, datetime, declare,defer, dir, disabled, enctype, for, headers, href, hreflang,http-equiv, id, ismap, label, lang, language, longdesc, maxlength,media, method, multiple, name, nohref, object, onblur, onchange,onclick, ondblclick, onfocus, onkeydown, onkeypress, onkeyup, onload,onload, onmousedown, onmousemove, onmouseout, onmouseover, onmouseup,onreset, onselect, onsubmit, onunload, onunload, profile, prompt,readonly, rel, rev, rowspan, scheme, scope, selected, shape, span,src, standby, start, style, summary, title, type (except on LI, OL and UL elements), usemap, value, valuetype, version.
For other languages, all document language-based styling must betranslated to the corresponding CSS and either enter the cascade atthe user agent level or, as with HTML presentational hints, be treatedas author level rules with a specificity of zero placed at the startof the author style sheet.
Example(s):
The following user style sheet would override the font weight of'b' elements in all documents, and the color of 'font'elements with color attributes in XML documents. It would not affectthe color of any 'font' elements with color attributes in HTMLdocuments:
b { font-weight: normal; }font[color] { color: orange; }
The following, however, would override the color of font elements in all documents:
font[color] { color: orange ! important; }