XML technologies / CSS / CSS Cookbook / General topics / Using class and id attributes correctly

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Problem

You want to know when to use attributes class and when attributes id are preferable. These attributes mark particular parts of a document, which can then be targeted with corresponding CSS selectors.

Solution

Use attributes class when the same style should be used several times in a document. id attributes are used when a style should apply to just one element in a document.

The following stylesheet uses the ID selectors #banner, #sub_banner, #nav1, #nav2, #footer and #content; .title and .content are class selectors:

body {
  margin: 0;
  font-family: Verdana, Arial, Helvetica, sans-serif;
  font-size: .75em;
  padding: 0;
}
#banner {
  margin-top: 0;
  margin-bottom: 0;
  background-color: #900;
  border-bottom: solid 1px #000;
  padding: 5px 5px 5px 10px;
  line-height: 75%;
  color: #fff;
}
#sub_banner {
  background-color: #ccc;
  border-bottom: solid 1px #999;
  font-size: .8em;
  font-style: italic;
  padding: 3px 0 3px 10px;
}
#content {
  position: absolute;
  margin-left: 18%;
  width: 40%;
  top: 100px;
  padding: 5px;
}
#nav1 {
  position: absolute;
  width: 30%;
  left: 60%;
  top: 100px;
  padding: 5px;
}
#nav2 {
  position: absolute;
  padding: 5px 5px 5px 10px;
  top: 100px;
  width: 15%;
}
#footer {
  text-align: center;
  padding-top: 7em;
}
.warning {
  font-weight: bold;
  color: red;
}
.title {
  font-size: 120%;
}
.content {
  font-family: Verdana, Arial, sans-serif;
  margin-left: 20px;
  margin-right: 20px;
}
.footer {
  font-size: 75%;
}

Here is how the attributes id and class are used in HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>CSS Kochbuch</title>
    <link href="rezept-1-3.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <div id="header">
      <h1>CSS-Sammlung</h1>
      <h2>Einige CSS-basierte Websites</h2>
    </div>
    <div id="content">
      <h3>Titel für Seiteninhalt</h3>
      <p class="title">Titel für Inhaltseintrag</p>
      <p class="content">Hier kommt der Inhalt hin.</p>
    </div>
    <div id="navigation">
      <h3>Listen</h3>
      <a href="http://csscookbook.com/">Site anmelden</a><br />
      <a href="http://csscookbook.com/">CSS-Ressourcen</a><br />
      <a href="http://csscookbook.com/">RSS</a><br />
      <h3>CSS Kochbuch-Themen</h3>  
      <a href="http://csscookbook.com/">Home</a><br />
      <a href="http://csscookbook.com/">Über uns</a><br />
      <a href="http://csscookbook.com/">Blog</a><br />
      <a href="http://csscookbook.com/">Dienstleistungen</a><br />
    </div>
    <div id="blipverts">
     <h3>Platz für Werbung</h3>
    </div>
    <div id="siteinfo">
      <p class="footer">Copyright 2006</p>
    </div>
  </body>
</html>

Discussion

ID selectors (id="Wert") refer to attributes occurring exactly once in the document tree. Class selectors (class="Wert") refer to attributes that may be used multiple times on a page. Remember: ID selectors use a hash sign (#), while class selectors begin with a dot (.).

ID selectors therefore mainly address sections occurring only once in a document. The previous example divides the page into these sections:

  • header (header)

  • content (content)

  • navigation (navigation elements)

  • blipverts (advertising banners, etc.)

  • siteinfo (site information)

Assigning distinct attributes id lets designers style these areas without affecting other page parts, by combining descendant and ID selectors.

In the following example, the two elements h3 receive different CSS rules:

#content h3 {
  font-size: 2em;
  font-weight: bold;
}
#navigation h3 {
  font-size: 0.8em;
  font-wieght: normal;
  text-decoration: underline;
}

See also

The CSS 2.1 specification on ID selectors and the CSS 2.1 specification on class selectors.

  

 

 

 

back to top