views:

35

answers:

4

Well, I finally decided to totally drop IE6. It's great. First big benefit which comes with this decision is child selectors. So I started to use them for my nested drop-down menus for the first time and expected it to be a breeze. But... here's the code:

<style>
body {
    color:#000;
}

ul.top > li {
    color:red;
}

<ul class="top">
    <li>top li</li>
    <li>
        <ul class="sub">
           <li>sub li</li>
        <li>sub li</li>
        </ul>
    </li>
</ul>

What I expected here, that only immediate children of .top unordered list are colored red and all the rest are black. Isn't it a logical thing to expect? But they all actually get red...

+5  A: 

try

ul.top > li {
    color:red;
}

ul.sub > li {
    color:black;
}

??

Tim
This should be right, the default thing for a element to do for the 'color' property is to inherit it from its parent, The child selector puts the color style attribute only on the immediate children, but all of the children of those children will inherit it.
Bob Fincheimer
Well I actually tried to color all li elements black before applying styles to childs of .top and it worked, but damn... what should I do with more complicated styles like absolute positioning, borders, paddings, changing fonts. Do I need to define them for all elements prior to using child selectors? Where's the benefit?
jayarjo
No, you don't. **Positioning is not inherited by default**.
MvanGeest
No you don't have to worry about layout items being inherited, only a certain number of style properties can be inherited (like color and background (maybe font???) [http://www.w3.org/TR/CSS2/cascade.html#value-def-inherit])... you don't need to worry about this kind of thing with position, padding, margin, width, height, etc
Bob Fincheimer
Hm... strange. Actually I got a weird behavior with positioning and the rest first and then simplified this thing down to a color just to see whether it works as expected... I will do another test now.
jayarjo
@Bob Fincheimer: you're not entirely correct. Only a certain number of style properties *are inherited by default*. Most of them can be inherited explicitly if you set them to `inherit`.
MvanGeest
+2  A: 

Tim gave you the solution. The explanation for this behaviour is that though color: red; is only applied to the top-level lis, the color is inherited by their descendants. Check CSS Inheritance at Dorward Online for an in-depth explanation.

MvanGeest
I think this breaks the whole benefit of this thing and turns it into another misleading shit. No?
jayarjo
Say you have `p.top { color:red }`. `<p class="top">By default, you would want <strong>all</strong> of this to be red, right?</p>`
Patrick McElhaney
I think child selectors are very useful as they can apply styles very specifically, so no. Inheritance is also very useful, since it will enable 'natural' behaviour (wouldn't it be annoying if a `<b>` inside your red `<p>` would remain black because it hasn't been selected specifically?). Just because **you** don't know about it doesn't mean it's misleading. CSS is a very well-defined language.
MvanGeest
No, I need to color red only immediate children of my unordered .top list. Actually I've got much more complicated things going on, but I simplified it to color only.
jayarjo
Thanks @Patrick McElhaney for illustrating my case, I was busy writing my long comment...
MvanGeest
A: 

Unfortunately the child selector causing all the li to inherit the class. So you'll need to define another child ul.sub > li{

bradenkeith
+1  A: 

Well you see the red color is applied to both the first and the second element in the list .top, now the second element does not have any style information for color applied, so therefore it uses the style of the parent witch has the color red.

Ramuns Usovs
Why it does not inherit black color from body then?
jayarjo