Rediscovering attribute selectors

Share on TwitterShare on TumblrSubmit to StumbleUponSave on DeliciousDigg This

In this article I’ll explain how to use CSS 2.1 attribute selectors to stylize a web document. The core syntax of attribute selectors is explained below.

Table of contents

Syntax of attribute selectors

Attribute selectors match elements with four different patterns:

  1. [attribute]Matches the element when it has the attribute attribute, whatever the value of the attribute. For example, the following CSS code:
    1. p[class] {color: green}

    will match the following (X)HTML element:

    1. <p class="foo">Lorem ipsum</p>
  2. [attribute="value"]Matches the element when its attribute attribute value is exactly value. For example, the following CSS code:
    1. p[class="foo"] {color: green}

    will match the following (X)HTML element:

    1. <p class="foo">Lorem ipsum</p>
  3. [attribute~="value"]Matches the element when its attribute attribute value is a space separated list of words, one of which is exactly value. For example, the following CSS code:
    1. p[class~="foo"] {color: green}

    will match the following (X)HTML element:

    1. <p class="foo bar baz">Lorem ipsum</p>

    The words in the value must not contain spaces, since they are separated by spaces.

  4. [attribute|="value"]Matches the element when its attribute attribute value is a hyphen-separated list of words, beginning with value. For example, the following CSS code:
    1. p[class|="foo"] {color: green}

    will match the following (X)HTML element:

    1. <p class="foo-bar-baz">Lorem ipsum</p>

    The match always start at the beginning of the attribute value.

The markup

We start with the following markup:

Listing 1. The markup of the page

  1. <div id="page">
  2. <div id="header">
  3. <div id="img-header">
  4. <h1><a href="#">Racing Thoughts</a></h1>
  5. <div>Just another WordPress weblog</div>
  6. </div>
  7. </div>
  8. <hr />
  9. <div id="content">
  10. <div id="post-3">
  11. <h2><a href="#" rel="bookmark" title="Permanent Link to Lorem ipsum dolor">Lorem ipsum dolor</a></h2>
  12. <small>August 15th, 2008</small>
  13. <div>
  14. <p>...</p>
  15. </div>
  16. ...omission...
  17. </div>
  18. </div>
  19. <div id="sidebar">
  20. <ul>
  21. <li>
  22. <form method="get" id="searchform" action="#">
  23. <label for="s">Search for:</label>
  24. <div>
  25. <input value="" name="s" id="s" type="text" />
  26. <input id="searchsubmit" value="Search" type="submit" />
  27. </div>
  28. </form>
  29. </li>
  30. ...omission...
  31. </ul>
  32. ...omission...
  33. </div>
  34. <hr />
  35. <div id="footer">
  36. <p>
  37. Racing Thoughts is proudly powered by <a href="#">WordPress</a> <br /><a href="#">Entries (RSS)</a> and <a href="#">Comments (RSS)</a>.
  38. </p>
  39. </div>
  40. </div>

View the example

We’re not interested in the basic styles given to this first example, so we’ll skip them and focus only on the sections where attribute selectors are actually used.

Global styles

Let’s start with stylizing the global wrapper, #page.

Listing 2. Global styles

  1. div[id="page"] {
  2. font-size: 120%;
  3. margin: 0;
  4. width: 100%;
  5. }

View the exampleView the screenshot

We’ve just increased a bit the base font size of the page, while adding a width declaration in order to prevent Internet Explorer 7 from applying its wrong behavior.

We’re going now to stylize the header. The styles are the following:

Listing 3. The header

  1. div[id="header"] {
  2. width: 100%;
  3. height: 5em;
  4. min-height: 80px;
  5. background: #fff url("../img/banner.png") no-repeat 0 0;
  6. position: relative;
  7. }
  8. div[id|="img"] {
  9. position: absolute;
  10. left: 200px;
  11. top: 0;
  12. }
  13. div[class="description"] {
  14. text-align: right;
  15. font: italic 120%/normal Georgia, serif;
  16. margin-top: -5px;
  17. }

View the exampleView the screenshot

The second rule is interesting. Since the container of the title has an ID value made up of two hyphen-separated words (img-header), this rule match the element. Remember that the word selected is always the first in the list.

Main content and sidebar

For the main content and the sidebar we’ll use more complex patterns.

Listing 4. Main content and sidebar

  1. div[id="content"][class|="narrow"] {
  2. margin: 0;
  3. float: left;
  4. width: 65%;
  5. padding: 0 5%;
  6. }
  7. div[class="post"][id="post-3"] {
  8. padding: 1em;
  9. border: 1px solid #c0c0c0;
  10. margin: 1em 0;
  11. }
  12. div[class="post"] a[title~="Permanent"] {
  13. color: #800;
  14. text-transform: uppercase;
  15. border-left: 1px solid #000;
  16. display: block;
  17. text-indent: 0.2em;
  18. }
  19. div[id="sidebar"] {
  20. float: right;
  21. width: 25%;
  22. }

View the exampleView the screenshot

As you can see, attribute selectors can be combined to create more complex patterns. A complex pattern matches an element when all the attribute selectors in the chain match the element. For example, the second rule matches its target element only if this element has two IDs with post and post-3 as values, respectively.

Form elements

Attribute selectors are really useful when dealing with form elements, as shown below.

Listing 5. Form elements

  1. form[method="get"] {
  2. margin: 0;
  3. padding: 0.5em;
  4. border: 1px solid #c0c0c0;
  5. }
  6. input[type="text"] {
  7. width: 100px;
  8. border: 1px solid #000;
  9. background: #fff;
  10. font: 1em Arial, sans-serif;
  11. }
  12. input[type="submit"] {
  13. font: 1em Arial, sans-serif;
  14. background: #666;
  15. color: #fff;
  16. font-weight: bold;
  17. border: 1px solid #c0c0c0;
  18. }

View the exampleView the screenshot

Through attribute selectors we can stylize a form element based on the presence of a particular value in its method or action attributes. So if we have two different forms on a page, we can give them different styles, for example by matching the value of its action attribute. Moreover, attribute selectors can give different styles to input elements by matching the value of their type attribute in order to keep form controls separated.

The styles for the footer are really simple.

Listing 6. The footer

  1. div[id="footer"] {
  2. clear: both;
  3. margin: 0;
  4. height: 5em;
  5. background: #fff url("../img/footer.png") no-repeat 0 0;
  6. }

View the exampleView the screenshot

The final layout is depicted in the following image.

Final layoutFigure 1. Final layout

Download examples