1.2. CSS to the Rescue
Of course, the problem of polluting HTML with presentational markup was not lost on the World Wide Web Consortium (W3C), which
1.2.1. Rich Styling
In the first place, CSS allows for much richer document
Take, for example, the first (and main) heading on a page, which is usually the title of the page itself. The proper markup is: <h1>Leaping Above The Water</h1>
Now, suppose you want this title to be dark red, use a certain font, be italicized and
h1 {color: maroon; font: italic 2em Times, serif; text-decoration: underline;
background: yellow;}
That's it. As you can see, everything you did in HTML can be done in CSS. There's no need to confine yourself to only those things HTML can do, however:
h1 {color: maroon; font: italic 2em Times, serif; text-decoration: underline;
background: yellow url(http://flylib.com/books/2/885/1/html/2/titlebg.png) repeat-x;
border: 1px solid red; margin-bottom: 0; padding: 5px;}
You now have an image in the background of the h1 that is only repeated horizontally, and a border around it, 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 matchingand that's just a taste of what CSS can do. 1.2.2. Ease of UseIf the depth of CSS doesn't convince you, then perhaps this will: style sheets can drastically reduce a web author's workload. First, style sheets centralize the commands for certain visual effects in one handy place, instead of scattering them throughout the document. As an example, let's say you want all of the h2 headings in a document to be purple. Using HTML, the way to do this would be to put a font tag in every heading, like so: <h2><font color="purple">This is purple!</font></h2> This must be done for every heading of level two. If you have 40 headings in your document, you have to insert 40 font elements throughout, one for each heading! That's a lot of work for one little effect. Let's assume that you've gone ahead and put in all those font elements. You're done, you're happyand then you decide (or your boss decides for you) that those h2 headings should really be dark green, not purple. Now you have to go back and fix every single one of those font elements. Sure, you might be able to find-and-replace, as long as headings are the only purple text in your document. If you've put other purple font elements in your document, then you can't find-and-replace because you'd affect those, too. It would be much better to have a single rule instead:
h2 {color: purple;}
Not only is this faster to type, but it's easier to change. If you do switch from purple to dark green, all you have to change is that one rule. Let's go back to the highly styled h1 element from the previous section:
h1 {color: maroon; font: italic 2em Times, serif; text-decoration: underline;
background: yellow;}
This may look like it's
h1, h2 {color: maroon; font: italic 2em Times, serif; text-decoration: underline;
background: yellow;}
Now the styles apply to both h1 and H2 elements, with just three extra keystrokes. If you want to change the way h1 and h2 elements look, the advantages of CSS are even more striking. Consider how long it would take to change the HTML markup for an H1 and 12 H2 elements, compared to changing the previous styles to this:
h1, h2 {color: navy; font: bold 2em Helvetica, sans-serif;
text-decoration: underline overline; background: silver;}
If the two approaches were timed on a stopwatch, I'm
In addition, most CSS rules are collected into one location in the document. It is possible to scatter them throughout the document by grouping them into associated styles or individual elements, but it's usually far more efficient to place all of your styles into a single style sheet. This lets you create (or change) the appearance of an entire document in one place. 1.2.3. Using Your Styles on Multiple PagesBut waitthere's more! Not only can you centralize all of the style information for a page in one place, but you can also create a style sheet that can then be applied to multiple pages. This is accomplished by a process in which a style sheet is saved to its own document and then imported by any page for use with that document. Using this capability, you can quickly create a consistent look for an entire web site. All you have to do is link the single style sheet to all of the documents on your web site. Then, if you ever want to change the look of your site's pages, you need only edit a single file and the change will be propagated throughout the entire serverautomatically! Consider a site where all of the headings are gray on a white background. They get this color from a style sheet that says:
h1, h2, h3, h4, h5, h6 {color: gray; background: white;}
Now let's say this site has 700 pages, each of which uses the style sheet that says the headings should be gray. At some point, the site's webmaster decides that the headings should be white on a gray background. So she edits the style sheet to say:
h1, h2, h3, h4, h5, h6 {color: white; background: gray;}
Then she saves the style sheet to disk and the change is made. That sure beats having to edit 700 pages to enclose every heading in a table and a font tag, doesn't it? 1.2.4. CascadingThat's not all! CSS also makes provisions for conflicting rules; these provisions are collectively referred to as the cascade . For instance, take the previous scenario in which you import a single style sheet into several web pages. Now inject a set of pages that share many of the same styles, but also include specialized rules that apply only to them. You can create another style sheet that is imported into those pages, in addition to the already existing style sheet, or you could just place the special styles into the pages that need them. For example, on one page out of the 700, you might want headings to be yellow on dark blue instead of white on gray. In that single document, then, you could insert this rule:
h1, h2, h3, h4, h5, h6 {color: yellow; background: blue;}
Thanks to the cascade, this rule will override the imported rule for white-on-gray headings. By understanding the cascade rules and using them to your advantage, you can create highly sophisticated sheets that can be changed easily and come together to give your pages a professional look.
The power of the cascade is not confined to the author. Web surfers (or
readers
) can, in some browsers, create their own style sheets (called
reader style sheets
, obviously enough) that will cascade with the author's styles as well as the styles used by the browser. Thus, a reader who is colorblind could create a style that makes
a:link, a:visited {color: white; background: black;}
A reader style sheet can contain almost anything: a directive to make text large enough to read if the user has impaired vision, rules to remove images for faster reading and browsing, and even styles to place the
Between importing, cascading, and its variety of effects, CSS is a wonderful tool for any author or reader.
1.2.5. Compact File
|