Sams Teach Yourself HTML 4 in 24 Hours

Sams Teach Yourself HTML 4 in 24 Hours

By Dick Oliver

Style Classes

This is a "teach yourself" book, so you don't have to go to a single class to learn how to give your pages great style, although you do need to learn what a style class is. Whenever you want some of the text on your pages to look different than the other text, you can create what amounts to a custom-built HTML tag. Each type of specially formatted text you define is called a style class.

A style class is a custom set of formatting specifications that can be applied to any passage of text in a Web page.

For example, suppose you wanted two different kinds of <h1> headings in your documents. You would create a style class for each one by putting the following text in the style sheet.

h1.silly {font: 36pt Comic Sans;}
h1.serious {font: 36pt Arial;}

To choose between the two style classes in an HTML page, you would use the class attribute, as follows:

<h1 class="silly">Marvin's Munchies Inc.</h1>
Text about Marvin's Muchies goes here.
<h1 class="serious">MMI Investor Information</h1>
Text for business investors goes here.

The words Marvin's Munchies Inc. would appear in 36-point Comic Sans to people whose browsers support style sheets (assuming you included a <link /> to the style sheet at the top of the Web page and assuming he or she has the Comic Sans font installed). The words MMI Investor Information would appear in the 36-point Arial font.

What if you want to create a style class that could be applied to any text, rather than just headings or some other particular tag? You can associate a style class with the <div> tag (which, as you may recall from Hour 5, "Text Alignment and Lists," can enclose any text but doesn't do anything except what its align or other attributes indicate).

You can essentially create your own custom HTML tag by using div. followed by any style class name you make up and any style specifications you choose. That tag can control any number of font, spacing, and margin settings all at once. Wherever you want to apply your custom tag in a page, use a <div> tag with the class= attribute followed by the class name you created.

For example, the style sheet in Figure 16.3 includes the following style class specification:

div.footnote {font-size: 9pt;
              line-height: 12pt;
              text-align: center;}

This style class is applied in Figure 16.4 with the following tag:

<div class="footnote">

Everything between that tag and the accompanying </div> tag in Figure 16.1 appears in 9-point centered text with 12-point vertical line spacing. The same style class was also used on the footnote in Figure 16.2.

Share ThisShare This

Informit Network