A Very Brief Introduction to CSS:

(intermediate)
     


CSS (Cascading Style Sheets) has been around for quite a while, but browsers are only now beginning to implement it fully. Netscape 6, as well as Internet Explorer 6 and Opera 5 have made great strides in implementing this technology; Netscape 4.* however, is extremely buggy when it comes to rendering CSS correctly.

CSS gives you:

  • Vastly increased Design and Layout Capabilities
  • the ability to change the look of an entire website through one external .css document change
  • Dynamic HTML: CSS-P and JavaScript hold almost unlimited possiblities!

All we have time for at this point are a few basic style items, that can be applied to block-level html elements in-line (in other words, without an external or embedded style sheet to depend on) (and, in case you forgot: block-level elements are P, DIV, BLOCKQUOTE, Hx, etc)

Try copying this line of HTML into a new page:

<H1 STYLE="font-size: 48pt; font-family: Verdana; color: #CC0033"> Hey, we got Styles!</H1>

And this:

<DIV STYLE="background: #333333; font-weight: bold; color: #990000">
<P>This is cool!.</P></DIV>

<P>Styles are our friends!</P>

For styling just a word or two, or even just a letter, use the <SPAN> element:

<P>Highlighting <SPAN STYLE="background: yellow; font-weight: bold; color: #000066">Fun</SPAN> - isn't it? </P>

Note the difference in markup from regular html!

To change the size and color of a line of text:

<P style="font-size: 24pt; color: #FF3333">

Result of your style

</P>

To add a highlighter-type background to a line of text:

<H2 style="background-color: #FFFF99">

Your text here

</H2>

There is, unfortunately, no time here to delve into styles to any extent: Eric Meyer's book CSS: The Definitive Guide (O'Reilly Press) is a highly recommended reference.

Watch out: Netscape Navigator and Internet Explorer will often display styles in different ways - always check!


A style declaration consists of attribute names followed by a colon (:) and the attribute's VALUE. Multiple attributes are separated by a semicolon (;). For example:

font-weight: bold;
font-size: 16px;
font-family: sans-serif;

color: #990099;

One of the most difficult things to get used to in html4 is that elements that had been tags are now attributes of a style.

Embedding a style into an html page will apply that style to the entire page - but only the page (not, i.e., other pages within the same site).
Perhaps you want a certain look for your homepage only, but not for any of the other pages in the site.
In this case, you would use and embedded style sheet in the <HEAD> of the page.

Here is an example for an embedded style; it's always between <STYLE> tags in the document <HEAD>:

<HEAD>
<STYLE>
<!--
H3 {font-size:14pt; text-align:center}
P {font-size:12pt; font-family:sans-serif; color="#333333}
//-->
</STYLE>
</HEAD>

You can apply the same declaration to a group of selectors by including all of the selector names separated by commas. For example:

<STYLE>
<!--
H1, H4, BLOCKQUOTE {color:ivory; font-family:sans-serif;}
//-->
</STYLE>


An external style sheet applies the styles contained in it to all the pages that link to the style sheet.

A style sheet has the extension .css; it's best to create it in a plain text editor such as NotePad or SimpleText; just give the extension .css when you save the file. That extension doesn't mean anything: it simply tells you and anyone else who may be working on your site that this is a styles-file.

In addition, there is no need for <STYLE> tags in your .css document (in fact, including them may on occasion cause rather weird problems).

Once you've created your style sheet, you can link your pages to it. by adding a <LINK> tag to the <HEAD> section of your html file. For example, these tutorial pages use the following link:

<LINK REL="stylesheet" HREF="wtt.css" TYPE="TEXT/CSS">

To apply this style sheet to other pages in your website, you need only add the <LINK> tag to the head of all the documents you want to link.

WATCH OUT: If a style in a linked style sheet conflicts with an embedded one, the embedded one takes precedence. Inline styles take precedence over both embedded and linked styles: it's what puts the 'cascade' into Cascading Style Sheets.

Elements on a webpage are nested within each other; the <BODY> tag is the one that contains all the other elements on the page. Any element that contains others is the parent of the descendant elements.

Any style defined for a parent tag is transferred to the tag's descendants in a principle known as inheritance.
You override this inheritance by specifying a different style for the element's descendants - i.e.:

BODY {color:blue}
H1, H2 {color:#9933FF}

-which would make the Header1 and 2 text (and descendants of the Headers) a kind of garish purple - while keeping the color blue for all the rest of the page text.

Have a look at the many websites dedicated to CSS (just do a search on Google for CSS and the Web)!

Have fun!