Cascading Style Sheet Tutorial
Introduction
Cascading Style Sheets (CSS) is a simple mechanism for adding style (e.g. fonts, colors, spacing) to Web documents. This document is a "low rent" tutorial on Cascading Style Sheets (CSS) and contains just enough information to get you started. If you want the Real Thing then you want the W3C's CSS Web page. The original specification was CSS 1.0 (aka CSS1), but as of 1999 there is a new version called CSS 2.0.
An Example
Here is a simple style sheet such as might be found in the <HEAD> part of an HTML document:
<STYLE TYPE="text/css"> <!--
BODY, TH, TD {
font-family: Verdana, sans-serif;
font-size: 10pt;
}
A:hover { color: red; }
.in { margin-left: 2em; }
// -->
</STYLE>
The <STYLE> tag identifies this as a CSS type style (there could be others), and the HTML comment right after it forces browsers that don't recognize CSS to ignore everything up to the end of the line before the ending </STYLE> tag.
Then general form of a style sheet is the name of one or more tags or "classes" (more on this later) followed by the style specification enclosed within squigley brackets. The style specifications, IMHO, are the tough part to remember you have to spell them exactly right, and remember what the valid values are.
"font-family", for instance, is how you pick a font. In the example above I want the "Verdana" font, but if it isn't availabe, then I'd like any "sans-serif" font. Sans-serif is a generic fontname and leaves the browser free to pick the specific font it wants from that family. The "BODY, TH, TD" part means I want this style to apply to all tags which inherit from BODY. I included TH and TD specifically because Netscape's browser doesn't apply the style properly (it doesn't let TH and TD to inherit from BODY), and I want the style to work inside TABLEs.
I used "font-size" to specify the size of characters I want. 10 point corresponds to "SIZE=2" in the <FONT> tag. I could also have specified the size using percentages and a number of other measures. I prefer using "points", however, as it is the same measure that Windows programs use to specify font size.
The next line, with the "A:hover", is really a CSS2 feature but it's really cool and IE 4 supports it. It means that for ANCHOR links, when the cursor hovers over the link, apply the given style. In this case, it turns the link red. CSS recognizes some basic colors by name, and otherwise uses the same RGB values that HTML does. e.g. "color: #FF0000;" would have done the same thing as what I wrote.
The last line of my style above creates a "class" called "in", with a style
which includes an indent from the left margin of the width of two em
characters (roughly the width of two letter W's). It is a class because it
starts with a dot. That means I can apply it to most other tags via what
is called an inline style. For example, to have a paragraph indented as
desired, I would code "<P CLASS=in>" in my HTML source. I
could apply the class to other tags as well, such as a <TABLE>.
The "Cascading" Part
The "cascading" part is a bit complicated. See the official specification if you want the fine details, but let me warn you it is complicated. In CSS1 in general, the Web page author's style is given the highest weight, followed by the user's style, and finally, the browser's style. In CSS2 I hear they give the user's style preference over the author's (which I think is a mistake).
For the purposes of this tutorial, let's say there are six ways to specify a style.
How to...
| Change font | I generally use the following specifications to change font, font
size, color, etc. You can do it with a single "font" specifier
but I like to be explicit:
font-size: font-weight: color: text-decoration: |
| Change margins | I generally use the following specifications to... |
http://webreview.com/wr/pub/Style_Sheets
Last updated: 26-Feb-1999
marcl@micapeak.com