Chapter 7: Style Sheet Fundamentals
Style Sheets, or more properly Cascading Style Sheets (
CSS), are a method
for specifying formatting in an
HTML document. When you use CSS, you
can avoid using most or all of the HTML tags and attributes that control
formatting. Why would you want to do this--what's wrong with the
formatting tags?
The main advantage of
CSS is that it allows you to define the formatting
of a Web page separately from the content of the Web page as I
mentioned earlier. Let's look at an example. Suppose you wanted to
define a special appearance for certain text in your Web pages: larger than
normal text in the Bazooka font, blue. Using regular
HTML tags you
would do it like this:
<font size="4" face="Bazooka" color="blue">This the
text</font>
In fact you would have to use this
<font> tag for each and every instance
of text that you want formatted this way. So far, so good--but then
suppose your boss decides that she really wants this text in Arial font, red.
Guess what--you have to go through the entire page, and every page in the
Website, and change each and every
<font> tag. This is not cool!
But what if you had used a style to define this special text? Then you are
in cool city! Here's what the style definition would look like:
.special { font-family: Bazooka; font-size: 14pt;
color: #0000FF; }
And here's how you would apply it to text:
<p class="special">This is the text</p>
Here's what's important--to change the text all you need do is edit the
style and the change will automatically be applied to every instance in the
Website. Interested? Then read on.
CSS Versions
CSS comes in two versions. CSS Level 1 has been around since 1996 and
presently enjoys wide browser support. CSS Level 1 includes styles for
font formatting, text alignment, line spacing, and similar formatting tasks.
CSS Level 2 has been available since 1998 but there is only limited
support at present. Level 2 provides a lot more power, particularly when it
comes to positioning elements on the page. In fact, some people predict
that
CSS Level 2 will eventually replace
HTML tables for page layout. It's
possible, but I'm not holding my breath! Because CSS Level 1 support is
so widespread and Level 2 support is patchy at best, I will limit this
chapter to the most commonly needed formatting that is available in CSS
Level 1.