January 01, 2002
URL:http://www.drdobbs.com/universal-selector/184412547
No, it isn't a new movie starring Jean-Claude Van Dammeit'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.
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.
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.
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.
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.
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.
Terms of Service | Privacy Statement | Copyright © 2024 UBM Tech, All rights reserved.