CINXE.COM
Assigning property values, Cascading, and Inheritance
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html lang="en"> <HEAD> <TITLE>Assigning property values, Cascading, and Inheritance</TITLE> <link rel="stylesheet" href="style/default.css" type="text/css"> <link rel="stylesheet" href="https://www.w3.org/StyleSheets/TR/W3C-REC.css" type="text/css"> <link rel="prev" href="selector.html"> <link rel="next" href="media.html"> <link rel="contents" href="cover.html#minitoc"> <link rel="CSS-properties" href="propidx.html" title="properties"> <link rel="index" href="indexlist.html" title="index"> <link rel="first" href="cover.html"> </HEAD> <BODY> <div class="navbar"> <p><a href="selector.html">previous</a> <a href="media.html">next</a> <a href="cover.html#minitoc">contents</a> <a href="propidx.html">properties</a> <a href="indexlist.html">index</a> </div> <hr class="navbar"> <h1><a name="q6.0">6 Assigning property values, Cascading, and Inheritance</a></h1> <div class="subtoc"> <p><strong>Contents</strong> <ul class="toc"> <li class="tocline2"><a href="cascade.html#value-stages" class="tocxref">6.1 Specified, computed, and actual values</a> <ul class="toc"> <li class="tocline3"><a href="cascade.html#specified-value" class="tocxref">6.1.1 <span class="index-def" title="specified value"> Specified values</span></a> <li class="tocline3"><a href="cascade.html#computed-value" class="tocxref">6.1.2 <span class="index-def" title="computed value"> Computed values</span></a> <li class="tocline3"><a href="cascade.html#used-value" class="tocxref">6.1.3 <span class="index-def" title="used value"> Used values</span></a> <li class="tocline3"><a href="cascade.html#actual-value" class="tocxref">6.1.4 <span class="index-def" title="actual value"> Actual values</span></a> </ul> <li class="tocline2"><a href="cascade.html#inheritance" class="tocxref">6.2 Inheritance</a> <ul class="toc"> <li class="tocline3"><a href="cascade.html#value-def-inherit" class="tocxref">6.2.1 The <span class="index-def" title="inherit, definition of">'inherit'</span> value</a> </ul> <li class="tocline2"><a href="cascade.html#at-import" class="tocxref">6.3 The @import rule</a> <li class="tocline2"><a href="cascade.html#cascade" class="tocxref">6.4 The cascade</a> <ul class="toc"> <li class="tocline3"><a href="cascade.html#cascading-order" class="tocxref">6.4.1 Cascading order</a> <li class="tocline3"><a href="cascade.html#important-rules" class="tocxref">6.4.2 !important rules</a> <li class="tocline3"><a href="cascade.html#specificity" class="tocxref">6.4.3 Calculating a selector's specificity</a> <li class="tocline3"><a href="cascade.html#preshint" class="tocxref">6.4.4 Precedence of non-CSS presentational hints</a> </ul> </ul> </div> <!-- warning --> <div> <input id=annoying-warning type=checkbox title="hide note"> <label for=annoying-warning>(hide)</label> <div class=annoying-warning> <p><strong>Note:</strong> Several sections of this specification have been updated by other specifications. Please, see <a href="https://www.w3.org/TR/CSS/#css" >"Cascading Style Sheets (CSS) — The Official Definition"</a> in the latest <cite>CSS Snapshot</cite> for a list of specifications and the sections they replace. <p>The CSS Working Group is also developing <a href="https://www.w3.org/TR/CSS22/">CSS level 2 revision 2 (CSS 2.2).</a> </div> </div> <!-- /warning --> <h2>6.1 <a name="value-stages">Specified, computed, and actual values</a></h2> <p>Once a user agent has parsed a document and constructed a <a href="conform.html#doctree">document tree</a>, it must assign, for every element in the tree, a value to every property that applies to the target <a href="media.html">media type</a>. <p>The final value of a property is the result of a four-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 "used value"), and finally transformed according to the limitations of the local environment (the "actual value"). <h3>6.1.1 <span class="index-def" title="specified value"> <a name="specified-value">Specified values</a></span></h3> <p>User agents must first assign a specified value to each property based on the following mechanisms (in order of precedence):</p> <ol> <li>If the <a href="#cascade">cascade</a> results in a value, use it. <li>Otherwise, if the property is <a href="#inheritance">inherited</a> and the element is not the root of the <a>document tree</a>, use the computed value of the parent element. <li>Otherwise use the property's <a name="x1"><span class="index-def" title="initial value">initial value</span></a>. The initial value of each property is indicated in the property's definition. </ol> <h3>6.1.2 <span class="index-def" title="computed value"> <a name="computed-value">Computed values</a></span></h3> <p>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 value never requires the user agent to render the document. <p>The computed value of URIs that the UA cannot resolve to absolute URIs is the specified value. <p>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 <a href="#inheritance">inheritance</a> for the definition of computed values when the specified value is 'inherit'. <p>The computed value exists even when the property does not apply, as defined by the <a href="about.html#applies-to">'Applies To'</a> 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. <h3>6.1.3 <span class="index-def" title="used value"> <a name="used-value">Used values</a></span></h3> <p>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 <dfn id="usedValue">used value</dfn> is the result of taking the computed value and resolving any remaining dependencies into an absolute value. <h3>6.1.4 <span class="index-def" title="actual value"> <a name="actual-value">Actual values</a></span></h3> <p>A used value is in principle 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, or the user agent may be forced to use only black and white shades instead of full color. The actual value is the used value after any approximations have been applied. <h2>6.2 <a name="inheritance">Inheritance</a></h2> <p>Some values are inherited by the children of an element in the <a href="conform.html#doctree">document tree</a>, as described <a href="#specified-value">above</a>. Each property <a href="about.html#property-defs">defines</a> whether it is inherited or not. <div class="html-example"><P> Suppose there is an H1 element with an emphasizing element (EM) inside:</p> <pre> <H1>The headline <EM>is</EM> important!</H1> </pre> <p>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. </div> <p>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. <div class="example"><P style="display:none">Example(s):</P><P> For example, given the following style sheet:</P> <pre> body { font-size: 10pt } h1 { font-size: 130% } </pre> <p>and this document fragment: <pre class="html-example"> <BODY> <H1>A <EM>large</EM> heading</H1> </BODY> </pre> <p>the <span class="prop-inst-font-size">'font-size'</span> property for the H1 element will have the computed value '13pt' (130% times 10pt, the parent's value). Since the computed value of <a href="fonts.html#propdef-font-size" class="noxref"><span class="propinst-font-size">'font-size'</span></a> is inherited, the EM element will have the computed value '13pt' as well. If the user agent does not have the 13pt font available, the actual value of <a href="fonts.html#propdef-font-size" class="noxref"><span class="propinst-font-size">'font-size'</span></a> for both H1 and EM might be, for example, '12pt'. </div> <p class=note>Note that inheritance follows the document tree and is not intercepted by <span class=index-inst><a name="x5" href="visuren.html#box-gen">anonymous boxes.</a></span> <h3>6.2.1 The <span class="index-def" title="inherit, definition of"><a name="value-def-inherit">'inherit'</a></span> value</h3> <P>Each property may also have a cascaded value of 'inherit', which means that, for a given element, the property takes the same specified value as the property for the element's parent. The 'inherit' value can be used to enforce inheritance of values, and it can also be used on properties that are not normally inherited. <p>If the 'inherit' value is set on the root element, the property is assigned its initial value. <div class="example"><P style="display:none">Example(s):</P><P>In the example below, the <a href="colors.html#propdef-color" class="noxref"><span class="propinst-color">'color'</span></a> and <a href="colors.html#propdef-background" class="noxref"><span class="propinst-background">'background'</span></a> 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. <PRE> body { color: black !important; background: white !important; } * { color: inherit !important; background: transparent !important; } </PRE> </div> <H2>6.3 <a name="at-import">The @import rule</a></h2> <P>The <a name="x7"><span class="index-def" title="@import"><dfn>'@import'</dfn></span></a> rule allows users to import style rules from other style sheets. In CSS 2.1, any @import rules must precede all other rules (except the @charset rule, if present). See the <a href="syndata.html#at-rules">section on parsing</a> for when user agents must ignore @import rules. 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. <div class="example"><P style="display:none">Example(s):</P><P> The following lines are equivalent in meaning and illustrate both '@import' syntaxes (one with "url()" and one with a bare string):</p> <PRE> @import "mystyle.css"; @import url("mystyle.css"); </PRE> </div> <p>So that user agents can avoid retrieving resources for unsupported <a href="media.html">media types</a>, authors may specify media-dependent <a name="x8"><span class="index-inst" title="@import">@import</span></a> rules. These <a name="x9"><span class="index-def" title="conditional import|media-dependent import">conditional imports</span></a> specify comma-separated media types after the URI. <div class="example"><P style="display:none">Example(s):</P> <p>The following rules illustrate how @import rules can be made media-dependent:</p> <pre> @import url("fineprint.css") print; @import url("bluish.css") projection, tv; </pre> </div> <p>In the absence of any media types, the import is unconditional. Specifying 'all' for the medium has the same effect. The import only takes effect if the target medium matches the media list. <p>A target medium matches a media list if one of the items in the media list is the target medium or 'all'. <p class=note>Note that Media Queries <a href="refs.html#ref-MEDIAQ" rel="biblioentry" class="noxref"><span class="informref">[MEDIAQ]</span></a> extends the syntax of media lists and the definition of matching. <p>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. <H2>6.4 <a name="cascade">The cascade</a></h2> <P>Style sheets may have three different origins: author, user, and user agent.</p> <ul> <li><strong>Author</strong>. 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. <li><strong>User</strong>: 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). <li><strong>User agent</strong>: <a href="conform.html#conformance">Conforming user agents</a> must apply a <span class="index-def" title="default style sheet"><a name="default-style-sheet"><dfn>default style sheet</dfn></a></span> (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 <a href="sample.html">A sample style sheet for HTML </a> for a recommended default style sheet for HTML documents. <p class="note">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. </ul> <P>Style sheets from these three origins will overlap in scope, and they interact according to the cascade. <P>The CSS <a name="x12"><span class="index-def" title="cascade">cascade</span></a> assigns a weight to each style rule. When several rules apply, the one with the greatest weight takes precedence. <P>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. <H3>6.4.1 <a name="cascading-order">Cascading order</a></H3> <P>To find the value for an element/property combination, user agents must apply the following sorting order:</p> <OL> <LI> Find all declarations that apply to the element and property in question, for the target <a href="media.html">media type</a>. Declarations apply if the associated selector <A HREF="selector.html">matches</A> the element in question and the target medium matches the media list on all @media rules containing the declaration and on all links on the path through which the style sheet was reached. <LI>Sort according to importance (normal or important) and origin (author, user, or user agent). In ascending order of precedence: <ol> <li>user agent declarations <li>user normal declarations <li>author normal declarations <li>author important declarations <li>user important declarations </ol> <LI> Sort rules with the same importance and origin by <a href="#specificity">specificity</a> of selector: more specific selectors will override more general ones. Pseudo-elements and pseudo-classes are counted as normal elements and classes, respectively. <LI>Finally, sort by order specified: if two declarations have the same weight, origin and specificity, the latter specified wins. Declarations in imported style sheets are considered to be before any declarations in the style sheet itself. </OL> <P>Apart from the "!important" setting on individual declarations, this strategy gives author's style sheets higher weight than those of the reader. User agents must give the user the ability to turn off the influence of specific author style sheets, e.g., through a pull-down menu. Conformance to UAAG 1.0 checkpoint 4.14 satisfies this condition <a href="refs.html#ref-UAAG10" rel="biblioentry" class="noxref"><span class="normref">[UAAG10]</span></a>. <H3>6.4.2 <a name="important-rules">!important rules</a></H3> <P>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). <P>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 accessibility of documents by giving users with special requirements (large fonts, color combinations, etc.) control over presentation. <P>Declaring a <a name="x13"><span class="index-inst" title="shorthand property">shorthand property</span></a> (e.g., <a href="colors.html#propdef-background" class="noxref"><span class="propinst-background">'background'</span></a>) to be "!important" is equivalent to declaring all of its sub-properties to be "!important". <div class="example"><P style="display:none">Example(s):</P> <P>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. <PRE> /* 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 } </PRE> </div> <H3>6.4.3 <a name="specificity">Calculating a selector's specificity</a></H3> <P>A selector's specificity is calculated as follows:</p> <ul> <li>count 1 if the declaration is from is a 'style' attribute rather than a rule with a selector, 0 otherwise (= a) (In HTML, values of an element's "style" attribute are style sheet rules. These rules have no selectors, so a=1, b=0, c=0, and d=0.) <li>count the number of ID attributes in the selector (= b) <li>count the number of other attributes and pseudo-classes in the selector (= c) <li>count the number of element names and pseudo-elements in the selector (= d) </ul> <p>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. <P>Concatenating the four numbers a-b-c-d (in a number system with a large base) gives the specificity. <div class="example"><P style="display:none">Example(s):</P><P> Some examples: <PRE> * {} /* 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 */ style="" /* a=1 b=0 c=0 d=0 -> specificity = 1,0,0,0 */ </PRE> </div> <div class="html-example"> <pre> <HEAD> <STYLE type="text/css"> #x97z { color: red } </STYLE> </HEAD> <BODY> <P ID=x97z style="color: green"> </BODY> </pre> <P>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. </div> <H3>6.4.4 <a name="preshint">Precedence of non-CSS presentational hints</a></H3> <P>The UA may choose to honor presentational attributes in an 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. <P>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 (except on LI, OL and UL elements), usemap, value, valuetype, version. <P>For other languages, all document language-based styling must be translated to the corresponding CSS and either enter the cascade at the user agent level or, as with HTML presentational hints, be treated as author level rules with a specificity of zero placed at the start of the author style sheet. <div class="example"><P style="display:none">Example(s):</P> <p>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 affect the color of any 'font' elements with color attributes in HTML documents: <pre> b { font-weight: normal; } font[color] { color: orange; } </pre> <P>The following, however, would override the color of font elements in all documents: <pre> font[color] { color: orange ! important; } </pre> </div> <hr class="navbar"> <div class="navbar"> <p><a href="selector.html">previous</a> <a href="media.html">next</a> <a href="cover.html#minitoc">contents</a> <a href="propidx.html">properties</a> <a href="indexlist.html">index</a> </div> </BODY> </html>