Book HomeHTML & XHTML: The Definitive GuideSearch this book

8.6. Applying Styles to Documents

There are several issues you should consider before, during, and after you use styles in your web documents and document collections. The first, overarching issue is whether to use them at all. Frankly, few of the style effects are unique; most can be achieved, albeit less easily and with much less consistency, via the physical and content-based style tags like <i> and <em> and the various tag attributes like color and background.

8.6.1. To Style or Not to Style

We think the CSS2 standard is a winner, not only over just JavaScript-based standards, but mostly for the convenience and effectiveness of all your mark-up documents, including HTML, XHTML, and any and most other XML-compliant ones. The majority of browsers in use today support CSS2 styles. The benefits are clear. So, why wouldn't you use styles?

Although we strongly urge that you learn and use CSS2 style sheets for your documents, we realize that creating style sheets is an investment of time and energy that pays off over the long run. Designing a style sheet for a one- or two-page document is probably not time-effective, particularly if you won't be reusing the style sheet for any other documents. In general, however, we believe the choice is not if you should use CSS2 style sheets, but when.

8.6.2. Which Type of Style Sheet and When

Once you have decided to use Cascading Style Sheets (for pain or pleasure), the next question is which type of style sheet -- inline, document-level, or external -- should you apply and when? Each has its pros and cons; each is best applied under certain circumstances.

8.6.2.1. The pros and cons of external styles

Since style sheets provide consistency in the presentation of your documents, external style sheets are the best and the easiest way to manage styles for your entire document collection. Simply place the desired style rules in a style sheet and apply those styles to the desired documents. And since all the documents are affected by a single style sheet, conversion of the entire collection to a new style is as simple as changing a single rule in the corresponding external style sheet.

Even in cases where documents may differ in style, it is often possible to collect a few basic style rules in a single sheet that can be shared among several otherwise different documents, including:

  • Background color

  • Background image

  • Font sizes and faces

  • Margins

  • Text alignment

Another benefit of external style sheets is that other web authors who want to copy your style can easily access that sheet and make their pages look like yours. Imitation being the sincerest form of flattery, you should not be troubled when someone elects to emulate the look and feel of your pages. More to the point, you can't stop them from linking to your style sheets, so you might as well learn to like it. Like conventional HTML documents, it is not possible to encrypt or otherwise hide your style sheets so that others cannot view and use them.

The biggest problem with external style sheets is that they increase the amount of time needed to access a given web page. Not only must the browser download the page itself, it must also download the style sheet before the page can be displayed to the user. While most style sheets are relatively small, their existence can definitely be felt when accessing the Web over a slow connection.

Without appropriate discipline, external style sheets can become large and unwieldy. When creating style sheets, remember to include only those styles that are common to the pages using the sheet. If a set of styles is needed only for one or two sheets, you are better off isolating them in a separate sheet or adding them to a document using document-level styles. Otherwise, you may find yourself expending an exorbitant amount of effort counteracting the effects of external styles in many individual documents.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.