-
Notifications
You must be signed in to change notification settings - Fork 791
Expand file tree
/
Copy pathcascade.src
More file actions
464 lines (376 loc) · 16.9 KB
/
cascade.src
File metadata and controls
464 lines (376 loc) · 16.9 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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html lang="en">
<!-- $Id: cascade.src,v 2.27 1998-04-30 17:45:58 howcome 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.
<li>Otherwise, if the property is <a
href="#inheritance">inherited</a>, use the value of the parent
element, generally the computed 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 a reference 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.
<P>In most cases, elements inherit computed values. However, there are
some properties whose specified value may be inherited (e.g., the
number value for the <span
class="propinst-line-height">'line-height'</span> property).
In the cases where child elements do not inherit the computed value,
this is described in the property definition.
<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 only be able to render borders with integer
pixel widths and may therefore have to approximate the computed
width. The actual value is the computed value after any approximations
have been applied.
<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.
<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> 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>
</div>
<P>Specified percentage values are not inherited; computed values are.
<div class="example"><P>
For example, given the following style sheet:</P>
<pre>
BODY { font-size: 10pt }
H1 { font-size: 120% }
</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 '12pt' (120% times
10pt, the parent's value). Since the computed value of <span
class="propinst-font-size">'font-size'</span> is inherited, the EM
element will have the computed value '12pt' as well. If the
user agent does not have the 12pt font available, the
actual value of <span class="propinst-font-size">'font-size'</span>
for both H1 and EM might be, for example, '11pt'.
</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="#computed-value">computed 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 from other style sheets. Any @import rules must
precede all rule sets in a style sheet. 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>
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 <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 author style sheets have more weight than
rules in user style sheets. Precedence is reversed, however, for
"!important" rules. All rules user and author rules have more weight
than rules in 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 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
<span class="index-def" title="important">"!important"</span>
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 of
the reader. It is therefore important that the user agent give 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 <span class="index-inst" title="shorthand
property">shorthand property</span> (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 overrides 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>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 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 3 of
the cascade algorithm, they are considered to have an ID selector
(specificity: a=1, b=0, c=0). For the purpose of step 4, they are
considered 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 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:
-->