3

I had a brief look at the CSS3 Selectors spec but couldn't find anything how to get round this problem. Additionally, I wasn't expecting the result from this to change when you move the CSS declarations around but it does. Any help would be great.

div.green_colour div.has_colour{ background-color: green; } div.red_colour div.has_colour{ background-color: red; }
<div class="red_colour"> <div class="green_colour"> <div class="has_colour"> I would like this to be green </div> </div> </div> <div class="green_colour"> <div class="red_colour"> <div class="has_colour"> I would like this to be red </div> </div> </div>

3
  • Just for reference, that’s the CSS3 Selectors spec. CSS3 is modular, so there are like 51 specs — see w3.org/Style/CSS/current-work. Like HTML5, It’s Not One Big Thing. Commented Sep 12, 2011 at 9:22
  • And I think issues relating to styles having different effects when moved around might be covered in the “CSS Cascading and Inheritance” spec. Commented Sep 12, 2011 at 9:23
  • Brilliant. Thanks v much for the links Paul. Commented Sep 13, 2011 at 11:29

1 Answer 1

5

You can use the E > F child selector as a solution to your problem as such:

div.green_colour > div.has_colour{ background-color: green; } div.red_colour > div.has_colour{ background-color: red; } 

According to this chart http://www.quirksmode.org/css/contents.html it is compatible with all major browsers and IE 7+

There are other ways to implement the solution above (e.g. via javascript) if you are interested.

-- Edit:

I'm not 100% sure if the reason for your solution not to work is due to the fact that browsers parse CSS from right to left instead of left to right, but I assume it has something to do with it. Someone please correct me if I'm wrong.

Sign up to request clarification or add additional context in comments.

5 Comments

'Link-' that's brilliant. Thanks so much. No actually I was trying to avoid the javascript and will now be able to chop out a whole load more. Thanks so much. And thanks for the QuirksMode link too. Here's the w3 spec on child-combinators though it doesn't talk about this enforcing a priority.
It has to do with the red rule being declared after the green rule. both has_colour elements match this rule but since the red one is the last, this one is applied. This is also why you import a reset.css file first and not last in the HTML head.
@Benjamin agree, however the has_colour is declared with an ancestor div.green_colour as a descendant combinator relationship shouldn't the browser check for the whole rule and not just a part of it? Hence my theory of right to left parsing.
Yeah I didn't know about the '>' or that the order or CSS declarations was important. Now I do. Actually, my actual problem (not the one stated above) had a slight twist in that I have the has_color div nested inside another so my final css is div.green_colour > div > div.has_colour
As Benjamin says, it has nothing to do with right-to-left parsing; it has to do with the order of declaration of equally-specific rules. Also, combinators do not play a part in specificity.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.