Poplar ProductivityWare Articles:
Getting Started with Cascading Style Sheets for HTML
by Jennifer Hodgdon

Introduction

This article is a tutorial guide to getting started with Cascading Style Sheets (CSS). Its aim is to get you familiar with the basics of CSS, as applied to HTML-based web pages, and to give you the background you will need for further reading. If you have any suggestions or comments about this tutorial, please contact Poplar ProductivityWare.

To get the most out of this tutorial, you should already be familiar with the HTML language, and know how to edit (using a plain text editor), save, and view an HTML web page. In particular, you should understand the meaning of the HTML term element (an HTML element consists of an opening tag, such as <P>, followed possibly by some text and a closing tag, such as </P>). You should also be familiar with the basic structure of an HTML document (HEAD and BODY elements), and with the common HTML tags (P, A, H1, H2, etc.). If you need more background in HTML, please consult our Resource Guide for Web Developers article to find a tutorial.

Why use CSS?

The main philosophy of CSS is that the logical structure of an HTML document can and should be separated from its page layout and formatting. The idea is to use the HTML tags to specify only the logical structure of your document; for example, P elements are paragraphs, and H1 elements are top-level headers. Then, instead of specifying the fonts, colors, borders, and other layout and formatting parameters using HTML's now-obsolete tags and attributes (FONT, BGCOLOR, etc.), you instead use CSS, which is much more flexible and efficient.


CSS Basics

Linking HTML to CSS

You have a choice, when adding CSS to an HTML page: you can either put the CSS style information inside the HTML file, or you can put it in a separate file. I recommend you use a separate file, because it is likely that you will want to use the same style sheet for more than one page on your web site, and that's much easier (and more maintainable) if it's in a separate file. This tutorial assumes you wish to adopt the separate-file practice; if not, consult the further reading section, below.

CSS information is stored in a plain text file with default extension ".css". If the CSS file you want to use for a particular page's layout is called "mysite.css", and it is located in the same directory folder as your HTML page, then to reference it, put the following LINK element inside the HEAD element of your HTML file:

<LINK HREF="mysite.css" REL="stylesheet" TYPE="text/css" MEDIA="screen,print">

A few notes on the attributes of the LINK tag:

You can also define your main styles in one file, and then add a second linked CSS file to override a few settings for one medium only (e.g. define generic styles for both screen and print, and then override a few styles for print only). The way CSS works, if you have multiple definitions, the last one takes precedence. Here's an example:

<LINK HREF="mymain.css" REL="stylesheet" TYPE="text/css" MEDIA="screen,print">
<LINK HREF="myprint.css" REL="stylesheet" TYPE="text/css" MEDIA="print">

Structure of CSS

Now that your HTML file is referencing your style sheet, you need to put style information into the "mysite.css" file (or "mymain.css" and "myprint.css"), using a text editor. Basically, a CSS file consists of a number of rules for layout, which appear sequentially in the file; if there is duplication, later rules override earlier rules. Each rule has two parts: the selector tells what HTML elements the rule corresponds to, and the declaration supplies the layout and style parameters for those elements. For instance, to specify that the H2 and H3 elements in your HTML document should have red text on a blue background, you would put the following in your CSS file:

H2,H3 {
color:red;
background-color:blue;
}

As you can see, the selector for a CSS rule is simply the HTML tag name; or you can specify more than one tag name, separated by commas. The declaration section is enclosed in curly brackets {}; each individual declaration within the section consists of a property and its value separated by a colon, and the property:value pair is followed by a semicolon. The indentation within the curly brackets is optional, but I recommend you indent your CSS rules, because it does improve readability.


Classes, Spans, and Divisions

With CSS, you have the flexibility to define different styles for H1, H2, P, HR, LI, A, and all other HTML elements, as well as a default style for the entire page (by making rules for the BODY element). Sometimes, however, you will find that even this flexibility is not enough, and therefore CSS provides Classes, Spans, and Divisions for further layout refinement.

Classes

Classes allow you to specify different layout rules for particular occurrences of a given element. For instance, at the bottom of this page, the copyright notice is logically a paragraph, and is therefore specified as a P element in HTML. However, I decided that I wanted to display it in smaller type than the other P tags on the page. To do this, I assigned the paragraph a class attribute in its P tag in the HTML document:

<P CLASS="disclaimer">Copyright (C)...</P>

Then, in the CSS file, I defined a rule for the "disclaimer" paragraph class, using the selector "p.disclaimer" (the tag name p, and the class name "disclaimer" that I chose, separated by a period; you can also just use ".disclaimer" as the selector):

p.disclaimer {
font-size:x-small;
}

You can choose your own names for classes; my advice is to choose ones that seem logical to you, so that in a month or a year when you are modifying your site's layout or adding a page, you won't have to spend a lot of time trying to remember why and how you did what you did.

Spans

Spans are similar to classes, except that they specify a style change for only part of an element (such as one sentence or phrase of a paragraph). For example, if you wanted to put the second sentence of a particular paragraph in italics, you could define a SPAN like this:

<P>First sentence. <SPAN CLASS="second">Second sentence</SPAN> Third sentence.</P>

In your CSS file, in this example, you would then define a rule for the selector ".second":

.second {
font-style:italic;
}

Divisions

Divisions are yet another variation -- they allow you to specify a style for a group of HTML elements as a block; typically, they are used for setting off a section of the page as a navigation bar. For instance, to create the navigation section in the upper-right of this page, I defined a division, using an HTML DIV tag:

<DIV CLASS="beginnav">
<H1>On This Site:</H1>
<P><A HREF=index.html>Home:</A> Read a company overview</P>
...
</DIV>

Then, in the CSS file, I defined the style for this division:

.beginnav {
background-color:#ecf2e6;
...
}

I've left out some details of the HTML and CSS (... is not correct CSS syntax!) -- I'll go over these in the next section.


Layout and Formatting with CSS

Now that you have some background on the structure of CSS, you are ready to tackle the most important (in my opinion) layout and formatting options available in CSS. Each section below is intended to be self-contained, so you can choose the style elements you want to use in your web pages, and ignore the other sections.

Fonts, Colors, and Text Alignment

For each HTML element (or class, span, or division), you can specify fonts, colors, and text alignment. I especially recommend you start by choosing default page colors and fonts, by specifying styles for the BODY element (elements inherit their formatting and layout from the element that contains them, by default, and the entire page is contained in the BODY element.) Here are some CSS properties related to fonts, colors, and text alignment (examples below):

Here are some examples of CSS declarations using the properties above:

color: #3f3e29;
color: red;
background-color: #fff6db;
font-family: Arial, Helvetica, sans-serif;
font-family: Courier, monospace;
font-size: small;
font-size: 12pt;
font-style:italic;
font-weight:bold;
font-variant:small-caps;
text-align:center;

Link Formatting

Hyperlinks in your HTML document have special formatting properties: you can specify different styles for:

Example:

a:link {
color:green;
}
a:visited {
color:maroon;
}
a:hover {
color:blue;
}

Wrapping Text Around Images

Another thing you can do with CSS is to put an image in a corner of a page, and have the page's text wrap around it. For example, you might want to put an image in the upper right corner, and have the text that is even with the image fill in the space to the image's left, and the text below the image go all the way to the right side of the page (scroll to the top of this article to see an example of what this looks like).

In CSS, this text wrapping is accomplished through style rules on the image, and the technique is called "floating" the image. Here is how you do it:

<IMG CLASS="floater" SRC="myimg.gif" ALT="logo">
<P>Text to wrap around this image....</P>
.floater {
float:right;
margin:.25in;
}

Navigation Bars

Navigation bars, such as the one in the upper-right corner of this page, can use CSS's block layout capabilities to specify the placement of the bar. To create a navigation bar:

<BODY>

<DIV CLASS="navbar">
<P>navigation bar content goes here</P>
</DIV>

<DIV CLASS="content">
<P>main page content goes here</P>
</DIV>

</BODY>
.navbar {
position:absolute;
top:0;
left:0;
width:1.5in;
background-color:yellow;
padding:.1in;
margin:0in;
}
.content {
position:absolute;
top:0;
left:1.5in;
padding:.1in;
margin:.25in;
background-color:white;
}
.navbar {
display:none;
width:0;
}
.content {
position:static;
}
<script type="text/javascript">
<!--
if( document.compatMode && document.compatMode == "CSS1Compat" ) {
document.onreadystatechange = function fixIE6AbsPos() {
if( !document.body ) { return; }
document.body.style.height = document.documentElement.scrollHeight + 'px';
}
}
// -->
</script>

Further Reading

The aim of this tutorial was to get you familiar with the basics of CSS; hopefully, it left you with the background and desire to learn about additional CSS features and the details I have left out. I recommend that after finishing this tutorial, you visit the W3C web site -- W3C is the standards body that maintains standards for HTML, CSS, and other Web technologies. You may also wish to consult other resources in my Resources for Web Developers article, or other articles in Poplar ProductivityWare's articles directory. Finally, you may also wish to use your browser's "view source" command to see how this page was generated; here is a link to this page's style sheet file.


Poplar ProductivityWare: your Seattle-area source for web databases, web programming, Drupal modules and sites, and WordPress plugins

Home | Services | Experience | Articles | Downloads | News | About | Contact Us

Poplar ProductivityWare® is a trademark registered in the U.S. Patent and Trademark Office

Copyright (C) 2003-2009 Poplar ProductivityWare LLC

On This Site:

Home: Visit our home page

Services: Find out how we can improve your web site

Experience: See what we've done for other clients

Articles: Get free information on a variety of topics

Downloads: Download free software, plugins, and modules

About: Learn more about Poplar ProductivityWare

Testimonials: See what other clients have said

Values: Learn about the values that govern our business, and read our privacy policy

Contact Us: Locate our phone, email, and mailing addresses


News: Find out what's new at Poplar ProductivityWare

RSSRSS Feed: Keep track of our articles and downloads by subscribing (learn about RSS here)


Search the Poplar ProductivityWare site using Google: