Chapter ONLINE SECTION III. Adding Cascading Style Sheets to Documents

Like their counterparts in word processing and desktop publishing programs, style sheets are supposed to simplify the deployment of fine-tuned formatting associated with HTML content. Instead of surrounding every h1 element in a document with <font> tags to make all of those headings the same color, you can use a one-line style definition in a style sheet to assign a color to every instance of the h1 element on the page. This puts the purpose of tagging in its proper place: assigning context within a document via HTML markup, while rules governing the appearance of data within that context belong to the style sheet.

Observing HTML Structures

In order to incorporate style sheets successfully into HTML documents, you may have to reexamine your current tagging practices. How much you’ll have to change your ways depends on how and when you learned HTML in the first place. Over the years, popular browsers have generally accommodated—how shall I say it—less-than-perfect HTML. Consider the <p> tag, which for years in the old days had been treated as a single tag that separates paragraphs with a wider line space than the <br> line break tag. HTML standards even encourage this start-tag-only thinking by making some end tags optional. For example, you can define an entire row of table cells without once specifying a </td> or </tr> tag: the browser automatically closes a tag pair when it encounters a logical start tag for, ...

Get Dynamic HTML: The Definitive Reference, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.