-
Notifications
You must be signed in to change notification settings - Fork 790
Expand file tree
/
Copy pathcascade.src
More file actions
513 lines (415 loc) · 18.6 KB
/
cascade.src
File metadata and controls
513 lines (415 loc) · 18.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html lang="en">
<!-- $Id: cascade.src,v 2.20 1998-03-22 02:03:01 ijacobs Exp $ -->
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<TITLE>Assigning property values, Cascading, and Inheritance</TITLE>
</HEAD>
<BODY>
<H1 align="center">Assigning property values, Cascading, and Inheritance</H1>
<H2>Specified, computed, and actual values</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 three-step
calculation: the value is determined through specification (the
"specified value"), then resolved into an absolute value if necessary
(the "computed value"), and finally transformed according to
the limitations of the local environment (the "actual value").
<h3>
<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 a 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. <!-- If the value is 'inherit', use the value of the parent
element. -->
<li>Otherwise, if the property is <a
href="#inheritance">inherited</a>, use the value of the parent
element, usually the actual value.
<li>Otherwise use the property's <span class="index-def"
title="initial value">initial value</span>. The <a
name="initial-value">initial value</a> of each property is indicated
in the property's definition.
</ol>
<P>Since it has no parent, the root of the <a
href="conform.html#doctree">document tree</a> cannot use values from
the parent element; in this case, the initial value is used if
necessary.
<h3><span class="index-def" title="computed value">
<a name="computed-value">Computed values</a>
</span>
</h3>
<P>Specified values may be absolute (i.e., they are not specified
relative to another value, as in 'red' or '2mm') or relative (i.e.,
they are specified relative to another value, as in 'auto', '2em', and
'12%'). For absolute values, no computation is needed to find the
computed value.
<p>Relative values, on the other hand, must be transformed into
computed values: percentages must be multiplied by the referenced
actual value (each property defines which value that is), values with
relative units (em, ex, px) must be made absolute by multiplying with
the appropriate font or pixel size, 'auto' values must be computed by
the formulas given with each property, certain keywords ('smaller',
'bolder', 'inherit') must be replaced according to their definitions.
<h3><span class="index-def" title="actual value">
<a name="actual-value">Actual values</a></span></h3>
<P>A computed value is in principle ready to be used, but a user agent
may not be able to make use of the value in a given environment. For
example, a user agent may not have a computed font size available, in
which case the user agent must approximate the computed value. The
actual value is the computed value after any approximations have been
applied.
<P>In most cases, elements inherit actual values. However, there are
some properties whose specified or computed values may be inherited
(e.g., the number value for the <span
class="propinst-line-height">'line-height'</span> property). The
definition of a property whose actual value is not what other elements
inherit will clearly say so.
<H2><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>. Each property <a
href="about.html#property-defs">defines</a> whether it is inherited or
not.
<!--<P>All properties accept the 'inherit' keyword. For an inherited
property, this value is merely an explicit statement of the
inheritance. For a property that is not inherited according to its
definition, this value forces inheritance.
[Already in next section. BB] -->
<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>Other style properties are likewise inherited, e.g., <span
class="propinst-font-family">'font-family'</span> and <span
class="propinst-font-size">'font-size'</span>.
-->
<!-- <P>The root of the document tree cannot inherit values.
[Already stated under "specified values". BB]-->
<P> To set a "default" style property for a document, authors may set
the property on the root of the document tree. In HTML, for example,
the HTML or BODY elements can serve this function. Note that this will
work even if the author omits the BODY tag in the HTML source since
the HTML parser will infer the missing tag.
<div class="example"><P>
For example, since the <span class="propinst-color">'color'</span>
property is inherited, all descendants of the BODY element will
inherit the color 'black':
<PRE>
BODY { color: black; }
</PRE>
<!--
<P>Not all style properties are inherited, for example <span
class="propinst-background">'background'</span>. But in the case of
<span class="propinst-background">'background'</span>, the <em>visual
effect</em> may be propagated to descendants. A
background specified for the root element, as in:</p>
<PRE>
BODY { background: url(texture.gif) white; }
</PRE>
<P>will shine through any descendants unless they specify a value for
<span class="propinst-background-color">'background-color'</span>
other than 'transparent' (the initial value).
[Why explain that here? BB]-->
</div>
<div class="example"><P>
The following example illustrates that <a href="#specified-value">
specified</a> percentage values are not inherited; only
<a href="#actual-value">actual values</a> are inherited. Consider
the style sheet:
<pre>
BODY { font-size: 10pt }
H1 { font-size: 120% }
</pre>
<p>and the document fragment:
<pre class="html-example">
<BODY>
<H1>A <EM>large</EM> heading</H1>
</BODY>
</pre>
<p>The <a href="#computed-value">computed value</a> of the <span
class="prop-inst-font-size">'font-size'</span> property for the H1
element is 12pt (120% times 10pt). If the user agent has the
appropriate 12pt font available, 12pt will also be the property's <a
href="#actual-value">actual value</a> and the EM will inherit that
value for the <span class="propinst-font-size">'font-size'</span>
property. However, if the user agent does not have the 12pt font
available, it may assign an actual value of, for example, 11pt to the
<span class="propinst-font-size">'font-size'</span> property of the H1
element. In that case, the EM will inherit a value of 11pt for the
same property.
</div>
<h3>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 specified value of 'inherit', which
means that, for a given element, the property takes the same <a
href="#actual-value">actual value</a> as the property for the
element's parent. The inherited value, which is normally only used as
a fallback value, can be strengthened by setting 'inherit' explicitly.
<div class="example"><P>In the example below, the <span
class="propinst-color">'color'</span> and <span
class="propinst-background">'background'</span> 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;
}
</PRE>
</div>
<!-- Add another example?. Check out:
http://lists.w3.org/Archives/Member/w3c-css-wg/1998JanMar/0054.html
-IJ, HWL -->
<H2><a name="at-import">The @import rule</a></h2>
<P>The <span class="index-def"
title="@import"><dfn>'@import'</dfn></span> rule allows users to import
style rules in other style sheets. The '@import' keyword must be
followed by the URI of the style sheet to include. Any @import rules
must precede all rule sets in a style sheet.
<div class="example"><P>
Both of these import rules are valid; the
first one illustrates that the "url()" syntax may be replaced by a
string in the
@import rule:</p>
<PRE>
@import "mystyle.css";
@import url(herstyle.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 <span class="index-inst"
title="@import">@import</span> rules. These <span class="index-def"
title="conditional import|media-dependent import">conditional
imports</span> specify comma-separated media types after the URI.
<div class="example">
<p>The following rules have the same effect as if the imported style
sheet were wrapped in an @media rule for the same media, but it may
save the UA a fruitless download.
<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.
<H2><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 behave 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) prior to all other style sheets for a
document. 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
4.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 are
modified by the user (e.g., system colors). However, due to
limitations in a user agent's internal implementation, it may be
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 <span class="index-def" title="cascade">cascade</span>
assigns a weight to each style rule. When several rules apply, the one
with the greatest weight takes precedence.
<P>By default, rules in a user's personal style sheets have less
weight than rules in the author's style sheets. Thus, if there are
conflicts between the style sheets of an incoming document and the
reader's personal sheets, the author's rules will be used. Both reader
and author rules override the UA's default style sheet.
<P>Imported style sheets also cascade and their weight depends on
their import order. Rules specified in a given style sheet override
rules imported from other style sheets. Imported style sheets can
themselves import and override other style sheets, recursively, and
the same precedence rules apply.
<H3><a name="cascading-order">Cascading order</a></H3>
<P>To find the value for an element/property combination, user agents
must apply the following algorithm:</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. If no
declarations apply, terminate the algorithm.
<LI>
Sort the declarations by weight and origin: For normal
declarations, author style sheets override
user style sheets which override the default style sheet. For
<span class="index-def" title="important">"!important"</span>
declarations, user style sheets override author style sheets
which override the default style sheet.
An imported style sheet has the same
origin as the style sheet that imported it.
<LI>
Sort by specificity of selector: more specific selectors will
override more general ones. <!-- The definition and calculation
of specificity depends on the document language (see below). [I
don't think so. BB]-->
Pseudo-elements and pseudo-classes are counted as normal
elements and classes, respectively.
<LI> Sort by order specified: if two rules have the same weight,
the latter specified wins. Rules in imported style sheets are
considered to be before any rules in the style sheet itself.
</OL>
<P>The search for the property value must be terminated when any of
the above steps yields a rule that has a higher weight than the other
rules that apply to the same element/property combination.
<!--
<P>Note that "!important" may occur in author's and UA's style sheets,
but only has an effect in the user's style sheets.
[nope, Chris Wilson wanted it to count, HWL]
-->
<P> 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 gives the
user the ability to turn off the influence of a certain style sheet,
e.g., through a pull-down menu.
<H3><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 keywords "!"
and "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.
<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 shorthand property (e.g., <span
class="propinst-background">'background'</span>) to be "!important" is
equivalent to declaring all of its sub-properties to be "!important".
<div class="example">
<P> The first rule in the user's style sheet in the following example
contains an "!important" declaration, which override the corresponding
declaration in the author's styles 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: 12pt sans-serif !important }
P { font-size: 24pt }
</PRE>
</div>
<H3><a name="specificity">Calculating a selector's specificity</a></H3>
<P>A selector's specificity is calculated as follows:</p>
<ul>
<li>(a) count the number of ID attributes in the selector
<li>(b) count the number of other attributes and pseudo-classes
in the selector
<li>(c) count the number of element names in the selector
<li>(d) ignore pseudo-elements.
</ul>
<P>Concatenating the three numbers (in a number system with a large
base) gives the specificity.
<div class="example"><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 sheet
rules. These rules have no selectors, but for the purpose of step 4 of
the cascade algorithm, they are considered to have an ID selector
(specificity = 100).
<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 be
red. Although the specificity is the same for both declarations, the
declaration in the "style" attribute will override the one in the
STYLE element because of cascading rule 4.
</div>
<H3>Precedence of non-CSS presentational hints</H3>
<P>The UA may choose to honor presentational hints from other sources
than style sheets, for example the FONT element or the "align"
attribute in HTML. If so, the non-CSS presentational hints must be
translated to the corresponding CSS rules with specificity equal to
zero. The rules are assumed to be at the start of the author style sheet
and 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 for
stylistic 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 specificity
equal to 1, not 0. The change is due to the introduction of the
universal selector which has a specificity of 0.
</em>
</div>
</BODY>
</html>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-declaration:"~/SGML/HTML4.decl"
sgml-default-doctype-name:"html"
sgml-minimize-attributes:t
sgml-nofill-elements:("pre" "style" "br")
sgml-live-element-indicator:t
End:
-->