views:

80

answers:

2
+3  Q: 

CSS Selector Style

I didn't see anything on here about it with a quick search, if there is let me know.

Here is an example of a CSS selector I'd write.

div#container div#h h1 { /* styles */ }
div#container div#h ul#navi { /* styles */ }
div#container div#h ul#navi li.selected { /* styles */ }

I write all my CSS like. This allows me to stop from having styles floating around and I can technically re-use the same class name easily. For instance, I might use .selected on multiple elements across the site.

I also specify the element type (div, ul, etc) before the class/id unless the style is used for multiple elements. I also specify the element before id's even though there will only ever be one id because it allows me to know the element easily when reading my CSS. For instance I'll know right off the bat that em#example will most likely have a font-style of italic.

This isn't a question about CSS formating, it's about writing selectors.

I'd love to hear opinions on this approach, as I've used it for years and I'm reevaluating my stying.

Although it's somewhat off topic I also write my selectors like this for selector libraries (like jQuery for instance). Although I haven't looked into jQuery's internals to see if there is performance issue with specifying the element with an ID.

+1  A: 

While the question is a little subjective I have to say I agree with your thinking. I think defining the element before the selector is clearer when reading the code and it is less error prone.

Darrell Brogdon
Well although is does have to do with formating, there could be a few real issues. For instance, some might feel that it doesn't allow you to easily use a style from one element to another without modifying the CSS file. This is the kinds of things I'm looking for. Glad to hear you agree. :)
William
+2  A: 

I think it really depends on what the selector is for.

Almost every site has one or a few style sheets that "skin" the site - fonts, text colour, link colour/hover, line spacing, and so on, and you don't want these selectors to be very specific. Similarly, if you have a component or element that's reused in many pages and always needs to look the same - like let's say the tags right here on SO - then it's going to be a pain to maintain if you use selectors based on the ID.

I would always use the ID in the selector if it refers to a specific element on a specific page. Nothing's more frustrating than trying to figure out why your rules don't seem to be working because of a conflict with some other rule, which can happen a lot if everything is classes. On the other hand, I think that nesting the IDs as you are (#container #h) is redundant, because the purpose of an ID is to be unique. If you have more than one element with the same ID on the same page then you can end up with all sorts of problems.

It does make the CSS slightly easier to understand when your selectors provide some idea of the "structure" that's being represented, but, to be honest, I think that goes against the idea of separation of concerns. The #navi might be moved outside the #h for perfectly legitimate reasons, and now somebody has to update the style sheet for #navi, even though nothing about it has changed.

A bit subjective as Darrell pointed out but that's my two cents.

Aaronaught
Good points. The ID thing is a bit tricky. I would only set an ID with the parent selectors if I want that ID to specifically be there. Now I could make it simply #id, but I'm still going to place it in the CSS file around the header (which would be the parent)'s group. So if someone wanted to move it, they'd most likely move the line anyways, so why not re-rewrite the selector? If you want the ID to be movable, don't specify the parents. I'll close this question as it is subjective. Thanks!
William