Placed in: Home arrow Programming arrow CSS arrow Why you want to use the :any() CSS selector viagra australia
viagra online
viagra sale
cialis online
cialis australia
Why you want to use the :any() CSS selector

Since Firefox 4 was relesed, loads of new (CSS) features were released into the wild. Ofcourse, this is a great thing, since Firefox has a rock solid place as 2nd most popular web browser. More people will actually be able to see all the nifty stuff you'll create using CSS.

I recently came accross a CSS selector that was included in Firefox 4 (and is added to Webkit (nightly) as well), called the :any() selector. We'll take a look at what this selector does, and why you would want to use it.

any() selector

Be aware that the :any() is not part of the CSS3 specification (yet), but since Webkit already implemented it, I'm pretty sure others will follow as well. Also, to make it work in Firefox, use the -moz- vendor prefix, resulting in :-moz-any (-webkit- vendor prefix for Webkit). So let's see what this :any() selector can do for you.

The problem

The problem is nesting things in CSS multiple levels deep. Here's an example:

 
/* Links in general on the website */
/* Level 0 */
a { 
   color:#eee;
}
 
/* Specific links in lists, navigation, header and footer */
/* Level 1 */
li a, nav a, header a, footer a {
   color:#555;
}
 
/* Specific links, another level deeper */
/* Level 2 */
section li a, section nav a, section header a, section footer a,
article li a, article nav a, article header a, article footer a,
aside li a, aside nav a, aside header a, aside footer a {
   color:#000;
}
 
/* Level 3 */
/* Yeah, you get the idea */

As you can see, each level we go deeper, the CSS selector will get harder to read/understand.

The solution

The :any() selector is created to group several of these selectors together, just to achieve the same kind of effect. The only difference is the length/readability, so you can make changes easier later on.

 
/* Links in general on the website */
/* Level 0 */
a { 
   color:#eee;
}
 
/* Specific links in lists, navigation, header and footer */
/* Level 1 */
:any(li, nav, header, footer) a {
   color:#555;
}
 
/* Specific links, another level deeper */
/* Level 2 */
:any(section, article, aside)
:any(li, nav, header, footer) a {
   color:#000;
}
 
/* Only apply to headings, another level deeper */
/* Level 3 */
:any(section, article, aside)
:any(li, nav, header, footer)
:any(h1, h2, h3) a {
   color:#aaa;
}

I know the hgroup element would be more appropriate for the last selector (instead of h1, h2, h3), but I wanted to use the "old" headings, just to make the code more clear.

Thoughts

As you can see, you can group several selectors together in the :any() selector in order to target your specified element. It's better to read, so making changes to your HTML and CSS will be easier to do. But are there more reasons why you should use this selector?

This selector is especially useful when using the HTML5 section, header, footer, nav, aside etc. elements. Since HTML5 is trying to get rid of the division (div) and make the HTML more meaningful, this CSS selector takes it another step further. To solve the solution described above (with nesting multiple levels deep), we could also use a specific class to do the job for you. But why would you need that, if your HTML is already as meaningful as it can be?

So, this selector is especially useful when you have some neat HTML5 with nested elements, and don't want to use the class selector (same goes for the id). I'm sad to say I haven't seen these kind of nested elements (a lot) in websites, so I'm not sure if the :any() selector will bring peace to that.

What do you think? Feel free to share!

Further reading


Tags:  any css selector firefox webkit

Interested in this topic? You might enjoy another article I've written called

Did you like this article? Subscribe to my feed or email to keep updated on new articles.

Spread the word and submit to:
Digg!Reddit!Del.icio.us!Facebook!StumbleUpon!
 
< Prev   Next >
Subscribe

Subscribe to Marcofolio