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

Universal Selector


WebReview.com: Universal Selector

No, it isn't a new movie starring Jean-Claude Van Damme—it's the first of the CSS2 selectors which I plan to cover in a series of articles. These include the child selector, sibling selectors, a trio of new pseudo-classes, and most complicated of all, the attribute selectors. I will examine each of these in turn, and in detail.

Note that support for these CSS2 selectors is limited as I write this, but the near future promises a number of browsers that will expand support dramatically. It's a perfect time to start learning how these new selectors can make your CSS more sophisticated and less dependent on classes and other special-case markup.

How It Works

In many ways, the most powerful of the new CSS2 selectors is the universal selector. The universal selector can be used to represent any element at all, and is specified using an asterisk (*). Thus, use this declaration to make sure all elements are purple:

* {color: purple;} 

Assume for a moment that you have a document containing the following HTML elements: an H1, some paragraphs, a table, some preformatted text, and an unordered list. For the purposes of this specific document, writing * {color: purple;} is exactly the same as writing:

BODY, H1, P, TABLE, TR, TD, TH, PRE, UL, LI {color: purple;} 

This isn't the extent of the universal selector's power, of course, because it will match any element at all. While this may sound somewhat useless at first, a closer examination reveals a great deal of power and flexibility.

Let's say that we want to apply a given style to all elements with a class of danger. The usual way to do this is:

.danger {color: red; background: yellow;} 

You can get exactly the same effect using the universal selector:

*.danger {color: red; background: yellow;} 

So what's the big deal? Not much in this case, as we get the same effect either way. However, consider this: if you're concerned about older user agents that don't know about CSS2, then *.class (or *#id) is an easy way to fool them. Because either of these are examples of an invalid selector in CSS1, CSS1-only parsers should ignore them. For this reason, it might be a good idea to omit the universal selector in conjunction with class and ID selectors, at least for the time being.

Circumventing Inheritance

As we look deeper, we can see that the universal selector completely bypasses the inheritance mechanism. By writing a rule that's matched by all elements, you have a rule that explicitly assigns a given style (or collection of styles) to every element. Consider:

* {color: cyan;} 

Given this rule, no element can possibly inherit the color of its parent, because the above example explicitly sets the color of every element to cyan. Hyperlinks, for example, will be cyan along with everything else.

Of course, since all elements in the document will be cyan, the distinction that the color can't be inherited seems sort of obscure. However, the universal selector can do more than simply address every element in a document. Consider that you can restrict the universal selector's effects to a given context. Perhaps you'd like every element that's a descendant of DIV with a class of danger to be red. This would be written:

DIV.danger * {color: red;} 

At first glance, this seems no different than if the * were left out, but that's not quite true. Again we see that there's a difference: The rule shown would match every DIV descendant, and therefore override the inheritance mechanism. Thus, even anchors that are descendants of a DIV with a class of danger would turn red under the given rule, whereas simple inheritance wouldn't be sufficient to make them red.

This ability to use the universal selector in a contextual selector leads to some surprising consequences, as we'll explore next.

Universality in Context

We start to get into some interesting effects when the universal selector is made part of a contextual selector. This ensures that styles are only applied if a given element is a sufficiently advanced descendant of another, or that all elements that descended from a given element are affected.

For example, assume that you want any UL element that's at least a grandchild of the bodyto be gray. In other words, any UL that's a child of bodywouldn't be gray, but any other UL, whether it's child to a DIV, a list item, or a table, should be gray. This works as follows:

body* UL {color: gray;} 

Remember that this applies to any UL that's at least a grandchild of the BODY, so even an unordered list element that's a great-great-great-great-grandchild will be matched. Only those UL elements that are direct children of the bodyelement will not be matched.

You could also decide to apply a thin green border to only those UL elements that are great-grandchildren or later:

body* * UL {border-right: thin solid green;}

Or suppose that you wish to boldface any UL that's at least a grandchild of a TABLE, given that the TABLE is at least a great-grandchild of the bodyelement. Thus:

body* * TABLE * UL {font-weight: bold;}

This is the sort of thing that conventional CSS1 selectors simply can't match, if you'll forgive the pun.

Browser Support

The universal selector is supported in Internet Explorer 5.x for both Windows and Macintosh, as well as IE 4.5 for Mac, and also in Opera 3.6. It's also supported in the Netscape 6 Preview Release 1 on all the myriad platforms for which it's available, and in Preview Release 3 of Opera 4 for Windows.

Still to Come...

So there you have it: a way to short-circuit inheritance, and also a way to apply styles to only those elements that are very deep inside the document's structure, without having to write incredibly complicated selectors just to reach them.

Despite the power and flexibility that the universal selector gives us, it's in some ways the least sophisticated of the new CSS2 selectors. As you'll see in future articles, there are even more interesting selectors and selection strategies in store for us.


Eric is the author of CSS: The Definitive Guide, from O'Reilly, and is the editor of the Style Sheets Reference Guide here on Web Review.

Be sure to drop by Eric's updated CSS2 Selectors Support Chart.


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.