Tutorials Bugs Masterclass Free stuff Test pages Proposals

RichInStyle.com CSS1 tutorial - Introduction to CSS

Contents

What are style sheets?

Why style sheets?

What reasons are there not to use style sheets?

Using style sheets

What are style sheets

Style sheets provide a means for authors to specify how they wish documents written in a markup language such as XML or HTML to be formatted. For example, an author might wish to specify that a document should be green on pink - this could be done using CSS, an established standard for styling documents.

You might ask 'Why do we need style sheets - can't you use HTML; for example, the FONT element or the bgcolor attribute?'

There are two answers to this:

  1. HTML isn't designed for styling documents - when you write an HTML document, you are specifying only the content that the element contains. Given the piece of HTML: <H1>A heading</H1>, you have specified nothing about the way the element should be rendered, only that the element is a level 1 heading. If you wished to state that you want your headings to be yellow Helvetica, then you could have <H1><FONT color="yellow" face="Helvetica">A heading</FONT></H1>, but this is bad for several reasons:
    1. You have to add that piece of code to each heading that you want styled - this is time-consuming, prone to error and makes files excessively bloated (a typical page styled using HTML will be 1/3rd formatting tags).
    2. If you want to change those headings to pink Arial, then you will have to change each heading individually - a prohibitively laborious task on a large site.
  2. HTML simply doesn't offer sufficient control over document formatting - important formatting effects such as leading (the space between lines), text shadows, and many other facts just can't be done using HTML.

Style sheets solve all of these problems. For example, say you want to make all those headings green Arial - no problem, just type H1 {font-family: Arial; color: green}. What if you want to change all of the headings on your site to red with a blue background? Just change that to H1 {color: red; background: blue}, and, at a stroke, the whole of the site is changed.

What can style sheets do?

To take an example, on this page all of the headings are reddish brown - this is so on every page on RichInStyle.com. This probably adds up to about 10000 headings.

If each of those headings were to be styled with a font element (e.g., <H2><font color="blue">A heading</font></h1>), that would take up 250kb - a significant amount of space. By replacing this with a CSS rule, the whole site can be styled with a single rule amounting to only about 40 bytes.

Assuming an average of 50,000 downloads per page, per month, that works out at a saving of 12.5gb.

That is a lot of saved space, especially if you pay for bandwidth by the gigabyte.

Just imagine the cumulative effect of that across all the margins, colors and fonts on the site, and that is a lot of saved traffic, so not only are cost reduced, but also the download speed will increase for viewers of your pages.

To give another example of the power of style sheets, have a look at RichInStyle.com's personalization page. This allows you, as the viewer of a page, to say 'I don't like the way that that page looks, I'd rather have it white on blue', and for that desire to be fulfilled.

This technology, unique to RichInStyle.com, and only achievable using style sheets, gives you total power, and your users total control (does your user have vision difficulties? - no problem, they can select a high contrast style sheet), and best of all, it is free for download from RichInStyle.com.

Why style sheets?

Style sheets have the following advantages:

  1. They separate content from formatting. This means that instead of making a quotation as italic, you mark it as a quote and then tell the browser that you want all quotes to be italic. This means that it is a two-second job to change quotes to bold, red, green or normal.
  2. They reduce download time by removing formatting information from documents. Thus instead of having to specify that you want Times New Roman a few dozen times in a file for headings, you specify once that you want headings to be Times New Roman. They also are advantageous in that they need only be downloaded once for an entire website.
  3. They give far more control over formatting than HTML - such features as background images and colors on all elements - not just the whole document, etc.
  4. They ensure a consistent appearance across a site - one style sheet can style thousands of pages.

What reasons are there not to use style sheets?

  1. About 1 in 20 users have browsers that do not support style sheets.
  2. Not all browsers support style sheets properly.

Support

Be aware that since CSS is a large specification, most browsers do not support it in its entirety. Given that this is so, don't be surprised if some concepts described in this guide don't work. To see whether they do, you should check out RichInStyle.com's bug table.

Using style sheets

Style sheets allow you to say, for example, that you want headings in 24pt Arial with a pink background, that you want the whole document to have a left margin of 1in, or whatever. The good thing about them is that they are the only way that you can say that you want BODY or P to be displayed in a certain way - in HTML you could make all P tags a certain color using HTML tags, but if you wanted to change them it would take hours on a big website, but seconds using CSS.

For example:

BODY {color: red;
background-color: white;
font-size: 16px;
font-family: Arial;
line-height: 20px;
margin-left: 5%}

As you can see, creating a style sheet is easy, but before you can do that, it is necessary to decide how you are going to apply your style to your page. This topic is covered in the next section.