Cascading Style Sheets (CSS): Basics
Cascading Style Sheets (CSS): Basics
When you've been writing HTML for a while, you'll start wishing that it could do a few more tricks. Wouldn't it be nice, for instance, if you could adjust the spaces between lines? Wouldn't it be nice if you could create shortcuts for some common formatting operations? Wouldn't it be nice if you could position a graphic right up against the upper-left corner of the browser window?
Style sheets are the tooth fairy for a lot of these wishes.
A style sheet is a collection of templates, or styles, that apply to various parts of your document and describe the way it gets rendered. Style sheets have been available in all the major word processing programs for quite some time.
Early in the development of HTML and the Web, designers and programmers started asking for a form of style sheets that could be applied to HTML documents. A number of different proposals were fielded, and one proposal, Cascading Style Sheets (CSS), was implemented. There are other style sheet specifications, but CSS is the form that has been accepted, and all the major browser vendors are working hard to adopt it.
Cascading Style Sheets
The Cascading Style Sheets (CSS) specification is the form of style sheets recommended by the World Wide Web Consortium (W3C) as appropriate for use with HTML. It is also the form that is being deployed by the two major browser manufacturers: Netscape and Microsoft.
Unfortunately for those of us who would really like to use CSS, it is implemented neither completely nor consistently enough in either of the major browsers for us to use CSS with any confidence. With that in mind, the intent of this article is to show you how CSS works and what its potential is, so that you will be familiar with it when it eventually becomes ready for prime time.
Therefore, we urge you to read this article with this caveat emptor: The details of CSS will almost certainly change somewhat before the dust settles. The basic principles will almost certainly not change. Treat what you learn in this article as a preview of things to come, and experiment with it, but don't be surprised if it works differently in later versions of your favorite browser. (We conducted our tests using Netscape Navigator 4.03 and Microsoft Internet Explorer 4 [preview release 2], and we have included only examples that work the same between the two.)
The scope of this article is to show you generally what style sheets are going to be able to do. The scope of this article is not to show you all the details of CSS because most of it doesn't work today and is subject to a lot of change before it does work. Or spoken more simply, "Your mileage may vary."
How CSS Style Sheets Work
A CSS style sheet is the type of document that is used by a Web browser to redefine the properties of the various elements and tags in the HTML.
The style sheet document may be contained within the HTML document, or it may be in a separate file on the server. An internal style sheet is one that's contained within the HTML document. An external style sheet is stored in a separate file on the server, allowing one style sheet to be used for a whole group of HTML documents. In the future, you may also be able to specify a style sheet with your favorite settings in your browser and have it apply to pages without style sheets, or even override some style sheets on some pages.
For our purposes, we will work with internal style sheets. (See the following sidebar, "External Style Sheets," for a brief description of the other method.) Just keep in mind that there are other ways to do this.
External Style Sheets
External style sheets are convenient for situations when you may want to apply a set of styles across a number of different documents. For example, you could create an external style sheet that defines a color scheme for your Web site, including a background image, fonts, and sizes for headings and paragraphs, and so on. Then, whenever you want to change the look of your site, you simply update the external style sheet and voilà! Your site is updated! For more details about linking external style sheets, see the W3C's documentation at the following site:
For the first few examples, we use a page from the Ducks In A Row site, which explains how the rubber stamps are used. First, here's what the page looks like in plain old ordinary HTML (see Figure 1).
<HEAD> <TITLE>CSS Examples</TITLE> </HEAD> <BODY BGCOLOR="#FFFFCC" TEXT="#663300" LINK="#006699" VLINK="#006699"> <H1>Working with Ducks in a Row Rubber Stamps</H1> <P> Because the style of art throughout the catalog is consistent, any of the designs may be used together side by side, or superimposed, making the possibilities for personal expression boundless. <P> You will find designs in our catalog which cover a vast array of categories, from floral images to humor, holiday stamps, border designs and decorative imagery, for all around use. You, as the stamping artist will be the one to create original art works from these designs, and nothing pleasures us more than to see the beautiful and original ways in which our designs are being used. <P> Throughout the catalog you will find some helpful hints on some of the uses and techniques to enjoy your stamps, but we're sure that you as the stamping artist can teach us a thing or two! One thing for certain, the possibilities are endless! </BODY>
Figure 1 Sample page without CSS.