Sams Teach Yourself HTML 4 in 24 Hours

Sams Teach Yourself HTML 4 in 24 Hours

By Dick Oliver

A Basic Style Sheet

Despite their intimidating power, style sheets can be very simple to create. Consider the documents in Figures 16.1 and 16.2. These documents share several properties that could be put into a common style sheet.

16fig01.jpg

Figure 16.1 This page uses a style sheet to fine-tune the appearance and spacing of the text and background.

16fig02.jpg

Figure 16.2 This page uses the same style sheet as the one in Figure 16.1.

Some of these properties, such as text color, background tile, and centered small print, are easy to achieve with ordinary HTML tags. Others, such as line spacing and wide margins, are beyond the scope of standard HTML. All of them can now be achieved easily with style sheets.

Figure 16.3 shows how an HTML style sheet that specified these properties would look.

16fig03.gif

Figure 16.3 A single style sheet can specify the properties of any number of pages. Figures 16.1 and 16.2 both use this style sheet.

The first thing you'll undoubtedly notice about this style sheet is that it doesn't look anything like normal HTML. Style sheet specifications are really a separate language.

Of course, there are some familiar HTML tags in there. As you might guess, body, p, h1, h2, a, and div in the style sheet refer to the corresponding tags in the HTML documents to which the style sheet will be applied. In curly braces after each tag name are the specifications for how all text within that tag should appear.

In this case, all body text that isn't within some other tag should be rendered at a size of 12 points, in the Book Antiqua font if possible, and with the color maroon and 16 points between lines. The page should have 10-point margins, and the background should be the image found at the relative URL parchmnt.jpg.

Any paragraph starting with a <p> tag will be indented an additional 24 points.

Any text within <h1> or <h2> tags should be rendered in boldface ProseAntique at a size of 24 points and 18 points, respectively. If a user doesn't have a font named ProseAntique installed, the Lucida Handwriting font will be used instead.

The pt after each measurement in Figure 16.3 means points (there are 72 points in an inch). If you prefer, you can specify any style sheet measurement in inches (in), centimeters (cm), pixels (px), or widths-of-a-letter-m, which are called ems (em).

To link this style sheet to the HTML documents, you include a <link /> tag in the <head> section of each document. Figure 16.4 is the HTML for the page in Figure 16.1. It contains the following <link /> tag:

16fig04.jpg

Figure 16.4 When viewed in Netscape Navigator or Microsoft Internet Explorer (version 4.0 or later), this page looks like Figure 16.1. In older browsers that don't support style sheets, it looks like Figure 16.5.

<link rel="stylesheet" type="text/css" href="hhh.css" />

This assumes the style sheet was saved under the name hhh.css in the same directory folder as the HTML document. Netscape Navigator 4 and Internet Explorer 4 and later will then give the body and heading text the properties specified in the style sheet, without the need for any <font> tags or <body background> attribute in the document itself.

Share ThisShare This

Informit Network