Since the beginning of February there were some proposals for the CSS3 specification. These proposals are not yet part of the specification and will likely be changed until they get adopted by the CSSWG. One of these drafts is the CSS Hierarchies Module Level 3.
So what’s the Hierarchies Module? And why “Level 3”?
The Hierarchies Module is not exactly new. It exists since the very first steps of CSS and was first released in Dezember 1996 with CSS1. The CSS1 specification is still up, so check it out if you want to.
When you write CSS you always use selectors to target the element you want to style. This works pretty much straight-forward: The right-most simple selector is the one which get the declarations assigned that are defined in the ruleset.
These selectors define a certain hierarchy in your stylesheet.
Note: Please make sure to read Tab Atkins’ Reference for CSS Spec Terms.
An example
Today you would target some links in a navigation like this:
#nav ul li a { color: #000; }
If you’re smart and your markup allows you to do so, you may write:
#nav a { color: #000; }
The li-elements need to be inlined as you want a horizontal navigation:
#nav ul li { display: inline; }
Furthermore the navigation should be aligned on the right and all the links centered:
#nav ul { text-align: right; } #nav ul li { text-align: center; }
This leaves us with a lot of repetition for the selectors.
#nav ul { text-align: right; } #nav ul li { text-align: center; display: inline; } #nav ul li a { color: #000; }
With the Level 3 proposal by Tab Atkins Jr and Shane Stephens (working for Google) this is about to get a little bit easier. The existing system will be extended with some new functionality. This is why it’s called Level 3. (Also it will likely be part of CSS 3 – and not 4 or 10 or something.)
What’s new in there?
Lately you saw the rise of CSS Preprocessors. Everyone has an opinion on this. The thing why so many developers use them is that they help to organize the CSS-code you are writing.
Preprocessors have brought up another idea of CSS hierarchies. And with the CSS Hierarchies Module Level 3 this has been adopted.
The specification draft allows you to nest your rules and use the &
-character to reference a parent selector in a certain rule.
Here’s the example from above with the new hierarchy:
#nav ul { text-align: right; & li { text-align: center; display: inline; & a { color: #000; } } }
As you can see there are no repetitions for the selectors as you can reference them by just writing &
and nest them properly.
How can we use this today and when will it be ready for the “real world”?
As you may guess this is not implemented in any browser yet.
As this spec is written by two Google-guys it’s likely that this will be implemented in Chrome in the near future. At the moment this is not working with Chrome 19 (Canary) or WebKit Nightly.
I’ve made this Dabblet to check if the module is supported in a browser or not. caniuse.com does not provide any data for the Hierarchies Module yet.
Aside: Check my Dabblet Chrome App to view all your latest dabblets if you’re a Chrome user.
Tab Atkins proposed another style for nesting. To reference the parent selector you use @this
instead of the &
-character. Peter Beverloo wrote about this a year ago. Also Tab Atkins has an article about his further plans. This variant was not implement either and is replaced with the new draft for the specification. Anyway you could view the dabblet I’ve made for this propose.
Preprocessors
As mentioned above CSS preprocessors offer the chance to use something like hierarchies in there own way. Here are two examples:
SCSS (aka. SASS)
#nav ul { text-align: right; li { display: inline; a { color: #000; &:hover { color: gray; } } } }
Stylus
#nav ul text-align right li display inline a color #000 &:hover color grey
As you can see there is a difference in how preprocessors treat the &
-character as a reference: In the CSS specification draft &
is obligatory in every nested rule you write to reference the parent selector. Preprocessors need them only if there is a pseudo-class or -element or a combinator (such as ~
or &
) used for a specific selector.
As I stated in the introduction it may happen that the specification will be changed until it becomes final or is adopted by the CSS Working Group. I recommend you follow the discussions about this topic in order to stay up to date.
When this specification will be implemented by one of the browser vendors it’s just a matter of weeks until it will be adopted by the others I guess.
But, as the draft states, this is not yet part of CSS:
This document is purely an Editor’s Draft. It has not yet been adopted by the Working Group, and should not be considered to be part of CSS.
~ CSS Hierarchies Module – Status
Opinion
Personally I hope that referencing the parent selector with &
will not be in the final specification as it is now. It’s not needed as the rules are nested anyway and preprocessors nowadays treat them better.
Anyway the new nesting is pretty useful as it reduces the redundancy of writing selectors in CSS. In connection with other upcoming specifications like the CSS Variables Module Level 1 this is what is needed and will be helpful for every web-developer once it will be implemented in browsers.