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

CSS2 Reference Guide

CSS2 Basics

Linking to XHTML documents

There are different ways that a style sheet can be applied to a document.

External

An external style sheet can be linked to a document by including the <link /> tag in the HEAD section of that document.

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

This links the style sheet mystyle1.css to the current document. As with other elements which have an href attribute, absolute or relative URLs are allowed.

The @import rule may also be used for linking an external style sheet.

@import url(mystyle2.css);

@import can appear in an external style sheet or between the <style> and </style> tags. Both relative and absolute URLs are permitted. Muliple @import rules can appear in the same style sheet but must be positioned before any other rules. Support for this rule in external style sheets from older browsers is limited.

Embedded

A style sheet may be embedded in the document itself. The syle sheet is enclosed between the <style> and </style> tags which are positioned in the HEAD section.

An external style sheet can be used in conjunction with an embedded one.

<head>
<link rel="stylesheet" type="text/css" href="mystyle1.css" />
<style>
<!--
@import url(mystyle2.css);
h1 {align:center; color:blue;}
-->
</style>

Note the use of the XHTML comment delimiters within the <style> tags. This prevents older browsers which do not support CSS from displaying the style rules in the mistaken idea that they are document text.

Inline

If you only wish to apply a style to a particular element , the style may be 'inlined' by using the style attribute in an appropriate tag, for example, <p style="background-color: white; color: red;">.

Multple declarations may be made, each one terminating with a semi-colon(;).

Cascade Rules

The rules for applying styles where there are multiple style sheets and multiple declarations for the same selector can be quite complicated. The chapter on Cascading goes into the selection process in detail. As a rule of thumb though, an inline style usually wins over an embdedded style or external style. As to the embedded and external styles, it really depends on how specific the rule is and the order. For rules with the same specificity, if the <style> tag follows the <link> tag then the embedded style will win out but if the <link> tag is after the <style> tag then the external stylesheet will take precedence as far as the order goes.

Top

Style Sheet Rules

A simple style sheet rule might look like the following

h1 {align:center;  color:blue;}  /*A comment in a style sheet is enclosed in a /*...*/ combination */

The 'h1' element referred to, is known as a selector and it is this which determines ('selects') the part or parts of the document the style rule is to be applied to. The property/value combination, e.g. 'align:center', is known as a declaration. Each property and value is separated by a colon (:), each declaration must be ended with a semi-colon (;), this is optional on the last in a set but is recommended, and the complete set for a selector must be enclosed in braces ({}).

Where there is no explicit rule governing an element, that element's properties may or may not be inherited from their parent element. This is dependant on the property, as some cannot be inherited.

There are a variety of ways in which you can specify the selector in a rule declaration. A comprehensive list is available in the topic CSS2 Selectors.

Top

A Simple Style Example

Using Classes

h1.regular {color: blue; text-align: center;}
h1.warning {color: red; text-align: right;}

Adding the classes "regular" and "warning" to the following XHTML:

<h1 class="regular">This is a regular heading</h1>
<h1 class="warning">This is a warning heading</h1>

will produce the following result:

This is a regular heading

This is a warning heading

Using <span> and <div>

The span and div elements can be used to apply styles to varying pieces of text. For example, define a class called "brownish".

.brownish { color: #963 }

Applying this class using the span attribute as shown below;

<p><span class="brownish">The first few words</span> of a
paragraph could be in a different color. Style sheets may also be inlined, in this case
to change the style of the word <span style="font-family: 'Monotype Corsiva'">
Monotype Corsiva</span>.</P>

displays the text as follows:

The first few words of a paragraph could be in a different color. Style sheets may also be inlined, in this case to change the style of the word Monotype Corsiva.

Note also the use of an inline style here to change the font using the span attribute.

To apply a style to a block comprising several different elements use the div attribute. These may be nested as in the example below.

<div class="brownish">
<h1>Divisions</h1>
<p>The div element was first defined in HTML 3.2, but with the ALIGN attribute
   now deprecated, it doesn't have any supported attributes.</p>
<div style="font-style: italic;">
<p>Since <span style="font: bold 1em monotype; color: black;">DIV</span>
   may contain other block-level containers, it is useful for marking large sections
   of a document, such as this note.</p>
<p>Style sheets must be used to provide formatting.</p>
</div>
</div>

displays as follows:

Divisions

The DIV element was first defined in HTML 3.2, but with the ALIGN attribute now deprecated, it doesn't have any supported attributes.

Since DIV may contain other block-level containers, it is useful for marking large sections of a document, such as this note.

Style sheets must be used to provide formatting.

Top

Valid XHTML 1.0! Valid CSS2