Home > Articles > Web Development > HTML/CSS

  • Print
  • + Share This
This chapter is from the book

A Basic Stylesheet

Despite their intimidating power, stylesheets can be simple to create. Consider the web pages shown in Figures 3.1 and 3.2. These pages share several visual properties that can be put into a common stylesheet:

  • 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.
  • They include vertical space between lines of text.
  • They include a footer that is centered and in small print.

    FIGURE 3.1

    FIGURE 3.1 This page uses a stylesheet to fine-tune the appearance and spacing of the text and images.

    FIGURE 3.2

    FIGURE 3.2 This page uses the same stylesheet as the one in Figure 3.1, thus maintaining a consistent look and feel.

Listing 3.1 shows the CSS used in a stylesheet to specify these properties.

LISTING 3.1 A Single External Stylesheet

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;

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 stylesheet code is that it doesn’t look anything like normal HTML code. CSS uses a syntax all its own to specify stylesheets.

Of course, the listing includes some familiar HTML tags (although not all tags require an entry in the stylesheet). As you might guess, body, h1, p, img, a, and footer in the stylesheet refer to the corresponding tags in the HTML documents to which the stylesheet will be applied. The curly braces after each tag name describe how all content within that tag should appear.

In this case, the stylesheet says that all body text should be rendered at a size of 10 points, in the Verdana font (if possible), and with the color black, with 14 points between lines. If the user does not have the Verdana font installed, the list of fonts in the stylesheet 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 padding 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 stylesheet 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 that you can easily add another style rule after it. Review the remainder of the stylesheet in Listing 3.1 to see the presentation formatting applied to additional tags. Don’t worry—you’ll learn more about each of these types of entries throughout the lessons in this book.

To link this stylesheet 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 stylesheet is stored under the name styles.css in the same folder as the HTML document. As long as the web browser supports stylesheets—and all modern browsers do—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 meets one of the goals of HTML, 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

<!DOCTYPE html>

<html lang="en">
    <title>About BAWSI</title>
    <link rel="stylesheet" type="text/css" href="styles.css" />

     <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>

     Copyright &copy; 2005-2013 BAWSI (www.bawsi.org).
     All rights reserved.  Used with permission.

The code in Listing 3.2 is interesting because it contains no formatting of any kind. In other words, nothing in the HTML code 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 stylesheet, 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 stylesheet) so that one change impacts all pages.

  • + Share This
  • 🔖 Save To Your Account