1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
5
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6
<TITLE>Introduction to CSS2</TITLE>
7
<link rel="stylesheet" href="style/default.css" type="text/css">
8
<link rel="prev" href="about.html">
9
<link rel="next" href="conform.html">
10
<link rel="contents" href="cover.html#minitoc">
11
<link rel="CSS-properties" href="propidx.html" title="properties">
12
<link rel="index" href="indexlist.html" title="index">
15
<div class="navbar" align="center">
16
<p><a href="about.html">previous</a>
17
<a href="conform.html">next</a>
18
<a href="cover.html#minitoc">contents</a>
19
<a href="propidx.html">properties</a>
20
<a href="indexlist.html">index</a>
24
<H1 align="center">2 Introduction to CSS2</H1>
26
<p><strong>Contents</strong>
28
<li class="tocline2"><a href="intro.html#q1" class="tocxref">2.1 A brief CSS2 tutorial for HTML</a>
29
<li class="tocline2"><a href="intro.html#q2" class="tocxref">2.2 A brief CSS2 tutorial for XML</a>
30
<li class="tocline2"><a href="intro.html#processing-model" class="tocxref">2.3 The CSS2 processing model</a>
32
<li class="tocline3"><a href="intro.html#q4" class="tocxref">2.3.1 The canvas</a>
33
<li class="tocline3"><a href="intro.html#q5" class="tocxref">2.3.2 CSS2 addressing model</a>
35
<li class="tocline2"><a href="intro.html#q6" class="tocxref">2.4 CSS design principles</a>
39
<H2><a name="q1">2.1 A brief CSS2 tutorial for HTML</a></H2>
41
<P> In this tutorial, we show how easy it can be to design simple
42
style sheets. For this tutorial, you will need to know a little HTML
43
(see <a href="refs.html#ref-HTML40" rel="biblioentry" class="noxref"><span class="informref">[HTML40]</span></a>) and some basic desktop publishing terminology.
45
<P>We begin with a small HTML document:</p>
47
<PRE class="html-example">
48
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
51
<TITLE>Bach's home page</TITLE>
54
<H1>Bach's home page</H1>
55
<P>Johann Sebastian Bach was a prolific composer.
60
<P>To set the text color of the H1 elements to blue, you can write the
61
following CSS rule:</P>
67
<P>A CSS rule consists of two main parts: <a
68
href="selector.html">selector</a> ('H1') and declaration ('color:
69
blue'). The declaration has two parts: property ('color') and value
70
('blue'). While the example above tries to influence only one of the
71
properties needed for rendering an HTML document, it qualifies as a
72
style sheet on its own. Combined with other style sheets (one
73
fundamental feature of CSS is that style sheets are combined) it will
74
determine the final presentation of the document.
76
<P> The HTML 4.0 specification defines how style sheet rules may be
77
specified for HTML documents: either within the HTML document, or via
78
an external style sheet. To put the style sheet into the document, use
79
the STYLE element:</p>
81
<PRE class="html-example">
82
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
85
<TITLE>Bach's home page</TITLE>
86
<STYLE type="text/css">
91
<H1>Bach's home page</H1>
92
<P>Johann Sebastian Bach was a prolific composer.
97
<P> For maximum flexibility, we recommend that authors specify
98
external style sheets; they may be changed without modifying the
99
source HTML document, and they may be shared among several
100
documents. To link to an external style sheet, you can use the LINK
103
<PRE class="html-example">
104
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
107
<TITLE>Bach's home page</TITLE>
108
<LINK rel="stylesheet" href="bach.css" type="text/css">
111
<H1>Bach's home page</H1>
112
<P>Johann Sebastian Bach was a prolific composer.
117
<P>The LINK element specifies:</p>
120
<li> the type of link: to a "stylesheet".
121
<li> the location of the style sheet via the "ref" attribute.
122
<li>the type of style sheet being linked: "text/css".
125
<P>To show the close relationship between a style sheet and the
126
structured markup, we continue to use the STYLE element in this
127
tutorial. Let's add more colors:
129
<PRE class="html-example">
130
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
133
<TITLE>Bach's home page</TITLE>
134
<STYLE type="text/css">
140
<H1>Bach's home page</H1>
141
<P>Johann Sebastian Bach was a prolific composer.
146
<P>The style sheet now contains two rules: the first one sets the
147
color of the BODY element to 'red', while the second one sets the
148
color of the H1 element to 'blue'. Since no color value has been
149
specified for the P element, it will inherit the color from its parent
150
element, namely BODY. The H1 element is also a child element of BODY
151
but the second rule overrides the inherited value. In CSS there are
152
often such conflicts between different values, and this specification
153
describes how to resolve them.
155
<P>CSS2 has more than 100 different properties, including <a href="colors.html#propdef-color" class="noxref"><span
156
class="propinst-color">'color'</span></a>. Let's look at some of the
159
<PRE class="example">
160
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
163
<TITLE>Bach's home page</TITLE>
164
<STYLE type="text/css">
166
font-family: "Gill Sans", sans-serif;
173
<H1>Bach's home page</H1>
174
<P>Johann Sebastian Bach was a prolific composer.
179
<P>The first thing to notice is that several declarations are grouped
180
within a block enclosed by curly braces ({...}), and separated by
181
semicolons, though the last declaration may also be followed by a
184
<P>The first declaration on the BODY element sets the font family to
185
"Gill Sans". If that font isn't available, the user agent (often
186
referred to as a "browser") will use the 'sans-serif' font family
187
which is one of five generic font families which all users agents
188
know. Child elements of BODY will inherit the value of the <a href="fonts.html#propdef-font-family" class="noxref"><span
189
class="propinst-font-family">'font-family'</span></a> property.
191
<P>The second declaration sets the font size of the BODY element to
192
12 points. The "point" unit is commonly used in print-based typography
193
to indicate font sizes and other length values. It's an example of an
194
absolute unit which does not scale relative to the environment.
196
<P>The third declaration uses a relative unit which scales with regard
197
to its surroundings. The "em" unit refers to the font size of the
198
element. In this case the result is that the margins around the BODY
199
element are three times wider than the font size.
201
<H2><a name="q2">2.2 A brief CSS2 tutorial for XML</a></H2>
203
<P>CSS can be used with any structured document format, for example
204
with applications of the eXtensible Markup Language <a href="refs.html#ref-XML10" rel="biblioentry" class="noxref"><span class="informref">[XML10]</span></a>. In
205
fact, XML depends more on style sheets than HTML, since authors can
206
make up their own elements that user agents don't know how to
209
<P>Here is a simple XML fragment:
211
<PRE class="xml-example">
213
<HEADLINE>Fredrick the Great meets Bach</HEADLINE>
214
<AUTHOR>Johann Nikolaus Forkel</AUTHOR>
216
One evening, just as he was getting his
217
<INSTRUMENT>flute</INSTRUMENT> ready and his
218
musicians were assembled, an officer brought him a list of
219
the strangers who had arrived.
224
<P>To display this fragment in a document-like fashion, we must first
225
declare which elements are inline-level (i.e., do not cause line breaks) and
226
which are block-level (i.e., cause line breaks).
228
<PRE class="example">
229
INSTRUMENT { display: inline }
230
ARTICLE, HEADLINE, AUTHOR, PARA { display: block }
233
<P>The first rule declares INSTRUMENT to be inline and the second
234
rule, with its comma-separated list of selectors, declares all the
235
other elements to be block-level.
237
<P>One proposal for linking a style sheet to an XML document is to use
238
a processing instruction:
240
<PRE class="xml-example">
241
<?XML:stylesheet type="text/css" href="bach.css"?>
243
<HEADLINE>Fredrick the Great meets Bach</HEADLINE>
244
<AUTHOR>Johann Nikolaus Forkel</AUTHOR>
246
One evening, just as he was getting his
247
<INSTRUMENT>flute</INSTRUMENT> ready and his
248
musicians were assembled, an officer brought him a list of
249
the strangers who had arrived.
254
<P>A visual user agent could format the above example as:
257
<p><img src="images/bach1.gif" alt="Example rendering"><SPAN class="dlink"> <A name="img-bach1" href="images/longdesc/bach1-desc.html" title="Long description for the first Bach/XML formatting example">[D]</A></SPAN>
260
<P>Notice that the word "flute" remains within the paragraph since it
261
is the content of the inline element INSTRUMENT.
263
<P>Still, the text isn't formatted the way you would expect. For
264
example, the headline font size should be larger than then rest of the
265
text, and you may want to display the author's name in italic:</P>
267
<PRE class="example">
268
INSTRUMENT { display: inline }
269
ARTICLE, HEADLINE, AUTHOR, PARA { display: block }
270
HEADLINE { font-size: 1.3em }
271
AUTHOR { font-style: italic }
272
ARTICLE, HEADLINE, AUTHOR, PARA { margin: 0.5em }
275
<P>A visual user agent could format the above example as:
278
<p><img src="images/bach2.gif" alt="Example rendering"><SPAN class="dlink"> <A name="img-bach2" href="images/longdesc/bach2-desc.html" title="Long description for the second Bach/XML formatting example">[D]</A></SPAN>
281
<P>Adding more rules to the style sheet will allow you to further
282
improve the presentation of the document.
284
<H2>2.3 <a name="processing-model">The CSS2 processing model</a></H2>
286
<P>This section presents one possible model of how user
287
agents that support CSS work. This is only a conceptual model; real
288
implementations may vary.
290
<P>In this model, a user agent processes a source
291
by going through the following steps:</P>
294
<li>Parse the source document and create a <a
295
href="conform.html#doctree">document tree</a>.</li>
297
<li>Identify the target <a href="media.html">media type</a>.
299
<li>Retrieve all style sheets associated with the document that are
300
specified for the target <a href="media.html">media type</a>.
302
<li>Annotate every element of the document tree by assigning a single
303
value to every <a href="syndata.html#properties">property</a> that is
304
applicable to the target <a href="media.html">media type</a>.
305
Properties are assigned values according to the mechanisms described
306
in the section on <a href="cascade.html">cascading and
309
<P>Part of the calculation of values depends on the formatting
310
algorithm appropriate for the target <a href="media.html">media
311
type</a>. For example, if the target medium is the screen, user agents
312
apply the <a href="visuren.html">visual formatting model</a>. If the
313
destination medium is the printed page, user agents apply the <a
314
href="page.html#page-model">page model</a>. If the destination medium
315
is an aural rendering device (e.g., speech synthesizer), user agents
316
apply the <a href="aural.html">aural rendering model</a>.
318
<li>From the annotated document tree, generate a
319
<span class="index-def" title="formatting structure">
320
<a name="formatting-structure"><dfn>formatting
321
structure</dfn></a></span>. Often, the formatting structure closely
322
resembles the document tree, but it may also differ significantly,
323
notably when authors make use of pseudo-elements and generated content.
324
First, the formatting structure need not be "tree-shaped" at all -- the
325
nature of the structure depends on the implementation. Second, the
326
formatting structure may contain more or less information than the
327
document tree. For instance, if an element in the document tree has a
328
value of 'none' for the <a href="visuren.html#propdef-display" class="noxref"><span
329
class="propinst-display">'display'</span></a> property, that element will
330
generate nothing in the formatting structure. A list element, on the
331
other hand, may generate more information in the formatting structure:
332
the list element's content and list style information (e.g., a bullet
335
<P>Note that the CSS user agent does not alter the document tree
336
during this phase. In particular, content generated due to style
337
sheets is not fed back to the document language processor (e.g., for
340
<li>Transfer the formatting structure to the target medium (e.g., print
341
the results, display them on the screen, render them as speech,
345
<P>Step 1 lies outside the scope of this specification (see, for
346
example, <a href="refs.html#ref-DOM" rel="biblioentry" class="noxref"><span class="informref">[DOM]</span></a>).
348
<P>Steps 2-5 are addressed by the bulk of this specification.
350
<P>Step 6 lies outside the scope of this specification.</p>
352
<h3><a name="q4">2.3.1 The canvas</a></h3>
354
<P>For all media, the term <span class="index-def" title="canvas"> <a
355
name="canvas"><dfn>canvas</dfn></a></span> describes "the space where
356
the formatting structure is rendered." The canvas is infinite for each
357
dimension of the space, but rendering generally occurs within
358
a finite region of the canvas, established by the user agent
359
according to the target medium. For instance, user agents rendering
360
to a screen generally impose a minimum width and choose an initial
361
width based on the dimensions of the <a href="visuren.html#viewport">
362
viewport</a>. User agents rendering to a page generally impose
363
width and height constraints. Aural user agents may impose limits
364
in audio space, but not in time.
366
<h3><a name="q5">2.3.2 CSS2 addressing model</a></h3>
368
<P> CSS2 <a href="selector.html">selectors</a> and properties allow
369
style sheets to refer to the following parts of a document
373
<li>Elements in the document tree and certain relationships between
374
them (see the section on <a href="selector.html">selectors</a>).
376
<li>Attributes of elements in the document tree, and values of those
377
attributes (see the section on <a
378
href="selector.html#attribute-selectors">attribute selectors</a>).
380
<li>Some parts of element content (see the <a
381
href="selector.html#first-line">:first-line</a> and <a
382
href="selector.html#first-letter">:first-letter</a> pseudo-elements.
384
<li>Elements of the document tree when they are in a certain state
385
(see the section on <a href="selector.html#pseudo-classes">pseudo-classes</a>).
387
<li>Some aspects of the <a href="intro.html#canvas">canvas</a> where
388
the document will be rendered.
390
<li>Some system information (see the section on <a href="ui.html">user
394
<H2><a name="q6">2.4 CSS design principles</a></H2>
396
<P>CSS2, as CSS1 before it, is based on a set of design principles:</p>
402
<P><STRONG>Forward and backward compatibility</STRONG>. CSS2 user
403
agents will be able to understand CSS1 style sheets. CSS1 user agents
404
will be able to read CSS2 style sheets and discard parts they don't
405
understand. Also, user agents with no CSS support will be able to
406
display style-enhanced documents. Of course, the stylistic
407
enhancements made possible by CSS will not be rendered, but all
408
content will be presented.</P>
412
<P><STRONG>Complementary to structured documents</STRONG>. Style
413
sheets complement structured documents (e.g., HTML and XML
414
applications), providing
415
stylistic information for the marked-up text. It should be easy to
416
change the style sheet with little or no impact on the markup.</P>
420
<P><STRONG>Vendor, platform, and device independence</STRONG>. Style
421
sheets enable documents to remain vendor, platform, and device
422
independent. Style sheets themselves are also vendor and platform
423
independent, but CSS2 allows you to target a style sheet for a group of
424
devices (e.g., printers).</P>
428
<P><STRONG>Maintainability</STRONG>. By pointing to style sheets from
429
documents, webmasters can simplify site maintenance and retain
430
consistent look and feel throughout the site. For example, if
431
the organization's background color changes, only one file needs to be
436
<P><STRONG>Simplicity</STRONG>. CSS2 is more complex than CSS1, but
437
it remains a simple style language which is human readable and
438
writable. The CSS properties are kept independent of each other to the
439
largest extent possible and there is generally only one way to achieve a
444
<P><STRONG>Network performance</STRONG>. CSS provides for compact
445
encodings of how to present content. Compared to images or audio
446
files, which are often used by authors to achieve certain rendering
447
effects, style sheets most often decrease the content size. Also,
448
fewer network connections have to be opened which further increases
449
network performance.</P> </LI>
452
<P><STRONG>Flexibility</STRONG>. CSS can be applied to content in
453
several ways. The key feature is the ability to cascade style
454
information specified in the default (user agent) style sheet, user
455
style sheets, linked style sheets, the document head, and in
456
attributes for the elements forming the document body.</P>
460
<P><STRONG>Richness</STRONG>. Providing authors with a rich set of
461
rendering effects increases the richness of the Web as a medium of
462
expression. Designers have been longing for functionality commonly
463
found in desktop publishing and slide-show applications. Some of
464
the requested rendering effects conflict with device independence, but
465
CSS2 goes a long way toward granting designers their requests.</P>
469
<P><STRONG>Alternative language bindings</STRONG>. The set of CSS
470
properties described in this specification form a consistent
471
formatting model for visual and aural presentations. This formatting
472
model can be accessed through the CSS language, but bindings to other
473
languages are also possible. For example, a JavaScript program may
474
dynamically change the value of a certain element's <a href="colors.html#propdef-color" class="noxref"><span
475
class="propinst-color">'color'</span></a> property.</P>
479
<P><STRONG>Accessibility</STRONG>. Several CSS
480
features will make the Web more accessible
481
to users with disabilities:</p>
484
<LI>Properties to control font appearance allow authors
485
to eliminate inaccessible bit-mapped text images.
486
<li>Positioning properties allow authors to eliminate
487
mark-up tricks (e.g., invisible images) to force layout.
489
<tt>!important</tt> rules mean that users with
490
particular presentation requirements
491
can override the author's style sheets.
492
<li>The new 'inherit' value for all properties
493
improves cascading generality and allows for
494
easier and more consistent style tuning.
495
<li>Improved media support, including media groups and the
496
braille, embossed, and tty media types, will allow users and
497
authors to tailor pages to those devices.
498
<LI>Aural properties give control over voice and audio output.
500
selectors, 'attr()' function,
501
and 'content' property give access to alternate content.
502
<LI>Counters and section/paragraph numbering can improve
503
document navigability and save on indenting spacing
504
(important for braille devices). The 'word-spacing' and
505
'text-indent' properties also eliminate the need
506
for extra whitespace in the document.
509
<div class="note"><P> <em><strong>Note.</strong> For more information
510
about designing accessible documents using CSS and HTML, please
511
consult <a href="refs.html#ref-WAI-PAGEAUTH" rel="biblioentry" class="noxref"><span class="informref">[WAI-PAGEAUTH]</span></a>.</em>
518
<div class="navbar" align="center">
519
<p><a href="about.html">previous</a>
520
<a href="conform.html">next</a>
521
<a href="cover.html#minitoc">contents</a>
522
<a href="propidx.html">properties</a>
523
<a href="indexlist.html">index</a>