Syntax Of And Using CSS

CSS was created to have a fairly simple syntax and means of adding to a webpage. The latter uses either the link element explained in Comments, HTML, Head and its Children, Body or another element that is essentially the style attribute's big brother.

Syntax of CSS

CSS is made up of a series of rules, which have a very simple syntax:

Basic Rule Syntax
selector{
/* Declarations as needed (see explanation below) */
}
selector
This tells the browser what elements to select.
{
Each selector has a group of zero (yes, zero) or more declarations associated with it. { marks the beginning of the list of declarations.
declarations
These work exactly as they do in the style attribute described in Attributes. However, I will give you a reminder: property:value;
property
The aspect of the element you want to control (for example, text colour or background).
:
Separates the property and the value.
value
What you want that property to be (for example, what colour you want the text or background).
;
Ends the declaration, which allows you to add more declarations as necessary.
}
This marks the end of the list of declarations.

If you forget the selector or braces, strange things will happen to your webpage. Luckily, the W3C has a CSS validator which will highlight errors. It can be found at http://jigsaw.w3.org/css-validator/.

A good example of a CSS rule is the one for caption elements:

Example CSS Rule
caption{font-size:0.8em; font-weight:bold;}

Basically, this means that the dd element doesn't have any extra space around it. Now, you might be thinking that what's between { and } looks a lot like the contents of the style attribute. It certainly should: the style attribute by definition contains CSS.

Adding CSS Comments

As in (X)HTML, you can have comments in CSS.

/*
Starts the comment
*/
Ends the comment.

Adding Style Sheets

For a stylesheet to have any effect on a webpage, the (X)HTML document must either include an internal style sheet or be linked to an external style sheet.

Internal Style Sheets And The Style Element

Element Name: style

An internal style sheet is a stylesheet contained in the (X)HTML document itself. Specifically, an internal style sheet is contained in the style element, itself a child of the head element.

You may have as many style elements as you like.

A major drawback of internal style sheets is they only affect the webpage in which they are included; they must be repeated for every (X)HTML document for which you want to use the style sheet.

Required Attributes

type
This attribute tells the browser what type of code is contained within the style element. The value should always be text/css.

Implied Attributes

media

This contains a space-separated list of possible media. Possible media are:

audio
speech
What the user hears when hearing the page through a screen reader.
braille
embossed
For tactile devices (which are felt).
handheld
For hand-held devices, which have smaller screens.
print
What you get when you print out a webpage.
projection
For pages intended to be projected onto a screen (like a slide show presentation).
screen
What you see on a computer monitor.
tty
For media where characters have a fixed width (for example, i is exactly the same width as m.)
tv
For WebTV, viewed through a television screen (limited scrollability, colours, etc.).

Note that audio/speech and braille/embossed have absolutely nothing to do with visual appearance. By the way, if you omit the media attribute, it's the same as writing media="all": the sheet will apply (or try to apply) to all media types.

title
This attribute does the same job as it does in any other element, but in this context, it really doesn’t do much, so it’s usually omitted.

The language attributes may also be used with this element.

Note: I'm not going to explain CSS for tactile devices solely because I'm not aware if such CSS exists. Audio CSS does exist as of CSS version 2, and gets its own chapter.

Forbidden Attributes

The core attributes—aside from title, as mentioned above—may not be used with the style element.

The style Element In XHTML

In HTML, you can include the stylesheet as simple content of the style element.

XHTML, being an XML-based language, is a bit more complex: anything you don't want treated as markup has to be contained in a character data section. In any XML document, character data is read as raw text, not markup. Therefore, it will not try to interpret CSS as markup, which is a good thing, since CSS is a very different language—in fact, one old trick to keep older browsers from trying to interpret CSS as markup was to put it into an HTML comment.

A character data section starts with the character sequence <![CDATA[ and ends with ]]>. Do note that these sections are not elements!

Depending on how your XHTML document is read, you may want to use CSS comments to make sure that the starting and ending character sequences aren't read as CSS. If the document is an HTML document, then the character sequences, since they are inside the style element, will be read as CSS. If the document is an XHTML document, then character data sections are an integral part of them, and the character sequences will not be interpreted as CSS or anything other than the beginning and end of a character data section, so they don't need to be commented out.

Character Data Sections In XHTML
Read As HTML
<style type="text/css">/*<![CDATA[*/
/*]]>*/</style>
Read As XHTML
<style type="text/css"><![CDATA[
]]></style>

One excellent way around any confusion arising from this is to use an external file for your CSS.

External Style Sheets

An external style sheet is the result of taking the contents of an (X)HTML document's style element and placing it in a separate text file. This file must use the extension .css. It is important not to include the style tags or character data section code in an external style sheet.

Unlike internal style sheets, an external style sheet will affect any (X)HTML document that refers to it, which means the style sheet only needs to be created once.

Linking To An External Style Sheet

Creating a reference to an external style sheet requires the link element, which needs to use specific values for certain attributes for this purpose.

media
I didn't mention the media attribute for the link element before since it normally has little effect. However, in this context, it works just like the media attribute for the style element.
type
You can actually skip this, but if you do use it, the value, like that of the style element, should be text/css.
rel
The value must be stylesheet, which lets the browser know that the reference is to a style sheet. If it's something else, your CSS won't apply.
Example link Element Referring To Generic External Style Sheet
<link rel="stylesheet" type="text/css" href="../../style.css">

It's important to remember you may link to as many style sheets as you like, or even have external and internal style sheets for a single document. (This comes in very handy when creating stylesheets for separate media). Just remember: The stylesheets that come before are overridden by stylesheets that come afterwards.

Setting The Default Style Language

Earlier in this book, I mentioned that you could set the default webpage language (text/html) through the meta element. You can do that with style, too. The code goes like this:

The meta Element Setting Default Stylesheet Language
<meta http-equiv="Content-Style-Type" content="text/css">

This tells your browser and your server what your style sheets are coded in.

Arranging A Style Sheet

Unlike an (X)HTML document, the contents of a style sheet don't need to be in any particular order to be valid CSS. For a style sheet to work properly, however, the order of rules is important. Remember that CSS stands for Cascading Style Sheets. Since a style sheet is read from top to bottom, what comes later in the page can override what was written earlier. Therefore, the best way to arrange the rules is to have the most general rules (which are, well, the rules) at the top and the most specific rules (which are the exceptions) nearer the bottom. It is important to note that exceptions only override what you tell them to. If you use CSS to set a standard height, width, colour, and border for an element, then create an exception because you want a different colour, you only need to specify colour. The rest has already been defined by the more general rule.