Library tutorials & articles

Using Cascading Style Sheets (CSS)

Syntax of Styles

Style sheets contain a list of styles, known as rules. The general syntax of a style is as follows:

selector
{
  property: value;
}

The selector may either be an existing tag, or a class. The following example has a selector of p, a property of color, and background, with the values #00f and transparent respectively.

p {
  color: #00f;
  background: transparent;
}

The means that when the <p> tag is used, the colour will be applied from the above style definition.

Using Comments

You can add comments to your style sheets. Comments are not only useful for reminding yourself what a particular style does, but also useful for other project members if you're working in a project group. A comment is anything between /* and */.

p {
  /* Set the colour to Blue for the paragraph tag */
  color: #00f;
  background: transparent;
}

Grouping Selectors

If you want to apply a style to a range of selectors, you can do so by separating them by commas in the selector section. The following example sets the foreground and background colour for a paragraph, and a table column.

p, td {
  color: #000;
  background-color: #fff;
}

Classes

The class attribute is uselful for defining different styles for the same tag. The name of the class is separated from the tag using a period. The class name is then specified to determine which class of the tag is required in the HTML document.

The following example defines three classes of style to be used with the <p> tag.

p.red {
  color: #f00;
  background: transparent;
}
p.green {
  color: #0f0;
  background: transparent;
}
p.blue {
  color: #00f;
  background: transparent;
}

The HTML document can now reference the class of the <p> tag to apply that particular style.

<p class="red">
  This paragraph is red
</p>
<p class="green">
  This paragraph is green
</p>
<p class="blue">
  This paragraph is blue
</p>

If you want to define a class that can be used on a range of tags, you can do so by not specifying the tag name as in the following example.

.whiteOnBlue {
  color: #fff;
  background-color: #39C;
}

You can then use the class in an appropriate html tag. The following example uses the whiteOnBlue class in a <p> tag.

<p class="whiteOnBlue">This text is white, on a blue background</p>

The next example uses the whiteOnBlue class in a <td> tag.

<table border="0" cellpadding="5" cellspacing="0">
<tr>
  <td>Left</td>
  <td class="whiteOnBlue">This text is white, on a blue background</td>
  <td>Right</td>
</tr>
</table>

ID Selectors

All id attributes in an (X)HTML document must be unique. You may specify styles for an id by specifying a # immediately before the id value. This effectively allows you to specify a style for a single instance of an element in the document tree, which is useful for laying out documents.

The following example defines a style for a paragraph with an id of "firstPara".

p#firstPara {
  font-size: large;
}

The style would then be used in the document as follows.

<p id="firstPara">
  This paragraph has been specified with an id attribute, and so may only appear once in the document.
</p>

The following example specifies styles that may be used for layout purposes.

#navBar {
  float: left;
  width: 15%;
  color: #000;
  background: #fff;
  border-right: 1px solid #cc9;
  border-bottom: 1px solid #cc9;
  padding: 10px;
}
#mainContent {
  float: left;
  width: 80%;
  color: #000;
  background: #fff;
  padding: 10px;
}

The styles may then be used in the document to create a two column effect as follows.

<div id="navBar">
  Navigation links in here
</div>
<div id="mainContent">
  Main content in here
</div>

Comments

  1. 30 Jul 2003 at 03:36

    This is a good tutorial as it covers the basics.


    Nevertheless, readers should know that CSS can be taken to the extreme and should know the other side of the story in regards to fully integrated CSS.


    One of the points to always remember in any technology is the K.I.S.S. principle.


    Tables versus fully CSS (i.e. replacing all table tags with div tags)


    http://www.decloak.com/Products/Dreamweaver/NestedTemplates/TablesOrLayers.aspx

  2. 01 Jan 1999 at 00:00

    This thread is for discussions of Using Cascading Style Sheets (CSS).

Leave a comment

Sign in or Join us (it's free).

Gez Lemon I'm available for contract work. Please visit Juicify for details.

Related podcasts

  • Despise the listener

    We have a great lineup this week: Paul talks about getting things done in web design and an alternative approach to your reading list. Marcus explains the exciting area of insurance for web designers and we have Andy Clarke on the show to give us an update on CSS 3.

Events coming up

  • Nov 23

    Dan Cederholm Workshop

    London, United Kingdom

    Handcrafted Bulletproof CSSwith Dan Cederholm of "Simplebits"Learn how craftsmanship can apply to the intangibility of web design. Dan Cederholm will guide you though a case study, pointing out the details that matter most when designing fle...

We'd love to hear what you think! Submit ideas or give us feedback