📄 cascade.html
字号:
<pre>@import url("fineprint.css") print;@import url("bluish.css") projection, tv;</pre></div><p>In the absence of any media types, the import isunconditional. Specifying 'all' for the medium has the same effect.<H2>6.4 <a name="cascade">The cascade</a></h2><P>Style sheets may have three different origins: author, user, anduser agent.</p><ul><li><strong>Author</strong>. The author specifies style sheetsfor a source document according to the conventions of the documentlanguage. For instance, in HTML, style sheets may be included in thedocument or linked externally.<li><strong>User</strong>: The user may be able to specify styleinformation for a particular document. For example, the user mayspecify a file that contains a style sheet or the user agent mayprovide an interface that generates a user style sheet (or behave asif it did).<li><strong>User agent</strong>: <ahref="conform.html#conformance">Conforming user agents</a> must applya <span class="index-def" title="default style sheet"><aname="default-style-sheet"><dfn>default style sheet</dfn></a></span>(or behave as if they did) prior to all other style sheets for adocument. A user agent's default style sheet should present theelements of the document language in ways that satisfy generalpresentation expectations for the document language (e.g., for visualbrowsers, the EM element in HTML is presented using an italicfont). See <a href="sample.html">"A sample style sheet for HTML4.0"</a> for a recommended default style sheet for HTML 4.0 documents.<P>Note that the default style sheet may change if system settings aremodified by the user (e.g., system colors). However, due tolimitations in a user agent's internal implementation, it may beimpossible to change the values in the default style sheet.</ul><P>Style sheets from these three origins will overlap in scope, andthey interact according to the cascade.<P>The CSS <a name="x10"><span class="index-def" title="cascade">cascade</span></a>assigns a weight to each style rule. When several rules apply, the onewith the greatest weight takes precedence.<P>By default, rules in author style sheets have more weight thanrules in user style sheets. Precedence is reversed, however, for"!important" rules. All rules user and author rules have more weightthan rules in the UA's default style sheet.<P>Imported style sheets also cascade and their weight depends ontheir import order. Rules specified in a given style sheet overriderules imported from other style sheets. Imported style sheets canthemselves import and override other style sheets, recursively, andthe same precedence rules apply.<H3>6.4.1 <a name="cascading-order">Cascading order</a></H3><P>To find the value for an element/property combination, user agentsmust 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. <LI> The primary sort of the declarations is by weight and origin: for normal declarations, author style sheets override user style sheets which override the default style sheet. For <a name="x11"><span class="index-def" title="important">"!important"</span></a> declarations, user style sheets override author style sheets which override the default style sheet. "!important" declaration override normal declarations. An imported style sheet has the same origin as the style sheet that imported it. <LI> The secondary sort is 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 rules have the same weight, origin and specificity, the latter specified wins. Rules in imported style sheets are considered to be before any rules 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 ofthe reader. It is therefore important that the user agent give theuser the ability to turn off the influence of a certain style sheet,e.g., through a pull-down menu.<H3>6.4.2 <a name="important-rules">!important rules</a></H3><P>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). <P>However, for balance, an "!important" declaration (the keywords "!"and "important" follow the declaration) takes precedence over a normaldeclaration. 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.<div class="note"><P><em><strong>Note.</strong>This is a semantic change since CSS1. In CSS1, author "!important"rules took precedence over user "!important" rules. </em></div><P>Declaring a <a name="x12"><span class="index-inst" title="shorthandproperty">shorthand property</span></a> (e.g., <a href="colors.html#propdef-background" class="noxref"><spanclass="propinst-background">'background'</span></a>) to be "!important" isequivalent 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 examplecontains an "!important" declaration, which overrides the correspondingdeclaration in the author's styles 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.<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: 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 the number of ID attributes in the selector (= a)<li>count the number of other attributes and pseudo-classes in the selector (= b)<li>count the number of element names in the selector (= c)<li>ignore pseudo-elements.</ul><P>Concatenating the three numbers a-b-c (in a number system with a largebase) gives the specificity. <div class="example"><P style="display:none">Example(s):</P><P>Some examples:<PRE>* {} /* a=0 b=0 c=0 -> specificity = 0 */LI {} /* a=0 b=0 c=1 -> specificity = 1 */UL LI {} /* a=0 b=0 c=2 -> specificity = 2 */UL OL+LI {} /* a=0 b=0 c=3 -> specificity = 3 */H1 + *[REL=up]{} /* a=0 b=1 c=1 -> specificity = 11 */UL OL LI.red {} /* a=0 b=1 c=3 -> specificity = 13 */ LI.red.level {} /* a=0 b=2 c=1 -> specificity = 21 */#x34y {} /* a=1 b=0 c=0 -> specificity = 100 */ </PRE></div><P>In HTML, values of an element's "style" attribute are style sheetrules. These rules have no selectors, but for the purpose of step 3 ofthe cascade algorithm, they are considered to have an ID selector(specificity: a=1, b=0, c=0). For the purpose of step 4, they areconsidered to be after all other rules.<div class="html-example"><P><PRE><HEAD><STYLE type="text/css"> #x97z { color: blue }</STYLE></HEAD><BODY><P ID=x97z style="color: red"></BODY></PRE><P> In the above example, the color of the P element would bered. Although the specificity is the same for both declarations, thedeclaration in the "style" attribute will override the one in theSTYLE element because of cascading rule 4.</div><H3><a name="q12">6.4.4 Precedence of non-CSS presentational hints</a></H3> <P>The UA may choose to honor presentational hints from other sourcesthan style sheets, for example the FONT element or the "align"attribute in HTML. If so, the non-CSS presentational hints must betranslated to the corresponding CSS rules with specificity equal tozero. The rules are assumed to be at the start of the author style sheetand may be overridden by subsequent style sheet rules.<div class="note"><P><em><strong>Note.</strong>In a transition phase, this policy will make it easier forstylistic attributes to coexist with style sheets.</em></div><div class="note"><P><em><strong>Note.</strong>In CSS1, the non-CSS presentational hints were given a specificityequal to 1, not 0. The change is due to the introduction of theuniversal selector, which has a specificity of 0.</em></div><hr class="navbar"><div class="navbar" align="center"><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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -