Cover | Table of Contents | Colophon
<B> and <I> started
to creep into the language. Suddenly, a structural language started
to become presentational.<h1>Leaping Above The Water</h1>
h1 into a table and load it up with a ton of other
elements like font and U. With
CSS, all you need is one rule:h1 {color: maroon; font: italic 2em Times, serif; text-decoration: underline;
background: yellow;}h1 {color: maroon; font: italic 2em Times, serif; text-decoration: underline;
background: yellow url(titlebg.png) repeat-x;
border: 1px solid red; margin-bottom: 0; padding: 5px;}h1
that is only repeated horizontally, and a border around it, which is
separated from the text by at least five pixels.
You've also removed the margin (blank space) from
the bottom of the element. These are feats that HTML
can't even come close to matching—and
that's just a taste of what CSS can do.p, table,
span, a, and
div. In XML languages, the elements are defined by
the language's
Document Type Definition (DTD). Every
single element in a document plays a part in its presentation. In CSS
terms, at least as of CSS2.1, that means each element generates a box
that contains the element's content.span and div.
In CSS, elements generally take two forms: replaced and nonreplaced.
The two types are explored in detail in Chapter 7, which covers the particulars of the box
model, but I'll address them briefly here.img element,
which is replaced by an image file external to the document itself.
In fact, img has no actual content, as you can see
by considering a simple example:<img src="howdy.gif" />
src attribute). The input
element is also replaced by a radio button, checkbox, or text input
box, depending on its type. Replaced elements also generate boxes in
their display.<span>hi
there</span> is a nonreplaced element, and
the text "hi there" will be
displayed by the user agent. This is true of paragraphs, headings,
table cells, lists, and almost everything else in XHTML.<html>
<head>
<title>Eric's World of Waffles</title>
<link rel="stylesheet" type="text/css" href="sheet1.css" media="all" />
<style type="text/css">
@import url(sheet2.css);
h1 {color: maroon;}
body {background: yellow;}
/* These are my styles! Yay! */
</style>
</head>
<body>
<h1>Waffles!</h1>
<p style="color: gray;">The most wonderful of all breakfast foods is
the waffle--a ridged and cratered slab of home-cooked, fluffy goodness
that makes every child's heart soar with joy. And they're so easy to make!
Just a simple waffle-maker and some batter, and you're ready for a morning
of aromatic ecstasy!
</p>
</body>
</html>
link tag:<link rel="stylesheet" type="text/css" href="sheet1.css" media="all" />
link tag is a little-regarded but nonetheless
perfectly valid tag that has been hanging around the HTML
specification for years just waiting to be put to good use. Its basic
purpose is to allow HTML authors to associate other documents with
the document containing the link tag. CSS uses it
to link style sheets to the document; in Figure 1-5, a style sheet called
sheet1.cs
s is linked to the
document.display property, and in a different way by
associating style sheets with a document. The user will never know
whether this is done via an external or embedded style sheet, or even
with an inline style. The real importance of external style sheets is
the way in which they allow authors to put all of a
site's presentation information in one place, and
point all of the documents to that place. This not only makes site
updates and maintenance a breeze, but it helps to save bandwidth
since all of the presentation is removed from documents.h2 elements appear gray.
Using old-school HTML, you'd have to do this by
inserting <FONT
COLOR="gray">...</FONT> tags in all your
h2 elements:<h2><font color="gray">This is h2 text</font></h2>
h2 elements. Worse, if you later decide that
you want all those h2s to be green instead of
gray, you'd have to start the manual tagging all
over again.h2 elements gray:h2 {color: gray;}h2 text to another
color—for example, silver—simply alter the rule:h2 {color: silver;}h2 elements appear gray.
Using old-school HTML, you'd have to do this by
inserting <FONT
COLOR="gray">...</FONT> tags in all your
h2 elements:<h2><font color="gray">This is h2 text</font></h2>
h2 elements. Worse, if you later decide that
you want all those h2s to be green instead of
gray, you'd have to start the manual tagging all
over again.h2 elements gray:h2 {color: gray;}h2 text to another
color—for example, silver—simply alter the rule:h2 {color: silver;}
h1 elements are selected. If the selector were
p, then all p (paragraph)
elements would be selected.h2 elements and paragraphs to have
gray text. The easiest way to accomplish this is to use the following
declaration:h2, p {color: gray;}h2 and p
selectors on the left side of the rule and separating them with a
comma, you've
defined a rule where the style on the right
(color: gray;) applies to the
elements referenced by both selectors. The comma tells the browser
that there are two different selectors involved in the rule. Leaving
out the comma would give the rule a completely different meaning,
which we'll explore later in Section 2.5.2.body, table, th, td, h1, h2, h3, h4, p, pre, strong, em, b, i {color: gray;}h1 {color: purple;}
h2 {color: purple;}
h3 {color: purple;}
h4 {color: purple;}
h5 {color: purple;}
h6 {color: purple;}
h1, h2, h3, h4, h5, h6 {color: purple;}/* group 1 */
h1 {color: silver; background: white;}
h2 {color: silver; background: gray;}
h3 {color: white; background: gray;}
h4 {color: silver; background: white;}
b {color: gray; background: white;}
/* group 2 */
h1, h2, h4 {color: silver;}
h2, h3 {background: gray;}
h1, h4, b {background: white;}
h3 {color: white;}
b {color: gray;}
/* group 3 */
h1, h4 {color: silver; background: white;}
h2 {color: silver;}
h3 {color: white;}
h2, h3 {background: gray;}
b {color: gray; background: white;}p {font-weight: bold;}class attribute:<p class="warning">When handling plutonium, care must be taken to avoid the formation of a critical mass.</p> <p>With plutonium, <span class="warning">the possibility of implosion is very real, and must be avoided at all costs</span>. This can be accomplished by keeping the various masses separate.</p>
h1 elements
that have a class attribute with any value and
make their text silver, write:
h1[class] {color: silver;}<h1 class="hoopla">Hello</h1> <h1 class="severe">Serenity</h1> <h1 class="fancy">Fooling</h1>
planet elements with a
moons attribute and make them boldface, thus
calling attention to any planet that has moons, you would write:planet[moons] {font-weight: bold;}<planet>Venus</planet> <planet moons="1">Earth</planet> <planet moons="2">Mars</planet>
<html>
<head>
<base href="http://www.meerkat.web/">
<title>Meerkat Central</title>
</head>
<body>
<h1>Meerkat <em>Central</em></h1>
<p>
Welcome to Meerkat <em>Central</em>, the <strong>best meerkat web site
on <a href="inet.html">the <em>entire</em> Internet</a></strong>!</p>
<ul>
<li>We offer:
<ul>
<li><strong>Detailed information</strong> on how to adopt a meerkat</li>
<li>Tips for living with a meerkat</li>
<li><em>Fun</em> things to do with a meerkat, including:
<ol>
<li>Playing fetch</li>
<li>Digging for food</li>
<li>Hide and seek</li>
</ol>
</li>
</ul>
</li>
<li>...and so much more!</li>
</ul>
<p>
Questions? <a href="mailto:suricate@meerkat.web">Contact us!</a>
</p>
</body>
</html>
a), which, in HTML and XHTML, establishes
a link from one document to another. Anchors are always anchors, of
course, but some anchors refer to pages that have already been
visited, while others refer to pages that have yet to be visited. You
can't tell the difference by simply looking at the
HTML markup, because in the markup, all anchors look the same. The
only way to tell which links have been visited is by comparing the
links in a document to the user's browser history.
So, there are actually two basic types of anchors: visited and
unvisited. These types are known as
pseudo-classes
, and the
selectors that use them are called pseudo-class selectors.h1 {color: red;}
body h1 {color: green;}
h2.grape {color: purple;}
h2 {color: silver;}
html > body table tr[id="totals"] td ul > li {color: maroon;}
li#answer {color: navy;}h1 {color: red;}
body h1 {color: green;}
h2.grape {color: purple;}
h2 {color: silver;}
html > body table tr[id="totals"] td ul > li {color: maroon;}
li#answer {color: navy;}0,0,0,0. The actual
specificity of a selector is determined as follows:0,1,0,0.0,0,1,0.0,0,0,1.
CSS2 contradicted itself as to
whether pseudo-elements had any specificity at all, but CSS2.1 makes
it clear that they do, and this is where they belong.h1 element, for
example, then that color is applied to all text in the
h1, even the text enclosed within child elements
of that h1:h1 {color: gray;}
<h1>Meerkat <em>Central</em></h1>h1 text and the
em text are colored gray because the
em element inherits the value of
color. If property values could not be inherited
by descendant elements, the em text would be
black, not gray, and you'd have to color that
element separately.color: gray; for
ul elements:ul {color: gray;}ul
will also be applied to its list items, and to any content of those
list items. Thanks to inheritance, that's exactly
what happens, as Figure 3-3 demonstrates.
color:
gray; is applied to the ul
element, that element takes on that declaration. The value is then
propagated down the tree to the descendant elements and continues on
until there are no more descendants to inherit the value. Values are
never propagated upward; that is, an element
never passes values up to its ancestors.body element can be passed to the
html element, which is the
document's root element and therefore defines its
canvas.h1 {color: red;}
h1 {color: blue;}0,0,0,1, so they have equal weight and should both
apply. That simply can't be the case because the
element can't be both red and blue. But which will
it be?!important are given higher weight than those that
are not. Also sort by
origin
all declarations applying to a given element. There are three
origins:
author, reader, and user agent.
Under normal circumstances, the author's styles win
out over the reader's styles.
!important reader styles are stronger than any
other styles, including !important author styles.
Both author and reader styles override the user
agent's default styles.%). Percentage
values are nearly always relative to another value, which can be
anything, including the value of another property of the same
element, a value inherited from the parent element, or a value of an
ancestor element. Any property that accepts percentage values will
define any restrictions on the range of allowed percentage values,
and will also define the degree to which the percentage is relatively
calculated.red or
purple, or employ a vaguely cryptic method using
hexadecimal codes. Both of these methods for describing colors remain
in CSS, along with some other—and, I think, more
intuitive—methods.%). Percentage
values are nearly always relative to another value, which can be
anything, including the value of another property of the same
element, a value inherited from the parent element, or a value of an
ancestor element. Any property that accepts percentage values will
define any restrictions on the range of allowed percentage values,
and will also define the degree to which the percentage is relatively
calculated.red or
purple, or employ a vaguely cryptic method using
hexadecimal codes. Both of these methods for describing colors remain
in CSS, along with some other—and, I think, more
intuitive—methods.|
aqua
|
fuchsia
|
lime
|
olive
|
red
|
white
|
|
black
|
gray
|
maroon
|
orange
|
silver
|
yellow
|
|
blue
|
green
|
navy
|
purple
|
teal
|
h1 {color: maroon;}in
(inches) or pt (points). The only exception to
this rule is a length of 0 (zero), which need not
be followed by a unit.in)cm)mm)pt)@import
statement, which is used when importing an external style
sheet—the general format is:url(protocol://server/pathname)
pix, and
in this directory is an image waffle22.gif. In
this case, the absolute URL of that image would be:http://www.waffles.org/pix/waffle22.gif
url(pathname)pix/waffle22.gif
deg), grads
(grad), and radians (rad). For
example, a right angle could be declared as 90deg,
100grad, or 1.57rad; in each
case, the values are translated into degrees in the range 0 through
360. This is also true of negative values, which are allowed. The
measurement -90deg is the same as
270deg.ms) or seconds
(s). Thus, 100ms and
0.1s are equivalent. Time values may not be
negative, as CSS is supposed to avoid paradoxes.Hz) or megahertz (MHz) and
cannot be negative. The values' labels are
case-insensitive, so 10MHz and
10Mhz are equivalent.smaller) to color units to the location of files
(such as images). For the most part, units are the one area that user
agents get almost totally correct; it's those few
little bugs and quirks that get you, though. Navigator
4.x's failure to interpret relative URLs correctly,
for example, has bedeviled many authors, and lead to an overreliance
on absolute URLs. Colors are another area where user agents almost
always do well, except for a few little quirks here and there. The
vagaries of length units, however, far from being bugs, are an
interesting problem for any author to tackle.<FONT
FACE="..."> tags?
In fact, the beginning of the "Font
Properties" section of the specification begins with
the sentence, "Setting font properties will be among
the most common uses of style sheets."