A Basic Style Sheet
Despite their intimidating power, style sheets can be simple to create. Consider the web pages shown in Figure 3.1 and Figure 3.2. These pages share several visual properties that could be put into a common style sheet:
- They use a large, bold Verdana font for the headings and a normal size and weight Verdana font for the body text.
- They use an image named logo.gif floating within the content and on the right side of the page.
- All text is black except for subheadings, which are purple.
- They have margins on the left side and at the top.
- There is vertical space between lines of text.
- The footnotes are centered and in small print.
Figure 3.1 This page uses a style sheet to fine-tune the appearance and spacing of the text and images.
Figure 3.2 This page uses the same style sheet as the one shown in Figure 3.1, thus maintaining a consistent look and feel.
Listing 3.1 shows the code for the style sheet specifying these properties.
Listing 3.1 A Single External Style Sheet
body { font-size: 10pt; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: black; line-height: 14pt; padding-left: 5pt; padding-right: 5pt; padding-top: 5pt; } h1 { font: 14pt Verdana, Geneva, Arial, Helvetica, sans-serif; font-weight: bold; line-height: 20pt; } p.subheader { font-weight: bold; color: #593d87; } img { padding: 3pt; float: right; } a { text-decoration: none; } a:link, a:visited { color: #8094d6; } a:hover, a:active { color: #FF9933; } div.footer { font-size: 9pt; font-style: italic; line-height: 12pt; text-align: center; padding-top: 30pt; }
This might initially appear to be a lot of code, but if you look closely, you’ll see that there isn’t a lot of information on each line of code. It’s fairly standard to place individual style rules on their own line to help make style sheets more readable, but that is a personal preference; you could put all the rules on one line as long as you kept using the semicolon to separate each rule (more on that in a bit). Speaking of code readability, perhaps the first thing you noticed about this style sheet code is that it doesn’t look anything like normal HTML code. CSS uses a language all its own to specify style sheets.
Of course, the listing includes some familiar HTML tags. As you might guess, body, h1, p, img, a, and div in the style sheet refer to the corresponding tags in the HTML documents to which the style sheet will be applied. The curly braces after each tag name contain the specifications for how all content within that tag should appear.
In this case, the style sheet says that all body text should be rendered at a size of 10 points, in the Verdana font (if possible), with the color black, and 14 points between lines. If the user does not have the Verdana font installed, the list of fonts in the style sheet represents the order in which the browser should search for fonts to use: Geneva, then Arial, and then Helvetica. If the user has none of those fonts, the browser will use whatever default sans serif font is available. Additionally, the page should have left, right, and top margins of 5 points each.
Any text within an <h1> tag should be rendered in boldface Verdana at a size of 14 points. Moving on, any paragraph that uses only the <p> tag will inherit all the styles indicated by the body element. However, if the <p> tag uses a special class named subheader, the text will appear bold and in the color #593d87 (a purple color).
The pt after each measurement in Listing 3.1 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).
You might have noticed that each style rule in the listing ends with a semicolon (;). Semicolons are used to separate style rules from each other. It is therefore customary to end each style rule with a semicolon, so you can easily add another style rule after it.
To link this style sheet to HTML documents, include a <link /> tag in the <head> section of each document. Listing 3.2 shows the HTML code for the page shown in Figure 3.1. It contains the following <link /> tag:
<link rel="stylesheet" type="text/css" href="styles.css" />
This assumes that the style sheet is stored under the name styles.css in the same folder as the HTML document. As long as the web browser supports style sheets—and all modern browsers do support style sheets—the properties specified in the style sheet will apply to the content in the page without the need for any special HTML formatting code. This confirms the ultimate goal of XHTML, which is to provide a separation between the content in a web page and the specific formatting required to display that content.
Listing 3.2 HTML Code for the Page Shown in Figure 3.1
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title>About BAWSI</title> <link rel="stylesheet" type="text/css" href="styles.css" /> </head> <body> <h1>About BAWSI</h1> <p><img src="logo.gif" alt="BAWSI logo"/>The Bay Area Women's Sports Initiative (BAWSI) is a public benefit, nonprofit corporation with a mission to create programs and partnerships through which women athletes bring health, hope and wholeness to our community. Founded in 2005 by Olympic and World Cup soccer stars Brandi Chastain and Julie Foudy and Marlene Bjornsrud, former general manager of the San Jose CyberRays women's professional soccer team, BAWSI provides a meaningful path for women athletes to become a more visible and valued part of the Bay Area sports culture.</p> <p class="subheader">BAWSI's History</p> <p>The concept of BAWSI was inspired by one of the most spectacular achievements in women's sports history and born out of one its biggest disappointments... </p> <p><a href="secondpage.html">[continue reading]</a></p> <div class="footer">Copyright © 2005-2009 BAWSI (www.bawsi.org). All rights reserved. Used with permission.</div> </body> </html>
The code in Listing 3.2 is interesting because it contains no formatting of any kind. In other words, there is nothing in the HTML code that dictates how the text and images are to be displayed—no colors, no fonts, nothing. Yet the page is carefully formatted and rendered to the screen, thanks to the link to the external style sheet, styles.css. The real benefit to this approach is that you can easily create a site with multiple pages that maintains a consistent look and feel. And you have the benefit of isolating the visual style of the page to a single document (the style sheet) so that one change impacts all pages.