Sun, 31 Jan 2016 15:42:31 -0800
change //dev.w3.org/csswg/ urls to //drafts.csswg.org/
1 <h1>CSS Cascading and Inheritance Level 3</h1>
3 <pre class='metadata'>
4 Shortname: css-cascade
5 Level: 3
6 Status: ED
7 Work Status: Testing
8 Group: csswg
9 ED: https://drafts.csswg.org/css-cascade-3/
10 TR: http://www.w3.org/TR/css-cascade-3/
11 Editor: Elika J. Etemad / fantasai, Invited Expert, http://fantasai.inkedblade.net/contact
12 Editor: Tab Atkins Jr., Google, http://xanthir.com/contact/
13 Previous Version: http://www.w3.org/TR/2015/CR-css-cascade-3-20150416/
14 Previous Version: http://www.w3.org/TR/2013/WD-css-cascade-3-20130730/
15 Previous Version: http://www.w3.org/TR/2013/WD-css3-cascade-20130103/
16 Previous Version: http://www.w3.org/TR/2005/WD-css3-cascade-20051215/
17 Issue Tracking: Disposition of Comments https://drafts.csswg.org/css-cascade/issues
18 Abstract: This CSS module describes how to collate style rules and assign values to all properties on all elements. By way of cascading and inheritance, values are propagated for all properties on all elements.
19 Ignored Terms: auto, flex items
20 Link Defaults: css21 (property) display
21 </pre>
23 <h2 id="intro">
24 Introduction</h2>
26 One of the fundamental design principles of CSS is <a lt="cascade">cascading</a>,
27 which allows several style sheets to influence the presentation of a document.
28 When different declarations try to set a value for the same element/property combination,
29 the conflicts must somehow be resolved.
31 The opposite problem arises when no declarations try to set a the value for an element/property combination.
32 In this case, a value is be found by way of <a>inheritance</a>
33 or by looking at the property's <a>initial value</a>.
35 The <a href="#cascade">cascading</a> and <a href="#defaulting">defaulting</a> process takes a set of declarations as input,
36 and outputs a <a>specified value</a> for each property on each element.
38 The rules for finding the specified value for all properties on all elements in the document are described in this specification.
39 The rules for finding the specified values in the page context and its margin boxes are described in [[CSS3PAGE]].
41 <h2 id="at-import">
42 Importing Style Sheets: the ''@import'' rule</h2>
44 The <dfn>@import</dfn> rule allows users to import style rules from other style sheets.
45 If an ''@import'' rule refers to a valid stylesheet,
46 user agents must treat the contents of the stylesheet as if they were written in place of the ''@import'' rule.
48 <p class='example'>
49 For example, declarations in style rules from imported stylesheets interact with the cascade
50 as if they were written literally into the stylesheet at the point of the ''@import''.
51 Similarly, style rules in a stylesheet imported into a scoped stylesheet
52 are scoped in the same way.
54 Any ''@import'' rules must precede all other at-rules and style rules in a style sheet
55 (besides ''@charset'', which must be the first thing in the style sheet if it exists),
56 or else the ''@import'' rule is invalid.
57 The syntax of ''@import'' is:
59 <pre class='prod'>@import [ <<url>> | <<string>> ] <<media-query-list>>? ;</pre>
61 Where the <<url>> or <<string>> gives the URL of the style sheet to be imported,
62 and the optional <<media-query-list>>
63 (the <dfn>import conditions</dfn>)
64 states the conditions under which it applies.
66 If a <<string>> is provided,
67 it must be interpreted as a url with the same value.
69 <div class="example">
70 The following lines are equivalent in meaning
71 and illustrate both ''@import'' syntaxes
72 (one with ''url()'' and one with a bare string):
74 <pre>
75 @import "mystyle.css";
76 @import url("mystyle.css");
77 </pre>
78 </div>
80 The <a>import conditions</a> allow the import to be media-dependent.
81 In the absence of any <a>import conditions</a>, the import is unconditional.
82 (Specifying ''@media/all'' for the <<media-query-list>> has the same effect.)
84 The evaluation and full syntax of the expressions after the URL
85 is defined by the <a href="http://www.w3.org/TR/css3-mediaqueries/">Media Queries</a> specification [[!MEDIAQ]].
86 If the media query does not match,
87 the rules in the imported stylesheet do not apply,
88 exactly as if the imported stylesheet were wrapped in an ''@media'' block with the given media query.
89 User agents may therefore avoid fetching a media-dependent import
90 as long as the media query does not match.
92 <div class=example>
93 The following rules illustrate how ''@import'' rules can be made media-dependent:
95 <pre>
96 @import url("fineprint.css") print;
97 @import url("bluish.css") projection, tv;
98 @import url("narrow.css") handheld and (max-width: 400px);
99 </pre>
100 </div>
102 When the same style sheet is imported or linked to a document in multiple places,
103 user agents must process (or act as though they do) each link
104 as though the link were to an independent style sheet.
106 Note: This does not place any requirements on resource fetching,
107 only how the style sheet is reflected in the CSSOM and used in specs such as this one.
108 Assuming appropriate caching,
109 it is perfectly appropriate for a UA to fetch a style sheet only once,
110 even though it's linked or imported multiple times.
112 The <a>origin</a> of an imported style sheet is the same as the <a>origin</a> of the style sheet that imported it.
114 The <a>environment encoding</a> of an imported style sheet is the encoding of the style sheet that imported it. [[CSS3SYN]]
116 <h3 id='content-type'>
117 Content-Type of CSS Style Sheets</h3>
119 The processing of imported style sheets depends on the actual type of the linked resource.
120 If the resource does not have <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/fetching-resources.html#content-type">Content-Type metadata</a>,
121 or the host document is in <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/infrastructure.html#quirks-mode">quirks mode</a>
122 and has the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/origin-0.html#same-origin">same origin</a> as the imported style sheet,
123 the type of the linked resource is <code>text/css</code>.
124 Otherwise, the type is determined from its <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/fetching-resources.html#content-type">Content-Type metadata</a>.
126 If the linked resource's type is <code>text/css</code>,
127 it must be interpreted as a CSS style sheet.
128 Otherwise, it must be interpreted as a network error.
131 <h2 id='shorthand'>
132 Shorthand Properties</h2>
134 Some properties are <dfn export lt="shorthand property | shorthand">shorthand properties</dfn>,
135 meaning that they allow authors to specify the values of several properties with a single property.
136 A <a>shorthand property</a> sets all of its <dfn export lt="longhand | longhand property | sub-property">longhand sub-properties</dfn>,
137 exactly as if expanded in place.
139 When values are omitted from a <a>shorthand</a> form,
140 unless otherwise defined,
141 each âmissingâ <a>sub-property</a> is assigned its <a>initial value</a>.
143 <div class='note'>
144 This means that a <a>shorthand</a> property declaration always sets <em>all</em> of its <a>sub-properties</a>,
145 even those that are not explicitly set.
146 Carelessly used, this might result in inadvertently resetting some <a>sub-properties</a>.
147 Carefully used, a <a>shorthand</a> can guarantee a âblank slateâ
148 by resetting <a>sub-properties</a> inadvertently cascaded from other sources.
150 For example, writing ''background: green'' rather than ''background-color: green''
151 ensures that the background color overrides any earlier declarations
152 that might have set the background to an image with 'background-image'.
153 </div>
155 <div class='example'>
156 For example, the CSS Level 1 'font' property
157 is a <a>shorthand</a> property for setting <a property>font-style</a>, <a property>font-variant</a>, <a property>font-weight</a>, 'font-size', 'line-height', and <a property>font-family</a> all at once.
158 The multiple declarations of this example:
160 <pre>
161 h1 {
162 font-weight: bold;
163 font-size: 12pt;
164 line-height: 14pt;
165 font-family: Helvetica;
166 font-variant: normal;
167 font-style: normal;
168 }</pre>
170 can therefore be rewritten as
172 <pre>h1 { font: bold 12pt/14pt Helvetica }</pre>
174 As more 'font' <a>sub-properties</a> are introduced into CSS,
175 the shorthand declaration resets those to their initial values as well.
176 </div>
178 In some cases, a <a>shorthand</a> might have different syntax
179 or special keywords
180 that don't directly correspond to values of its <a>sub-properties</a>.
181 (In such cases, the <a>shorthand</a> will explicitly define the expansion of its values.)
183 In other cases, a property might be a <dfn>reset-only sub-property</dfn> of the shorthand:
184 Like other <a>sub-properties</a>, it is reset to its initial value by the shorthand when unspecified,
185 but the shorthand might not include syntax to set the <a>sub-property</a>
186 to any of its other values.
187 For example, the 'border' shorthand resets 'border-image'
188 to its initial value of ''border-image/none'',
189 but has no syntax to set it to anything else. [[CSS3BG]]
191 If a <a>shorthand</a> is specified as one of the <a href="http://www.w3.org/TR/css3-values/#common-keywords">CSS-wide keywords</a> [[!CSS3VAL]],
192 it sets all of its <a>sub-properties</a> to that keyword,
193 including any that are <a>reset-only sub-properties</a>.
194 (Note that these keywords cannot be combined with other values in a single declaration, not even in a shorthand.)
196 Declaring a <a>shorthand</a> property to be ''!important''
197 is equivalent to declaring all of its <a>sub-properties</a> to be ''!important''.
200 <h3 id="all-shorthand">
201 Resetting All Properties: the 'all' property</h3>
203 <table class="propdef">
204 <tr>
205 <th>Name:
206 <td><dfn>all</dfn>
207 <tr>
208 <th>Value:
209 <td>initial | inherit | unset
210 <tr>
211 <th>Initial:
212 <td>See individual properties
213 <tr>
214 <th>Applies to:
215 <td>See individual properties
216 <tr>
217 <th>Inherited:
218 <td>See individual properties
219 <tr>
220 <th>Percentages:
221 <td>See individual properties
222 <tr>
223 <th>Media:
224 <td>See individual properties
225 <tr>
226 <th>Computed value:
227 <td>See individual properties
228 <tr>
229 <th>Animatable:
230 <td>See individual properties
231 </table>
233 The 'all' property is a <a>shorthand</a> that resets <em>all</em> CSS properties
234 except 'direction' and 'unicode-bidi'.
235 It only accepts the <a href="http://www.w3.org/TR/css3-values/#common-keywords">CSS-wide keywords</a>.
237 Note: The excepted properties are actually markup-level features,
238 and <a href="http://www.w3.org/TR/css3-writing-modes/#text-direction">should not be set in the author's style sheet</a>.
239 (They exist as CSS properties only to style document languages not supported by the UA.)
240 Authors should use the appropriate markup, such as HTML's <code>dir</code> attribute, instead.
241 [[CSS3-WRITING-MODES]]
243 <div class='example'>
244 For example, if an author specifies ''all: initial'' on an element
245 it will block all inheritance and reset all properties,
246 as if no rules appeared in the author, user, or user-agent levels of the cascade.
248 This can be useful for the root element of a "widget" included in a page,
249 which does not wish to inherit the styles of the outer page.
250 Note, however, that any "default" style applied to that element
251 (such as, e.g. ''display: block'' from the UA style sheet on block elements such as <code><div></code>)
252 will also be blown away.
253 </div>
255 <h2 id="value-stages">
256 Value Processing</h2>
258 Once a user agent has parsed a document and constructed a document tree,
259 it must assign,
260 to every element in the tree,
261 and correspondingly to every box in the formatting structure,
262 a value to every property that applies to the target media type.
264 The final value of a CSS property for a given element or box
265 is the result of a multi-step calculation:
267 <ul>
268 <li>
269 First, all the <a>declared values</a> applied to an element are collected,
270 for each property on each element.
271 There may be zero or many <a>declared values</a> applied to the element.
273 <li>
274 Cascading yields the <a>cascaded value</a>.
275 There is at most one <a>cascaded value</a> per property per element.
277 <li>
278 Defaulting yields the <a>specified value</a>.
279 Every element has exactly one <a>specified value</a> per property.
281 <li>
282 Resolving value dependencies yields the <a>computed value</a>.
283 Every element has exactly one <a>computed value</a> per property.
285 <li>
286 Formatting the document yields the <a>used value</a>.
287 An element only has a <a>used value</a> for a given property
288 if that property applies to the element.
290 <li>
291 Finally, the used value is transformed to the <a>actual value</a>
292 based on constraints of the display environment.
293 As with the <a>used value</a>, there may or may not be an <a>actual value</a>
294 for a given property on an element.
295 </ul>
297 <h3 id="declared">
298 Declared Values</h3>
300 Each property declaration <a href="#filtering">applied to an element</a>
301 contributes a <dfn export>declared value</dfn> for that property
302 associated with the element.
303 See <a href="#filtering">Filtering Declarations</a> for details.
305 These values are then processed by the <a>cascade</a>
306 to choose a single âwinning valueâ.
308 <h3 id="cascaded">
309 Cascaded Values</h3>
311 The <dfn export>cascaded value</dfn> represents the result of <a href="#cascade">the cascade</a>:
312 it is the <a>declared value</a> that wins the cascade
313 (is sorted first in the <a>output of the cascade</a>).
314 If the <a>output of the cascade</a> is an empty list,
315 there is no <a>cascaded value</a>.
317 <h3 id="specified">
318 Specified Values</h3>
320 The <dfn export>specified value</dfn> the value of a given property that the style sheet authors intended for that element.
321 It is the result of putting the <a>cascaded value</a> through the <a href="#defaulting">defaulting</a> processes,
322 guaranteeing that a <a>specified value</a> exists for every property on every element.
324 In many cases, the <a>specified value</a> is the <a>cascaded value</a>.
325 However, if there is no <a>cascaded value</a> at all,
326 the <a>specified value</a> is <a href="#defaulting">defaulted</a>.
327 The ''initial'' and ''inherit'' keywords
328 are handled specially when they are the <a>cascaded value</a> of a property,
331 <h3 id="computed">
332 Computed Values</h3>
334 The <dfn export>computed value</dfn> is the result of resolving the <a>specified value</a>
335 as defined in the âComputed Valueâ line of the property definition table,
336 generally absolutizing it in preparation for <a>inheritance</a>.
338 Note: The <a>computed value</a> is the value that is transferred from parent to child during <a>inheritance</a>.
339 For historical reasons,
340 it is not necessarily the value returned by the <code>getComputedStyle()</code> function.
342 <div class="example">
343 A <a>specified value</a> can be either absolute (i.e., not relative to another value, as in ''red'' or ''2mm'')
344 or relative (i.e., relative to another value, as in ''auto'', ''2em'').
345 Computing a relative value generally absolutizes it:
347 <ul>
348 <li>
349 values with relative units
350 (''em'', ''ex'', ''vh'', ''vw'')
351 must be made absolute by multiplying with the appropriate reference size
353 <li>
354 certain keywords
355 (e.g., ''smaller'', ''bolder'')
356 must be replaced according to their definitions
358 <li>
359 percentages on some properties must be multiplied by a reference value
360 (defined by the property)
362 <li>
363 valid relative URLs must be resolved to become absolute.
364 </ul>
366 See examples (f), (g) and (h) in the <a href="#stages-examples">table below</a>.
367 </div>
369 Note: In general, the <a>computed value</a> resolves the <a>specified value</a>
370 as far as possible without laying out the document
371 or performing other expensive or hard-to-parallelize operations,
372 such as resolving network requests
373 or retrieving values other than from the element and its parent.
375 The <a>computed value</a> exists even when the property does not apply
376 (as defined by the âApplies Toâ line).
377 However, some properties may change how they determine the <a>computed value</a>
378 based on whether the property applies to the element.
380 <h3 id='used'>
381 Used Values</h3>
383 The <dfn export>used value</dfn> is the result of taking the <a>computed value</a>
384 and completing any remaining calculations to make it the absolute theoretical value
385 used in the layout of the document.
386 If the property does not apply to this element,
387 then the element has no <a>used value</a> for that property.
389 <p class='example'>
390 For example, a declaration of ''width: auto'' can't be resolved into a length without knowing the layout of the element's ancestors,
391 so the <a>computed value</a> is ''auto'',
392 while the <a>used value</a> is an absolute length, such as ''100px''. [[CSS21]]
394 <p class='example'>
395 As another example, a <code><div></code> might have a computed 'break-before' value of ''auto'',
396 but acquire a used 'break-before' value of ''break-before/page'' by propagation from its first child. [[CSS3-BREAK]]
398 <p class='example'>
399 Lastly, if a property does not apply to an element,
400 it has no <a>used value</a>;
401 so, for example, the 'flex' property has no <a>used value</a>
402 on elements that aren't <a>flex items</a>.
404 <h3 id="actual">
405 Actual Values</h3>
407 A <a>used value</a> is in principle ready to be used,
408 but a user agent may not be able to make use of the value in a given environment.
409 For example, a user agent may only be able to render borders with integer pixel widths
410 and may therefore have to approximate the <a lt="used value">used</a> width.
411 Also, the font size of an element may need adjustment based on the availability of fonts
412 or the value of the 'font-size-adjust' property.
413 The <dfn export>actual value</dfn> is the used value after any such adjustments have been made.
415 Note: By probing the actual values of elements,
416 much can be learned about how the document is laid out.
417 However, not all information is recorded in the actual values.
418 For example, the actual value of the 'page-break-after' property
419 does not reflect whether there is a page break or not after the element.
420 Similarly, the actual value of 'orphans'
421 does not reflect how many orphan lines there is in a certain element.
422 See examples (j) and (k) in the <a href="#stages-examples">table below</a>.
424 <h3 id="stages-examples">
425 Examples</h3>
427 <table class="data">
428 <thead>
429 <tr>
430 <th>
431 <th>Property
432 <th>Winning declaration
433 <th>Cascaded value
434 <th>Specified value
435 <th>Computed value
436 <th>Used value
437 <th>Actual value
438 <tbody>
439 <tr>
440 <td>(a)
441 <th>'text-align'
442 <td><code class="declaration">text-align: left</code>
443 <td><css>left</css>
444 <td><css>left</css>
445 <td><css>left</css>
446 <td><css>left</css>
447 <td><css>left</css>
448 <tr>
449 <td>(b)
450 <th>'border-top-width', 'border-right-width', 'border-bottom-width', 'border-left-width'
451 <td><code class="declaration">border-width: inherit</code>
452 <td><css>inherit</css>
453 <td class="say"><css>4.2px</css>
454 <td><css>4.2px</css>
455 <td><css>4.2px</css>
456 <td><css>4px</css>
457 <tr>
458 <td>(c)
459 <th>'width'
460 <td><small>(none)</small>
461 <td><small>(none)</small>
462 <td><css>auto</css> <small>(initial value)</small>
463 <td><css>auto</css>
464 <td><css>120px</css>
465 <td><css>120px</css>
466 <tr>
467 <td>(d)
468 <th>'list-style-position'
469 <td><code class="declaration">list-style-position: inherit</code>
470 <td><css>inherit</css>
471 <td class="say"><css>inside</css>
472 <td><css>inside</css>
473 <td><css>inside</css>
474 <td><css>inside</css>
475 <tr>
476 <td>(e)
477 <th>'list-style-position'
478 <td><code class="declaration">list-style-position: initial</code>
479 <td><css>initial</css>
480 <td><css>outside</css> <small>(initial value)</small>
481 <td><css>outside</css>
482 <td><css>outside</css>
483 <td><css>outside</css>
484 <tr>
485 <td>(f)
486 <th>'font-size'
487 <td><code class="declaration">font-size: 1.2em</code>
488 <td><css>1.2em</css>
489 <td><css>1.2em</css>
490 <td class="say"><css>14.1px</css>
491 <td><css>14.1px</css>
492 <td><css>14px</css>
493 <tr>
494 <td>(g)
495 <th>'width'
496 <td><code class="declaration">width: 80%</code>
497 <td><css>80%</css>
498 <td><css>80%</css>
499 <td><css>80%</css>
500 <td class="say"><css>354.2px</css>
501 <td><css>354px</css>
502 <tr>
503 <td>(h)
504 <th>'width'
505 <td><code class="declaration">width: auto</code>
506 <td><css>auto</css>
507 <td><css>auto</css>
508 <td><css>auto</css>
509 <td class="say"><css>134px</css>
510 <td><css>134px</css>
511 <tr>
512 <td>(i)
513 <th>'height'
514 <td><code class="declaration">height: auto</code>
515 <td><css>auto</css>
516 <td><css>auto</css>
517 <td><css>auto</css>
518 <td class="say"><css>176px</css>
519 <td><css>176px</css>
520 <tr>
521 <td>(j)
522 <th>'page-break-after'
523 <td><small>(none)</small>
524 <td><small>(none)</small>
525 <td><css>auto</css> <small>(initial value)</small>
526 <td><css>auto</css>
527 <td><css>auto</css>
528 <td><css>auto</css>
529 <tr>
530 <td>(k)
531 <th>'orphans'
532 <td><code class="declaration">orphans: 3</code>
533 <td><css>3</css>
534 <td><css>3</css>
535 <td><css>3</css>
536 <td><css>3</css>
537 <td><css>3</css>
538 </table>
542 <h2 id='filtering'>
543 Filtering</h2>
545 In order to find the <a>declared values</a>,
546 implementations must first identify all declarations that apply to each element.
547 A declaration applies to an element if:
549 <ul>
550 <li>
551 It belongs to a style sheet that currently applies to this document.
553 <li>
554 It is not qualified by a conditional rule [[!CSS3-CONDITIONAL]] with a false condition.
556 <li>
557 It belongs to a style rule whose selector matches the element. [[!SELECT]]
558 (Taking <a href="http://www.w3.org/TR/selectors4/#scoping">scoping</a> into account, if necessary.)
560 <li>
561 It is syntactically valid:
562 the declaration's property is a known property name,
563 and the declaration's value matches the syntax for that property.
564 </ul>
566 The values of the declarations that apply form,
567 for each property on each element,
568 a list of <a>declared values</a>.
569 The next section,
570 the <a>cascade</a>,
571 prioritizes these lists.
574 <h2 id='cascading'>
575 Cascading</h2>
577 The <dfn export>cascade</dfn>
578 takes a unordered list of <a>declared values</a>
579 for a given property on a given element,
580 sorts them by their declarationâs precedence as determined below,
581 and outputs a single <a>cascaded value</a>.
583 The cascade sorts declarations according to the following criteria,
584 in descending order of priority:
586 <dl>
587 <dt id='cascade-origin'>Origin and Importance
588 <dd>
589 The <a>origin</a> of a declaration is based on where it comes from
590 and its <a lt="important">importance</a> is whether or not it is declared ''!important'' (see below).
591 The precedence of the various <a>origins</a> is, in descending order:
593 <ol>
594 <li>Transition declarations [[!CSS3-TRANSITIONS]]
595 <li>Important user agent declarations
596 <li>Important user declarations
597 <li>Important override declarations [[!DOM-LEVEL-2-STYLE]]
598 <li>Important author declarations
599 <li>Animation declarations [[!CSS3-ANIMATIONS]]
600 <li>Normal override declarations [[!DOM-LEVEL-2-STYLE]]
601 <li>Normal author declarations
602 <li>Normal user declarations
603 <li>Normal user agent declarations
604 </ol>
606 Declarations from <a>origins</a> earlier in this list win over declarations from later <a>origins</a>.
608 <dt id='cascade-scope'>Scope
609 <dd>
610 A declaration can be <dfn export>scoped</dfn> to a subtree of the document
611 so that it only affects its <dfn export>scoping element</dfn> and that element's descendants.
612 For example, [[HTML5]] defines scoped <code><style></code> elements,
613 whose style sheets are scoped to the element's parent.
615 If the <a>scoping elements</a> of two declarations
616 have an ancestor/descendant relationship,
617 then for normal rules the declaration whose <a>scoping element</a> is the descendant wins,
618 and for important rules the declaration whose <a>scoping element</a> is the ancestor wins.
620 Note: In other words, for normal declarations the inner scope's declarations override,
621 but for ''!important'' rules <em>outer</em> scope's override.
623 For the purpose of this step,
624 all unscoped declarations are considered to be <a>scoped</a> to the root element.
625 Normal declarations from style attributes
626 are considered to be <a>scoped</a> to the element with the attribute,
627 whereas important declarations from style attributes
628 are considered to be <a>scoped</a> to the root element.
629 [[!CSSSTYLEATTR]]
631 Note: This odd handling of ''!important'' style attribute declarations
632 is to match the behavior defined in CSS Levels 1 and 2,
633 where style attributes simply have higher specificity than any other author rules. [[CSS21]]
635 <dt id='cascade-specificity'>Specificity
636 <dd>
637 The <a href="http://www.w3.org/TR/selectors/#specificity">Selectors module</a> [[!SELECT]] describes how to compute the specificity of a selector.
638 Each declaration has the same specificity as the style rule it appears in.
639 For the purpose of this step,
640 declarations that do not belong to a style rule
641 (such as the <a href="http://www.w3.org/TR/css-style-attr/#interpret">contents of a style attribute</a>)
642 are considered to have a specificity higher than any selector.
643 The declaration with the highest specificity wins.
646 <dt id='cascade-order'>Order of Appearance
647 <dd>
648 The last declaration in document order wins.
649 For this purpose:
651 <ul>
652 <li>Declarations from <a at-rule lt="@import">imported style sheets</a>
653 are ordered as if their style sheets were substituted in place of the ''@import'' rule.
654 <li>Declarations from style sheets independently linked by the originating document
655 are treated as if they were concatenated in linking order,
656 as determined by the host document language.
657 <li>Declarations from style attributes
658 are ordered according to the document order of the element the style attribute appears on,
659 and are all placed after any style sheets.
660 </ul>
661 </dl>
663 The <dfn>output of the cascade</dfn>
664 is a (potentially empty) sorted list of <a>declared values</a> for each property on each element.
667 <h3 id='cascading-origins'>
668 Cascading Origins</h3>
670 Each style rule has an <dfn export>origin</dfn>,
671 which determines where it enters the cascade.
672 CSS defines three core <a>origins</a>:
674 <dl>
675 <dt id='cascade-origin-author'>Author
676 <dd>
677 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.
679 <dt id='cascade-origin-user'>User
680 <dd>
681 The user may be able to specify style information for a particular document.
682 For example, the user may specify a file that contains a style sheet
683 or the user agent may provide an interface that generates a user style sheet
684 (or behaves as if it did).
686 <dt id='cascade-origin-ua'>User agent
687 <dd>
688 Conforming user agents must apply a default style sheet
689 (or behave as if they did).
690 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
691 (e.g., for visual browsers, the EM element in HTML is presented using an italic font).
692 See e.g. the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/rendering.html#the-css-user-agent-style-sheet-and-presentational-hints">HTML user agent style sheet</a>. [[HTML5]]
693 </dl>
695 Extensions to CSS define the following additional <a>origins</a>:
697 <dl>
698 <dt id='cascade-origin-override'>Override
699 <dd>
700 DOM Level 2 Style [[!DOM-LEVEL-2-STYLE]] defines an interface for âoverrideâ styles,
701 which enter the cascade at a higher level than other author rules.
703 <dt id='cascade-origin-animation'>Animation
704 <dd>
705 CSS Animations [[CSS3-ANIMATIONS]] generate âvirtualâ rules representing their effects when running.
707 <dt id='cascade-origin-transition'>Transition
708 <dd>
709 Like CSS Animations, CSS Transitions [[CSS3-TRANSITIONS]] generate âvirtualâ rules representing their effects when running.
710 </dl>
712 <h3 id='importance'>
713 Important Declarations: the ''!important'' annotation</h3>
715 CSS attempts to create a balance of power between author and user style sheets.
716 By default, rules in an author's style sheet override those in a user's style sheet,
717 which override those in the user-agent's default style sheet.
718 To balance this, a declaration can be made <dfn export>important</dfn>,
719 which increases its weight in the cascade and inverts the order of precedence.
721 A declaration is <a>important</a> if it has a <dfn export id='bang-important'>!important</dfn> annotation,
722 as defined by [[CSS3SYN]].
723 i.e. if the last two (non-whitespace, non-comment) tokens
724 in its value are the delimiter token ''!'' followed by the identifier token ''important''.
726 <div class='example'>
727 <pre>[hidden] { display: none !important; }</pre>
728 </div>
730 An <a>important</a> declaration takes precedence over a normal declaration.
731 Author and user style sheets may contain ''!important'' declarations,
732 with user ''!important'' declarations overriding author ''!important'' declarations.
733 This CSS feature improves accessibility of documents by giving users with special requirements
734 (large fonts, color combinations, etc.)
735 control over presentation.
737 Important declarations from all origins take precedence over animations.
738 This allows authors to override animated values in important cases.
739 (Animated values normally override all other rules.)
740 [[CSS3-ANIMATIONS]]
742 User agent style sheets may also contain ''!important'' declarations.
743 These override all author and user declarations.
745 <div class='example'>
746 The first rule in the user's style sheet in the following example contains an ''!important'' declaration,
747 which overrides the corresponding declaration in the author's style sheet.
748 The declaration in the second rule will also win due to being marked ''!important''.
749 However, the third declaration in the user's style sheet is not ''!important''
750 and will therefore lose to the second rule in the author's style sheet
751 (which happens to set style on a <a>shorthand</a> property).
752 Also, the third author rule will lose to the second author rule since the second declaration is ''!important''.
753 This shows that ''!important'' declarations have a function also within author style sheets.
755 <pre>
756 /* From the user's style sheet */
757 p { text-indent: 1em !important }
758 p { font-style: italic !important }
759 p { font-size: 18pt }
761 /* From the author's style sheet */
762 p { text-indent: 1.5em !important }
763 p { font: normal 12pt sans-serif !important }
764 p { font-size: 24pt }
765 </pre>
766 </div>
768 <h3 id="preshint">
769 Precedence of Non-CSS Presentational Hints</h3>
771 The UA may choose to honor presentational hints in a source document's markup,
772 for example the <code>bgcolor</code> attribute or <code><s></code> element in [[HTML5]].
773 All document language-based styling must be translated to corresponding CSS rules
774 and either enter the cascade at the user agent level or
775 be treated as author level rules with a specificity of zero placed at the start of the author style sheet.
776 A document language may define whether a presentational hint enters at the UA or author level of the cascade;
777 if so, the UA must behave accordingly.
778 For example, [[SVG11]] maps its presentation attributes into the author level.
780 Note: Presentational hints entering the cascade at the UA level can be overridden by author or user styles.
781 Presentational hints entering the cascade at the author level can be overridden by author styles,
782 but not by non-''!important'' user styles.
783 Host languages should choose the appropriate level for presentational hints with these considerations in mind.
786 <h2 id="defaulting">
787 Defaulting</h2>
789 When the <a>cascade</a> does not result in a value,
790 the <a>specified value</a> must be found some other way.
791 <a>Inherited properties</a> draw their defaults from their parent element through <a>inheritance</a>;
792 all other properties take their <a>initial value</a>.
793 Authors can explicitly request inheritance or initialization
794 via the ''inherit'' and ''initial'' keywords.
796 <h3 id="initial-values">
797 Initial Values</h3>
799 Each property has an <dfn export>initial value</dfn>,
800 defined in the property's definition table.
801 If the property is not an <a>inherited property</a>,
802 and the <a>cascade</a> does not result in a value,
803 then the <a>specified value</a> of the property is its <a>initial value</a>.
805 <h3 id="inheriting">
806 Inheritance</h3>
808 <dfn export>Inheritance</dfn> propagates property values from parent elements to their children.
809 The <dfn export>inherited value</dfn> of a property on an element
810 is the <a>computed value</a> of the property on the element's parent element.
811 For the root element,
812 which has no parent element,
813 the <a>inherited value</a> is the <a>initial value</a> of the property.
815 (Pseudo-elements inherit according to a fictional tag sequence described for each pseudo-element [[!SELECT]].)
817 Some properties are <dfn export lt="inherited property">inherited properties</dfn>,
818 as defined in their property definition table.
819 This means that,
820 unless the cascade results in a value,
821 the value will be determined by inheritance.
823 A property can also be explicitly inherited. See the ''inherit'' keyword.
825 Note: Inheritance follows the document tree and is not intercepted by <a href="http://www.w3.org/TR/CSS21/visuren.html#box-gen">anonymous boxes</a>,
826 or otherwise affected by manipulations of the box tree.
828 <h3 id="defaulting-keywords">
829 Explicit Defaulting</h3>
831 Several CSS-wide property values are defined below;
832 declaring a property to have these values explicitly specifies a particular defaulting behavior.
833 As specified in <a href="http://www.w3.org/TR/css3-values/#common-keywords">CSS Values and Units Level 3</a> [[!CSS3VAL]],
834 all CSS properties can accept these values.
836 <h4 id="initial">
837 Resetting a Property: the ''initial'' keyword</h4>
839 If the <a>cascaded value</a> is the <dfn for=all>''initial''</dfn> keyword,
840 the property's <a>initial value</a> becomes its <a>specified value</a>.
842 <h4 id="inherit">
843 Explicit Inheritance: the ''inherit'' keyword</h4>
845 If the <a>cascaded value</a> of a property is the <dfn for=all>''inherit''</dfn> keyword,
846 the <a>inherited value</a> becomes the property's <a lt="specified value">specified</a> and <a>computed values</a>.
848 <h4 id="inherit-initial">
849 Erasing All Declarations: the ''unset'' keyword</h4>
851 If the <a>cascaded value</a> of a property is the <dfn for=all>''unset''</dfn> keyword,
852 then if it is an inherited property, this is treated as ''inherit'',
853 and if it is not, this is treated as ''initial''.
854 This keyword effectively erases all <a>declared values</a> occurring earlier in the <a>cascade</a>,
855 correctly inheriting or not as appropriate for the property
856 (or all longhands of a <a>shorthand</a>).
858 <h2 id="changes">
859 Changes</h2>
861 <h3 id="changes-2013">
862 Changes Since the 3 October 2013 Candidate Recommendation</h3>
864 The following changes were made to this specification since the
865 <a href="http://www.w3.org/TR/2013/CR-css-cascade-3-20131003/">3 October 2013 Candidate Recommendation</a>:
867 <ul>
868 <li>Defined <a>environment encoding</a> of imported style sheets.
869 <blockquote>
870 <ins><p>The <a>environment encoding</a> of an imported style sheet is the encoding of the style sheet that imported it. [[CSS3SYN]]</p></ins>
871 </blockquote>
872 <li>Referenced [[!CSS3SYN]] for syntax of ''!important'' rules.
873 <blockquote>
874 <p>A declaration is <a>important</a> if it has a ''!important'' annotation,
875 <ins>as defined by [[CSS3SYN]]</ins>.
876 </blockquote>
877 <li>Explained <a>reset-only sub-properties</a>
878 and clarified that they also get affected by a CSS-wide keyword value in the shorthand declaration.
879 <blockquote>
880 <p><ins>In other cases, a property might be a <a>reset-only sub-property</a> of the shorthand:
881 Like other <a>sub-properties</a>, it is reset to its initial value by the shorthand when unspecified,
882 but the shorthand might not include syntax to set the <a>sub-property</a>
883 to any of its other values.
884 For example, the 'border' shorthand resets 'border-image'
885 to its initial value of ''border-image/none'',
886 but has no syntax to set it to anything else. [[CSS3BG]]</ins></p>
888 <p>If a <a>shorthand</a> is specified as one of the <a href="http://www.w3.org/TR/css3-values/#common-keywords">CSS-wide keywords</a> [[!CSS3VAL]],
889 it sets all of its <a>sub-properties</a> to that keyword,
890 <ins>including any that are <a>reset-only sub-properties</a></ins>.
891 (Note that these keywords cannot be combined with other values in a single declaration, not even in a shorthand.)</p>
892 </blockquote>
894 </ul>
896 <p>A <a href="https://drafts.csswg.org/css-cascade-3/issues-cr-2013">Disposition of Comments</a> is available.
898 <h2 class="no-num" id="acknowledgments">Acknowledgments</h2>
900 David Baron, Simon Sapin, and Boris Zbarsky contributed to this specification.