HTML: The Definitive Guide

Previous Chapter 9
Cascading Style Sheets

9.5 Applying Styles to Documents

There are several issues you should consider before, during, and after you use styles in your HTML 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.

To Style or Not to Style

Effects aside, the biggest question looming in the minds of HTML developers as we write this chapter in early 1997 is whether Cascading Style Sheets, particularly external ones, ever will be fully supported by the major browsers, or whether some other style-sheet delivery mechanism will prevail. Both Netscape Navigator and Internet Explorer have support for Cascading Style Sheets in their most recent versions, but Microsoft's support is out of date and buggy while Netscape's is currently incomplete. Moreover, Netscape has put its development weight behind not CSS, but JavaScript-based styles (see the section called "JavaScript Style Sheets").

So, currently, CSS style sheets are a bittersweet solution; they can create more work than they are worth. The majority of browsers in use today do not support styles. That means that to achieve the special effects for most users, you have to prepare redundant collections--mixed or separate--containing both styles and style-related tags and attributes, if you use styles at all. We're right back in the same boat we were in a year or so ago when deciding whether to use the many new Netscape extensions to HTML. We believe that this too will pass: Netscape promises complete support of the entire CSS standard, and Microsoft surely will update its support to keep pace with Netscape. While styles may be spotty at best in the first half of 1997, expect them to be pervasive by the end of the year.

Which Type of Style Sheet and When

Once you have decided to use HTML 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.

The pros and cons of external styles

Since style sheets ostensibly 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. 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.

The pros and cons of document-level styles

Document-level styles are most useful when creating a custom document. They let you easily override one or more rules in your externally defined style to create a slightly different document.

You might also want to use document-level styles to experiment with new style rules before moving them to your style sheets. By adding and changing rules using document-level styles, you eliminate the risk of adding a broken style to your style sheets, breaking the appearance of all the documents that use that sheet.

The biggest problem with document styles is that you may succumb to using them in lieu of creating a formal, external style sheet to manage your document collection. It is easy to simply add rules to each document, cutting and pasting as you create new documents. Unfortunately, managing a collection of documents with document-level styles is tedious and error-prone. Even a simple change can result in hours of editing and potential mistakes.

As a rule of thumb, any style rule that impacts three or more documents should be moved to a style sheet and applied to those documents using the <link> tag or @import command. Adhering to this rule as you create your document families will pay off in the long run when it is time to change your styles.

The pros and cons of inline styles

And at the end of the cascade, inline styles override the more general styles. Get into the habit now of using inline styles rarely and just for that purpose, too. Inline styles cannot be reused, making style management difficult. Moreover, such changes are spread throughout your documents, making finding and altering inline styles error-prone. (That's why we might eschew tag- and attribute-based styles in the first place, no?)

Anytime you use an inline style, think long and hard as to whether the same effect might be accomplished using a style class definition. For instance, you are better off defining:

<style type="text/css">
  P.centered {text-align: center} {color: blue}

and later using:

<p class=centered>
<em class=blue>

instead of:

<p style="text-align: center">
<em style="color: blue">

Your styles are easier to find and manage and can be easily reused throughout your documents.

Previous Home Next
Tag-Less Styles: The <span> Tag Book Index Forms

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell
Hosted by uCoz