CSS Attribute Selectors: Built-In Classes

Published April 30, 2008 by CSS Newbies.

If you’ve been a CSS groupie or follower of this website for any length of time, you should have a fairly firm grasp on the concept of using classes in XHTML as “hooks” into your pages (and if not, this article is a good place to catch up). But classes and IDs aren’t the only ways you can describe the elements in your website in order to style them. You can also make use of attribute selectors.

Attributes are the property=”value” pairs inside of most XHTML elements that further describe that element. Take the anchor tag, for example:

<a href="page.html" title="Click to visit!">Text</a>

This anchor tag has two attributes: href and title. Those attributes also have very specific values associated with them. And it’s these attributes, and their values, that make attribute selectors work.

By using attribute selectors in your CSS, you’re able to target elements with specific attributes, or even specific values within those attributes. When using attribute selectors, the attribute is contained within [brackets], just like how .classes have a leading period, or #ids have a leading pound sign. There are four ways to use attribute selectors:

The [attribute] syntax is the least specific of the four. It allows you to target any element with a specific attribute set, regardless of its value. For example:

a[title] {
	font-weight: bold;
	cursor: help;
}

This little bit of CSS would target any anchor tag that had a title attribute set, regardless of what the title contained. My CSS here makes the anchor boldfaced, and changes the cursor to a question mark when the user hovers over the link. This could be useful to indicate to the user that more information (i.e., the tool tip) is coming.

The most specific syntax is [attribute=”value”]. Using this selector, you’d be able to apply specific styles to elements with specific values set. So if, for example, you had an anchor that pointed to a specific website you wanted to emphasize:

a[href="http://www.cssnewbie.com/"] {
	font-size: 120%;
	color: red; 
}

This code would add a bit of emphasis to any link going to the front page of CSSnewbie. I would recommend copying and pasting this code into any website you happen to be working on at the moment. :)

The [attribute~=”value”] syntax allows you to look within the attribute’s values and find a specific value within a space-separated list of values. Alternatively, you can use the related [attribute|=”value”] syntax to find parts of a value that are separated by hyphens (instead of spaces). So, for example, if I had an image with an alt tag that read “Gallery: Wombats Gone Wild,” I could style this gallery (and all other galleries that followed this same pattern) like so:

img[alt~="Gallery:"] {
	border: 2px solid #333;
}

And suddenly, all of your images that link to galleries have special styling, without having to add any additional classes! And that’s really the beauty of attribute selectors: if used properly, they can be a great help in the fight against Classitis.

Of course, attribute selectors don’t work everywhere (why would they? They’re too cool for school). Here’s where attribute selectors earn a big fat FAIL:

  • IE6 or below on the PC
  • IE5 or lower on the Mac
  • Netscape 4 or under (i.e., people in 1997)
  • Both people still using Opera 3

Luckily, very few people are still using Opera 3, Netscape 4, or IE5. And with the recent release of IE7 (and the planned future release of IE8), Internet Explorer version 6 should be on its way out the door, as well. In other words, attribute selectors are fast becoming a reasonable means of styling your websites.

7 Responses

  1. ashooner (reply)

    “And with the recent release of IE7 (and the planned future release of IE8), Internet Explorer version 6 should be on its way out the door, as well.”

    I’m beginning to think that IE6 will never die. My sites still average 35% ie6 traffic. I can manage without repeating PNG backgrounds, but these attribute selectors could seriously streamline both my css and my markup.

  2. Rob (reply)

    You’re probably right, ashooner — IE6 will probably go to its grave kicking, screaming, and dragging its feet the whole way. But I’m really hoping that as IE7 proves itself and IE8 theoretically IMproves itself, adoption will grow at an increasing rate. Nobody likes to be two versions behind. :)

  3. Pingback: Stewart Schatz » Blog Archive » Neat CSS Trick

  4. Pingback: CSS Attribute Selectors: Built-In Classes | CSS Tutorials - CSSHelper.net

  5. airtonix (reply)

    doesn’t work with windows desktop widgets on windows 7.

    I assume the sidebar engine is using the latest explorer engine (being 8)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>