CSS attribute selectors: Precision styling at your fingertips

developer at his laptop looking stressed

You spend hours coding a template for a new content management system. The markup is pristine, frame-worthy even. You drop it into ModX or WordPress, add the content, and beam with pride. Then you activate a few plugins…

And the dream is over. Your once picture-perfect markup is suddenly desecrated by table-filled dreck that’s nearly impossible to style.

I found myself in this very situation with a plugin that automatically generated the markup for a form along the lines of this:

<form method="post">
<table>
  <tr valign="top">
    <td><b>Username:</b>&nbsp;</td>
    <td><input type="text" name="username" value="" size="10" /></td>
  </tr>
  <tr valign="top">
    <td></td>
    <td><input type="submit" value="Register" /></td>
  </tr>
</table>
</form>

Yikes. I planned to get rid of the table or, at the very least, add a class to the submit button and have it match the look of other form controls on the website. But the plugin’s source was encrypted and I was out of luck. So I turned to CSS attribute selectors.

What is an attribute selector?

A CSS attribute selector is a special style declaration that allows you to target elements through their attributes. For CSS 2.1, there are four ways to define how attributes are matched:

h1[title] {margin-bottom: 5px;}
a[href=http://www.mydomain.com/] {background-image: url(house.jpg);}
a[title~=Water] {color: blue;}
a[hreflang|=en] {padding-left: 20px;}

CSS3 allows for more complex matching patterns, including regular expressions, but these four should be enough to get you started.

Keep in mind that you can use multiple attribute selectors in a single declaration the same way you can use multiple classes. The following would hide the underline for anchor elements with a relationship attribute of external and a title attribute that contains the word “Clue”.

a[rel=external][title~=Clue] {text-decoration: none;}

Going back to the form markup I presented earlier, I was able to style the submit button without adding a class attribute by using the name/value attribute selector. I just targeted input elements with a type of “submit”, like this:

input[type=submit] {color: #fff; background-color: #cc6600;}

With CSS attribute selectors, the possibilities for precise element selection and styling really open up. Of course…

There’s always a catch

And you can probably guess what it is. Internet Explorer 6 doesn’t support attribute selectors. If you decide to use them, make sure your styles degrade gracefully. IE6 users should be able to access your content even if it doesn’t look the way you want.

The next time you need to style an element that seems out of reach, remember CSS attribute selectors. They’re a simple yet powerful option for precision styling.

How about you? How do you work with and style markup that’s less-than-perfect? Do you use attribute selectors on a regular basis or only in tight spots? Let us know in the comments.

Did you enjoy this post?

Shout it from the rooftops, or click one of the buttons there and spread the word to your favorite community. You’ll have my undying gratitude.

Stumble This Share on Reddit Bookmark on Delicious Digg This

New here? Get the latest updates by RSS Feed or Email. It’s FREE!

black-and-white photo of Jason

About Jason Garrison

Jason is a freelance web designer and developer who has a healthy obsession with web standards. When he’s not knee-deep in code, you can find him in a meditative state communing with the universe.

9 Comments

  • Design InformerDecember 12, 2009 My Website

    Hello Jason,

    I never knew that. Thank you for this great CSS tip. I absolutely love your tutorials, and your writing style is very easy to comprehend. Also, the site design looks great!

  • FAQPALDecember 12, 2009 My Website

    This is all new to me, I have never seen this being used any where.

    Thanks for the post.

  • BrandonDecember 13, 2009 My Website

    Wow! Awesome, I have never seen this before/had no idea you could do this. I’ve run into similar situations with Wordpress plugins that were difficult to style. Great reference article, thanks! :)

  • reADactorDecember 13, 2009 My Website

    Really nice tips. I just can’t wait to play with attributes.

  • andyDecember 14, 2009

    Yes, they’re awesome. But in reality you can’t use them because customers seem to almost always use IE6. So if it doesn’t look the way it should in their eight year old browser you’re screwed.
    So I’m always looking for a workaround or IE6 compatible way and think to myself how awesome it would be with CSS.
    Why can’t IE6 just die.

  • Dennis BurgerDecember 14, 2009 My Website

    Keep in mind that Win/IE 7 does not support ALL attribute type selectors in CSS 3. Sadly only the basic ones. And I’m not sure about Win/IE 8 as well.

  • Jason GarrisonDecember 14, 2009

    Thanks for your comments everyone.

    Dennis – According to Microsoft’s IE compatibility chart IE7 and 8 support all attribute selectors in both CSS 2.1 and 3. Whether that’s the case in real life, I’m not entirely sure…

  • Rajesh TrilokhriaDecember 14, 2009 My Website

    Working as a front end developer… I find CSS selectors very powerful way to achieve your desired output with CSS only :)

    I always used to think about advance versions of CSS and here is the real avatar – CSS 3

  • GregDecember 15, 2009 My Website

    Holy jumpin’. I never knew these existed. Thanks for the lesson!