Contents
Once a user agent has parsed a document and constructed a document tree, it must assign, for every element in the tree, a value to every property that applies to the target media type.
The final value of a property is the result of a three-stepfour-step
calculation: the value is determined through specification (the
"specified value"), then resolved into a value that is used for
inheritance (the "computed value"), then converted into an absolute
value if necessary (the "computed"used value"), and finally transformed
according to the limitations of the local environment (the "actual
value").
User agents must first assign a specified value to aeach property based
on the following mechanisms (in order of precedence):
Specified values may be absolute (i.e., theyare not specified relativeresolved to another value, as in 'red' or '2mm') orcomputed values during the cascade;
for example valid relative (i.e., theyURIs are specified relative to another value, as in 'auto', '2em',made absolute and '12%'). For'em' and 'ex'
units are computed to pixel or absolute values, no computation is neededlengths. Computing a value
never requires the user agent to findrender the computed value. Relative values, ondocument.
The other hand, must be transformed intocomputed values: percentages must be multiplied by a reference value (each property defines whichvalue that is), values with relative units (em, ex, px) must be madeof invalid and absolute by multiplying with the appropriate font or pixel size, 'auto' values must be computed byURIs is the formulas given with each property, certain keywords ('smaller', 'bolder', 'inherit') must be replaced according to their definitions.specified
value.
When the specified value is not 'inherit', the computed value of a property is determined as specified by the Computed Value line in the definition of the property. See the section on inheritance for the definition of computed values when the specified value is 'inherit'.
The computed value exists even when the property doesn't apply, as
defined by the 'Applies To' [add reference]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.
Computed values can be relative to each other; for example a width could be set as a percentage, which is dependent on the containing block's width. The used value is the result of taking the computed value and resolving these dependencies into a final absolute value used for the actual layout.
A used value is in principle ready to be used,the value used for rendering, 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.width, or the user agent may be forced to
use only black and white shades instead of full colour. The actual
value is the computedused value after any approximations have been applied.
Some values are inherited by the children of an element in the document tree,as described above. Each property defines whether it is inherited or not.
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 the color blue, the EM element will likewise be in blue.
When inheritance occurs, elements inherit computed values. The computed value from the parent element becomes both the specified value and the computed value on the child.
For example, given the following style sheet:
body { font-size: 10pt } h1 { font-size:120%130% }
and this document fragment:
<BODY> <H1>A <EM>large</EM> heading</H1> </BODY>
the 'font-size' property
for the H1 element will have the computed value '12pt' (120%'13pt' (130% times
10pt, the parent's value). Since the computed value of 'font-size' is inherited, the EM
element will have the computed value '12pt''13pt' as well. If the
user agent does not have the 12pt13pt font available, the
actual value of 'font-size'
for both H1 and EM might be, for example, '11pt'.'12pt'.
Each property may also have a specified value of 'inherit', which means that, for a given element, the property takes the same computed value as the property for the element's parent. The 'inherit' value can be used to strengthen inherited values, and it can also be used on properties that are not normally inherited.
In the example below, the 'color' and 'background' properties are set on the BODY element. On all other elements, the 'color' value will be inherited and the background will be transparent. If these rules are part of the user's style sheet, black text on a white background will be enforced throughout the document.
body { color: black !important; background: white !important; } * { color: inherit !important; background: transparent !important; }
The '@import' rule allows users to import style rules from other style sheets. Any @import rules must precede all 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. These conditional imports specify comma-separated media types after the URI.
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 is unconditional. Specifying 'all' for the medium has the same effect.
Style sheets may have three different origins: author, user, and user agent.
Note that the default style sheetuser may change ifmodify system settings are modified by the user (e.g.,(e.g.
system colors).colors) that affect the default style sheet. However, due to limitations in asome user
agent's internal implementation,agent implementations make it may beimpossible to change the values in the
default style sheet.
Style sheets from these three origins will overlap in scope, and they interact according to the cascade.
The CSS cascade assigns a weight to each style rule. When several rules apply, the one with the greatest weight takes precedence.
By default, rules in author style sheets have more weight than rules in user style sheets. Precedence is reversed, however, for "!important" rules. All user and author rules have more weight than rules in the UA's default style sheet.
To find the value for an element/property combination, user agents must apply the following sorting order:
Apart from the "!important" setting on individual declarations, this strategy gives author's style sheets higher weight than those of the reader. It is therefore important that the user agent give the user the ability to turn off the influence of a certain style sheet, e.g., through a pull-down menu.
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 (see cascade rule 3).
However, for balance, an "!important" declaration (the keywordsdelimiter 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 accessibility
of documents by giving users with special requirements (large
fonts, color combinations, etc.) control over presentation.
Declaring a shorthand property (e.g., 'background') to be "!important" is equivalent to declaring all of its sub-properties to be "!important".
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 }
A selector's specificity is calculated as follows:
The specificity is based only on the form of the selector. In particular, a selector of the form "[id=p33]" is counted as an attribute selector (a=0, b=0, c=1, d=0), even if the id attribute is defined as an "ID" in the source document's DTD.
Concatenating the four numbers a-b-c-d (in a number system with a large base) gives the specificity.
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=0d=1d=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 */ style="" /* 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 style="color: green"> </BODY>
In the above example, the color of the P element would be green. The declaration in the "style" attribute will override the one in the STYLE element because of cascading rule 3, since it has a higher specificity.
The UA may choose to honor presentational attributes in thean HTML source
document. If so, these attributes are translated to the corresponding
CSS rules with specificity equal to 0, and are treated as if they were
inserted at the start of the author style sheet. They may therefore be
overridden by subsequent style sheet rules. In a transition phase,
this policy will make it easier for stylistic attributes to coexist
with style sheets.
For HTML, any attribute that is not in the following list should be
considered 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,type (except on LI, OL and
UL elements), usemap, value, valuetype, version.
For XHTML andother languages written in XML, no attribute should be considered presentational. Thelanguages, all document language-based styling of elements and non-presentational attributesshould be
handled in the user agent stylesheet.style sheet.
The following user stylesheet 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 affect the color of any 'font' elements with color attributes in HTML documents:
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; }