Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Attribute Selectors in One Quick Bite


WebReview.com: Attribute Selectors in One Quick Bite

Sept. 22, 2000 > Tag of the Week

Attribute Sectors in One Quick Bite

By Chuck Musciano

Last week, we looked into the new kinds of element selectors defined in the CSS2 standard. These new selectors allow you to apply style properties based upon element parent/child and adjacency relationships in your documents. We continue this look at the new CSS2 selectors with the attribute selectors.

General Attribute Selectors

CSS1 allowed the selection of elements based upon the element name alone. CSS2 extends this to include attributes associated with the element, using a special syntax. When an element in a selector is followed by a name in square brackets, that name is taken to be an attribute of the element and must be present for the rule to be applied to the element.

This is easier to see in an example:

img[align] { border-width : 0 }

This style property would only be applied to <img> elements that have the align attribute specified. If the attribute is missing, the rule is not applied.

You can extend this idea to include several attributes. For example,

img[align][border] { margin : 10pt }

is only applied to <img> elements with both the align and border attributes present.

To make things even more general, CSS2 introduces the special selector *, which matches any element. It comes in handy when you want to match any element with a specific attribute mentioned. For instance:

*[align]

will match any element with the align attribute set.

Attribute Value Selectors

For those cases where just having the attribute present is not enough, CSS2 also lets you dictate the value of the attribute. If you follow an attribute name in a selector with an equal sign and a string, not only must the attribute be present, it must have that exact value to let the element be selected for that style property. To further refine our <img> example, you could say

img[align=left] { margin : 10pt}

to single out only those <img> elements whose align attribute is set to left. Note that this match only occurs if the attribute is explicitly set in the document itself. Even if left were the default value for align, the match would not take place unless you explicitly set align to left in your document.

CSS2 defines a number of ways to match a string. The plain = exactly matches the string, spaces and all. Using ~= matches any occurrence of the string as a space-delimited word in the attribute value. In this case, there may not be any spaces in the string, since they are considered delimiters in the attribute value for matching purposes. For example,

img[alt~=copy] { border : 0pt }

would match this image tag

<img alt="This is a copy of the original" />

since "copy" is a word in the alt attribute's value, but not this one

<img alt="This was made by copying the original">

since "copy" does not exist as a standalone word in the alt attribute.

In a similar vein, using |= as the matching operator matches the string against a hyphen-delimited word in the attribute value. This odd little feature is intended to allow matching of parts of ISO language names, which are delimited by hyphens. Thus, you might use

*[lang|=fr] { display : none }

to hide any element in your document whose lang attribute has "fr" as part of its value. This hides any French content in your document, in case you ever have such a need.

Class Selectors

Those of you who followed our original articles on CSS1 may realize that the CSS1 class selector, which applied a rule only to an element whose class attribute had a certain value, is a special case of the more general CSS2 attribute value matching. In fact, these two notations are equivalent:

H1.title
H1[class=title]

In either case, any properties associated with these rules would be applied to any <h1> element whose class attribute is set to title. Given its shorter syntax, you should continue to use the traditional CSS1 class selection model instead of the CSS2 attribute value matching model when creating rules for specific element classes.


Chuck is the author of the best-selling HTML: The Definitive Guide and now, the fourth and expanded edition HTML & XHTML: The Definitive Guide. He also writes on a variety of Internet and Web-related topics for a number of online magazines.


Previously in Tag of the Week

CSS2 Standard Tour: Begin Here


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.