Introduction Basics Selectors Values Cascade Rules CSS Box Model Margins, Padding & Borders Float Example Position Example Further Reading Site Home

CSS2 Reference Guide

Introduction

This guide provides a more in-depth look at Cascading Style Sheets (CSS). As CSS is now the standard for web page design it is important that the underlying concepts and some of the techniques are understood.

This guide actually started life as a chapter in the XHTML Guide but soon outgrew itself and consequently I was forced to create the document you are now reading. The XHTML Guide contains information on the basics of CSS, all of which are covered here in more detail.

This guide cannot hope to comprehensively cover this subject so I do recommend that anyone seriously using style sheets indulge in further reading on the subject. It is my intention however, when I have the time, to add more topics to this guide and expand current ones. This guide covers the basics of the CSS Box Model (the underlying concept required for page layout), with examples of two techniques for multi-column layout. It also contains full details on CSS Selectors and Cascade Rules.

What is a Style Sheet?

A style sheet is a set of formatting rules that control the appearance of a document. One style sheet can be applied to any number of documents to give them all a common look. Properties such as fonts, colours and alignments can all be specified in a style sheet.

The purpose of a style sheet is to separate the content from the presentation. This leads to an uncluttered document without hundreds of tags and attributes being present to control its formatting and presentation. Tables have historically been used to determine the layout of a page, but this can be achieved just with the use of style sheets.

There are various levels of style sheet as specified by the W3C (World Wide Web Consortium), some of which may or may not be implemented on all browsers. Their first specification, Cascading style sheet level 1 (CSS1) is a fairly safe bet for most browsers, but CSS2, the latest offering at the current time (December 2005), although it may not be fully supported, is the level we are aiming ait here. Some of the more esoteric properties defined in CSS2 are not currently supported by any browser. The specification for CSS3 is currently being worked on by the W3C.

Browsers

Despite the efforts of the W3C we are unfortunately left entirely in the hands of the browser providers. Many browsers are not fully compliant with the W3C standards and amongst those that are, some standards are interpreted slightly differently with the same document being rendered inconsistently. This is not so much of a problem with HTML itself but is a big big problem where style sheets are concerned and also with script support.

There are several browsers on the market, the major ones being Microsoft's Internet Explorer(IE), Mozilla, Mozilla Firefox, Netscape and Opera. Of these by far the most popular one is currently Internet Explorer, purely for marketing reasons not performance, and unfortunately, out of these major players, Microsoft is the worst offender in respect of following standards.

Because of its big hold on the market, websites should be designed with Internet Explorer in mind. But don't use IE to develop your page! It is best to use a standards-compliant browser and then tweak it for IE. The advantage of this is that you can see how the page actually should look before massaging it into shape for IE, you won't have to unpick a whole load of rules to make it work in other browsers and it also future proofs it for the time that IE is standards compliant (IE7 maybe?).

Also, just for everyday browsing, each of the other browsers mentioned above has big advantages over IE. Of these browsers, the newest kid on the block, Mozilla Firefox, is the recommendation. It is easy to install, easier to use than IE, a lot more secure, has a load of really useful add-ons and of course it's free.

Get Firefox!

Validation

Validating your XHTML and style sheets is very important and there's no reason not to do it. If your page isn't behaving as expected it may be a closing tag missing, a semi-colon missing off the end of a rule or whatever. Validation ensures that any problems are not due to invalid code or typos. There are a few sites which offer validation giving you various options of referencing the code. The following table summarises these options for a couple of sites.

  Code referenced by
Site Address Type URL Local copy   copy/paste
W3C http:validator.w3.org XHTML a tick a tick
http://jigsaw.w3.org/css-validator/ CSS a tick   a tick
WDG http://www.htmlhelp.com/tools/validator/ XHTML a tick a tick a tick
http://www.htmlhelp.com/tools/csscheck/ CSS a tick a tick a tick

W3C - World Wide Web Consortium
WDG - Web Design Group

Get Out Clause

Although every effort has been made to guarantee the accuracy of this guide, there may be the odd porky or ambiguity and so if anyone finds any glaring errors please email me and let me know.

Top

Valid XHTML 1.0! Valid CSS2