CSS 3 Selectors

CSS 3 introduces a wide range of new selectors that let you specify which page elements a style applies to with even greater precision. In fact, several of the attribute selectors discussed on pages Attribute SelectorsTutorial: Selector Sampler are actually CSS 3 selectors. For example, to style links that point to Adobe Acrobat files, you can create a selector like this: a[href$=".pdf"]. The $= means "ends with," so the specified attribute (here, href) has to end with a particular value (.pdf). Similarly, you can use ^= to pinpoint an attribute that starts with a particular value (Attribute Selectors explains how to use this selector to identify links to another website).

The advanced attribute selectors in this section work with Firefox, Safari, Chrome, Opera, and even Internet Explorer 7 and above (but not IE 6, mind you). Other new CSS 3 selectors work mostly in non-IE browsers.

Note

If you want to see which CSS 3 selectors your favorite web browser understands, visit the CSS Selectors Test Suite at www.css3.info/selectors-test. This nifty JavaScript program tests your browser, giving either a green (thumbs up) or red (thumbs down) rating for each of the CSS 3 selectors. Safari 4, Firefox 3.5, Opera 9.5, and Chrome pass all 43 tests, while Internet Explorer 8 passes 22 of the 43 tests.

CSS provides several methods for formatting tags that are children of other tags. As described on The HTML Family Tree, a child is any tag that's directly wrapped inside a tag. Take, for example, a bold word inside a paragraph; the <strong> tag is the child of the <p> tag. In a bulleted list of items, the <li> tags are children of the <ul> tag.

ul :first-child

Note the space between ul and :first-child, which translates to "find the first child of a <ul> tag." (The selector ul:first-child, with no space, is entirely different: it selects all <ul> tags that are a first child of some other tag.)

If you want to add a special color to any text that appeared first inside a div with the class announcement, write the following style:

.announcement :first-child { color: #F33F00; }

This style applies to the first tag inside the div, whether it's an <h1>, <h2>, <p>, or any other tag. It gives you extra flexibility, since the style isn't dependent on an exact type of tag to appear first inside the div.

The :first-child, selector works in all modern browsers except Internet Explorer 6 and earlier.

Unfortunately, version 8 and earlier of Internet Explorer do not support the nth-child selector. (For IE 6, 7 and 8 you can use the lower-tech solution for striping tables, described on Styling Rows and Columns, which works in all browsers.)

CSS 3 introduces a selector that works much like the child selectors in the previous section but applies to children with a specific type of HTML tag. For example, say you want to format the first paragraph inside a sidebar in a particular way, but on some pages, that sidebar starts with an <h2> tag, and on other pages, it starts with a <p> tag. You can't use :first-child to select that paragraph, since in some cases it's the second child (following the <h2>). However, it's always the first paragraph (<p> tag), even if other tags come before it, so you can select it with a type selector called :first-of-type.

Here's the skinny on :first-of-type and several more type-related selectors:

Unfortunately, the type selectors don't work in Internet Explorer 8 or earlier; just Firefox 3.5, Safari, Opera, and Chrome.