XML technologies / CSS / CSS Cookbook / General information / Different selectors for assigning styles / Class selectors

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

If you often want to apply the same CSS rules to different elements, use a class selector.

For example, a class selector can display warnings in red in both links and list items by assigning both the same class, as shown below.

To create a selector for the class warning, prefix its name with a dot (.):

<html>
  <head>
    <title>CSS Kochbuch</title>
    <style type="text/css">
    <!--
      * {
          font-family: verdana, arial, sans-serif;
      }
      body { 
      }
      h1 {
        font-size: 120%;
      }
      #navigation {
        border: 1px solid black;
        padding: 40px;
      }
      li a {
        text-decoration: none;
      }
      p {
        font-size: 90%;
      }
      .warning {
        font-weight: bold;
      }
    -->
    </style>
  </head>
  <body>
    <h1>Seitenüberschrift</h1>
    <p>Dies ist ein Beispielabsatz mit einem <a href="http://csscookbook.com">Link</a>. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna <em class="warning">aliquam erat volutpat</em>. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>
    <ul id="navigation">
      <li><a href="http://csscookbook.com">Äpfel</a></li>
      <li><a href="http://csscookbook.com">Bananen</a></li>
      <li><a href="http://csscookbook.com">Kirschen</a></li>
    </ul>
  </body>
</html>

Now give a link, a list item and an em element the attribute class and the corresponding value warning to give these elements special styles (see the next figure):

<html>
  <head>
    <title>CSS Kochbuch</title>
    <style type="text/css">
    <!--
      * {
        font-family: verdana, arial, sans-serif;
      }
      h1 {
        font-size: 120%;
      }
      #navigation {
        border: 1px solid black;
        padding: 40px;
      }
      li a {
        text-decoration: none;
      }
      p {
        font-size: 90%;
      }
      .warning {
        font-weight: bold;
      }
     -->
    </style>
  </head>
  <body>
    <h1>Seitenüberschrift</h1>
    <p>Dies ist ein Beispielabsatz mit einem <a href="http://csscookbook.com" class="warning">Link</a>. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna <em class="warning">aliquam erat volutpat</em>. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>
    <ul id="navigation">
      <li class="warning"><a href="http://csscookbook.com">Äpfel</a></li>
      <li><a href="http://csscookbook.com">Bananen</a></li>
      <li><a href="http://csscookbook.com">Kirschen</a></li>
    </ul>
  </body>
</html>

Modified CSS rules on the web page

Figure: Modified CSS rules on the web page.

The following figure shows the selectors within the web page’s structure.

Styled elements within the page structure

Figure: Styled elements within the page structure.
(Note: The em element should also be highlighted.)

  

 

 

 

back to top