Movatterモバイル変換


[0]ホーム

URL:


previous  next  contents  properties  index  


6 Assigning property values, Cascading, and Inheritance

Contents

6.1Specified, computed, and actual values

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").

6.1.1Specified values

User agents must first assign a specified value to each property basedon the following mechanisms (in order of precedence):

  1. If thecascade results in a value, use it.Except that, if the value is 'inherit', the specified value is definedin“The 'inherit' value” below.
  2. Otherwise, if the property isinherited and the element is not the root of thedocument tree, use the computed value of the parent element.
  3. Otherwise use the property'sinitial value. The initial value of each property is indicated in the property's definition.

6.1.2Computed values

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.

6.1.3Used values

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.

6.1.4Actual values

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.

6.2Inheritance

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'font-size' is inherited, the EMelement will have the computed value '13pt' as well. If theuser agent does not have the 13pt font available, theactual value of'font-size'for both H1 and EM might be, for example, '12pt'.

Note that inheritance follows the document tree and isnot intercepted byanonymous boxes.

6.2.1 The'inherit'value

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'color' and'background' properties are set onthe BODY element. On all other elements, the 'color' value will beinherited and the background will be transparent. If these rules arepart of the user's style sheet, black text on a white background willbe enforced throughout the document.

body {  color: black !important;   background: white !important;}* {   color: inherit !important;   background: transparent !important;}

6.3The @import rule

The'@import' rule allows users toimport style rules from other style sheets. In CSS 2.2, any@import rules must precede all other rules (except the @charset rule,if present). See thesection onparsing for when user agents must ignore @import rules. The'@import' keyword must be followed by the URI of the style sheet toinclude. A string is also allowed; it will be interpreted as if it hadurl(...) around it.

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@import rules. Theseconditionalimports specify comma-separated media types after the URI.

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.

6.4The cascade

Style sheets may have three different origins: author, user, anduser agent.

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.

6.4.1Cascading order

To find the value for an element/property combination, user agentsmust apply the following sorting order:

  1. Find all declarations that apply to the element and property in question, for the targetmedia type. Declarations apply if the associated selectormatches 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.
  2. Sort according to importance (normal or important)and origin (author, user, or user agent). In ascending order ofprecedence:
    1. user agent declarations
    2. user normal declarations
    3. author normal declarations
    4. author important declarations
    5. user important declarations
  3. Sort rules with the same importance and origin byspecificity of selector: more specific selectors will override more general ones. Pseudo-elements and pseudo-classes are counted as normal elements and classes, respectively.
  4. 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.

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].

6.4.2!important rules

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.,'background') to be "!important" isequivalent to declaring all of its sub-properties to be "!important".

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 }

6.4.3Calculating a selector's specificity

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.

6.4.4Precedence of non-CSS presentational hints

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; }

previous  next  contents  properties  index  


[8]ページ先頭

©2009-2025 Movatter.jp