CSS Starter Kit

For those of you who don't know what CSS is, here's a primer! Just enough to get you started, not the real 'hardcore', pushing the edge, super-compatibility stuff. Knowledge of basic HTML is assumed.

See-Ess-Ess?

CSS Stands for Cascading Style Sheets. Let's start with the easy part, working backwards. Sheets as in 'pages'. CSS can either be in a seperate page, or as part of an HTML document.

Style. Here's the heart of the matter: CSS deals with the appearance or arrangement of HTML elements. How they look, how they are positioned, how (or if!) they are displayed. Typical HTML changes the appearance of the elements through formatting tags (<b>, <i>, <h1>) or tag attributes (WIDTH=, BGCOLOR=, ALIGN=). On the most basic level, CSS redefines HTML tags; saying "whenever you see tag X, draw it this way.". Through CSS 'classes', you can define several ways to display any given tag.

Now... Cascading? Merriam-Webster Online defines cascade as "something arranged or occurring in a series or in a succession of stages so that each stage derives from or acts upon the product of the preceding".

That's a very appropriate definition. Not only can CSS build upon and override the default style for a tag, successive CSS is cumulative and can overide all or part of previous CSS definitions (more on that in the next section.)

What? Where?

Look at the that title above. It should be green, spaced out, and have a line over it. But if you look in the source of this page you'll only see
<h2>What? Where?</h2>
But what you see is not a normal Level 2 Heading. It looks that way because in my style sheet is a entry that looks like this:
H2 {
    MARGIN-TOP: 2em;
    FONT-WEIGHT: normal;
    FONT-SIZE: 150%;
    MARGIN-BOTTOM: 0;
    MARGIN-LEFT: 0.5em;
    COLOR: #393;
    FONT-FAMILY: haettenschweiler, impact, verdana, tahoma, arial, helvetica;
    LETTER-SPACING: 0.6em;
    TEXT-DECORATION: overline
}
First, the tag: H2. Then a opening brace { followed by a series of attribute/value pairs. The attributes are separated from the values by a colon : and the pairs are separated by semicolons ;. The whole thing wraps up with a closing brace } after which the style for another tag can be defined.

Let's walk through the style attributes for this one, just to get the idea:

We could also define a 'class' by starting with a period and making up a name:

.Atten {
    FONT-SIZE: 110%;
    COLOR: #900
}
We can now apply that style to almost any tag by adding the CLASS= attribute to it:
<B CLASS=Atten>ATTENTION!</B>
Renders as ATTENTION! Further more, if we wanted to ONLY define the 'Atten' class for use with B, we could have started the style definition with B.Atten (note: no spaces!) In the B tag in the document would look the same, but <I class=Atten> would have no effect.

Ok, that's a enough about 'What?'. Now let's wrap up this lesson with a look at Where you put CSS in your document.

The ideal answer to that question is: in a document of it's own. The above bit of CSS is in the default stylesheet for my site, which is called bk.css. In each page that uses it, the following line or one like it appears in the <head> section of the document:

<link rel="stylesheet" type="text/css" href="bk.css">
This associates the stylesheet with the document. The great thing about this method is that all the settings are in one place. My level 2 headings used to be a browninsh red. One day decided I wanted them to be green. So I simply opened my css file and changed color: from #933 to #393 and--violá!--all the H2's on all the pages became green!

Now if you can't or don't want to use this method, or if you have settings that only need to be used on one page, you can define a <STYLE> block in the <head> section of the document:

<STYLE type="text/css"><!--
/* Style Definitions Here */
--></STYLE>
Here's where the first of that cascading stuff starts to kick in. My css file has a definition for the STRONG tag that looks like this:
STRONG {
    FONT-WEIGHT: bold;
    FONT-SIZE: 105%;
    COLOR: #0405b2;
    FONT-FAMILY: Arial
}
Which, when rendered, looks like THIS. But this document not only imports my css file, but has a STYLE block of it's own. That block contains this definition:
STRONG {
    font-family: Times New Roman, Times Roman;
    background:#ffc;
    border-bottom:1px dotted #960;
}
Which makes STRONG look like THIS. Color, font-weight, and font-size carry over from the original definition, but the font-family definition on this page override the one in the style sheet. background and border-bottom are added on top. The new strong tag looks smaller because of differences between the font faces, but it is still slightly larger than normal 'Times' text would be. (Note also that font-weight:bold is a little redundant anyway, because mose systems render <STRONG> the same as <B> anyway.)

This page also contains a special class:

.HiVis {
    color:#ff0;
    background:#f00;
    border-bottom:none;
}
Which makes <STRONG CLASS="HiVis"> look like THIS. The three attributes of HiVis override the ones in the local STRONG definition. font-family is still Times (as defined on this page), and the font-size is still coming from the external css file.

Finally, for 'one-time' use, you can use the STYLE= attribute in any tag. For example, <B STYLE="font-style:italic">Hello!</B> renders as Hello! The STYLE= attribute can be used in conjuction with the other methods. You guessed it: we can layer it on again with <STRONG CLASS=HiVis STYLE="background:#00f;"> and get text that looks like THIS, which includes elements of all four style definitions.

Cascading, indeed!

Lagniappe*

Well, there's one more twist on 'Cascading' that should be thrown in. We can set special definitions when tags are nested:

H1 STRONG {
    color:#090;
    background:#ff0;
    border:3px groove #090;
    padding-left:.5em;
    padding-right:.5em;
}
This sets a special 'class' of STRONG that is automatically used only when STRONG is nested inside an H1 tag. Wanna see what it looks like? Scroll to the top of the page and look at 'CSS' in the title. Note that the font-family is still Times New Roman, as definied in the 'normal' STRONG tag for this page.

I should point out that only a space separates the H1 and the STRONG in the definition. If there was a comma, the style definition would be applied to the default styles for both H1 and STRONG.