Contents

  1. Introduction
  2. History
  3. Web Standards
  4. Structure and presentation
  5. (X)HTML
  6. CSS
  7. Accessibility
  8. URLs
  9. References
  10. Glossary

6. CSS

Having been used mostly to specify font properties, CSS can now be used to control the entire layout of a document. However, to do this efficiently requires a somewhat different approach than when tables are used to control layout.

Structured, semantic XHTML is necessary for CSS to be able to control layout in an efficient way.

Browser support

As mentioned earlier, browser support for CSS has been improved a lot in the last few years. Unfortunately all browser vendors don’t seem to be interested in implementing open standards, so the level of support varies from browser to browser. On top of that there are software bugs that make browsers act in ways they shouldn’t be.

Currently (2004), the web browsers that have the best CSS support are Mozilla (and other browsers based on Gecko: Firefox, Camino, Netscape 6+), Opera and Safari (and other browsers based on WebCore: OmniWeb 4.5 and later). Internet Explorer 6/Win doesn’t have the same level of CSS support, but it will let you do most basic stuff. Internet Explorer 5/Mac has very good support for CSS 1, but it doesn’t support a lot of CSS 2. IE 5.* for Windows supports quite a bit, but has some problems that you need to be aware of. Earlier versions of Internet Explorer have no support worth mentioning. The same goes for Netscape versions earlier than 6.

Since currently a majority of people use Internet Explorer for Windows, you will often have to let that browser be the lowest common denominator. That is not to say that you cannot or should not use the abilities of browsers with better CSS support to enhance the design for them.

Not all browsers currently in use have the level of CSS support required to render a website which makes full use of CSS for layout in a graphically attractive way. Luckily, on most websites, only a few percent of the visitors are using a browser too old to render a CSS-based layout properly.

It’s important to note that these people will not be locked out. In the nineties, browser check scripts were a popular way of redirecting anybody using the “wrong” browser (normally anything but Internet Explorer for Windows) to a page that told them to upgrade their browser to gain access to the site.

These days you can handle unsupported browsers in a better way. One big advantage of using logical, semantic XHTML is that it makes documents accessible and usable even without CSS. The presentation — the way the document looks — won’t be the same as in a supported browser, but the content will be there. In most cases, for most visitors to a site, the content is actually more important than the way it is presented. That’s why it is better to send an unstyled page to unsupported browsers than to lock them out of the site.

There are different ways of doing that. One of the most common ways is to use @import to link to the associated CSS file. Netscape 4 and older browsers don’t understand @import and will not import the CSS file. There are plenty of ways to hide CSS from web browsers. The thing most methods for hiding CSS have in common is that they are based on bugs in the way browsers interpret CSS code. That means there will always be a risk that a browser update fixes the bug that is used to hide CSS from it, but not the CSS problem that was the reason for hiding certain CSS from that browser. So, the less you rely on CSS hacks, the better.

Obviously you can use server side technology to do a browser check and then send different CSS files (or none at all) to different browsers. If you do that you will have to make sure the detection script is kept current to avoid sending the wrong CSS file when a browser is updated or a completely new browser is released.

Read more:

Different ways of applying CSS

There are several ways of applying CSS to elements in an HTML document.

External

There are several advantages to keeping all CSS rules in one ore more separate files. The HTML documents become smaller, CSS files are stored in the browser cache and will only need to be downloaded once, and you only need to edit a single file to change the presentation of an entire website. An external CSS file can look like this:

h1 {
    font-weight:bold;
}

Note: there is no <style> element in an external CSS file.

You can link a CSS file to an HTML document by using a <link> element:

<link rel="stylesheet" type="text/css" href="styles.css" />

or by using an @import rule in a <style> element:

<style type="text/css">
@import url("styles.css");
</style>

Inline

By using the attribute style, you can apply CSS directly to an HTML element:

<h1 style="font-weight:bold;">Rubrik</h1>

This should be avoided since it mixes structure and presentation.

Internal

Internal CSS is contained in a <style> element, which in turn belongs in the document’s <head> element:

<style type="text/css">
h1 {
    font-weight:bold;
}
</style>

This should also be avoided, since it is best to keep HTML and CSS in separate files.

Read more:

CSS Syntax

A CSS rule consists of a selector and one or more declarations. The selector determines which HTML element or elements the rule affects. Each declaration consists of a property and a value. The declaration block is surrounded by { }, and each declaration ends in ; (semicolon).

A simple CSS rule looks like this:

p {
    color:#0f0;
    font-weight:bold;
}

In this case, p is the selector, which means that the rule will affect all <p> elements in the document. The rule has two declarations, which together will make all text contained in a <p> element green and bold.

For a more detailed description of CSS rules, see for example CSS Beginner’s Guide, CSS from the Ground Up or a CSS book.

Read more:

Superfluous elements and classes

When starting out with CSS, it’s common to make the mistake of using unnecessary XHTML elements, superfluous classes, and extra <div> elements. This doesn’t necessarily mean that the code will be invalid, but it counteracts one of the reasons of separating structure from presentation; to get simpler, cleaner markup.

An example of using unnecessary XHTML elements:

<h3><em>Headline</em></h3>

If you want to make the headline italic, use CSS to restyle the <h3>< element:

h3 {
    font-style:italic;
}

Superfluous usage of classes can look like this:

<div id="main">
    <div class="maincontent">
        <p class="maincontenttext">
            Lorem ipsum dolor
        </p>
    </div>
</div>

This will do fine:

<div id="main">
    <div>
        <p>
            Lorem ipsum dolor
        </p>
    </div>
</div>

To control elements contained in div#main you can use contextual selectors in the CSS code. In this case it could look like this:

div#main p {
    /* rules */
}

In most cases CSS can be used to style logical XHTML the way you want without having to add any extra markup. However, there are cases when adding some extra code is worth it.

CSS tips

Obviously, once you start using CSS seriously, you will eventually run into problems of some kind. Some problems may be caused by misunderstandings, others on unclear specifications or buggy browsers. The CSS Crib Sheet is a collection of good advice, compiled by Dave Shea. Here are some of the most important tips plus a few more that aren’t in the CSS Crib Sheet.

CSS Layouts

There are many examples and step-by-step tutorials on how to use CSS for layout. A good advice is to start with a simple layout, learn how it works, and then move on to more advanced layouts.

Read more:

Comments, questions or suggestions? Please let me know.

© Copyright 2004–2006 Roger Johansson