Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does CSS section:only-child selector not work?

I was just messing around with the new HTML5 tags and pseudo selectors and noticed that the section:only-child selector appears not to work. I'm testing this on the latest version of Chrome (23.0).

My code is:

<section id="s1">
    <div id="div1">abc</div>
    <div id="div2">
        <span>first span</span>
        <span class="sp">second span</span>
    </div>
</section>

<section>
    <div>first child test</div>
</section>​

and the CSS is:

section {
background-color: brown;
width: 400px;
height: 200px;
}

#s1 {
position: relative; 
background-color: rgba(0,255,0,0.5); 
border: 1px solid #000; width: 50%;
}

#div1 {
background-color: #0f0; 
position: relative;
}

#div2 {
background-color: #0ff; 
width: 200px; 
color: red;
}

#div2 .sp {
color: white;
text-decoration:line-through;
}

section:only-child {
color: yellow;

    }​

In theory, the text "first child test" should be yellow, because there is only one div on the second section, but it is not getting the yellow color for a font.

If I change the selector to div:only-child it'll work just fine.

You can see the jsfiddle here: http://jsfiddle.net/KwzZs/3/

any reason why this is not working?

thanks

like image 329
GR7 Avatar asked Feb 21 '26 10:02

GR7


2 Answers

This rule:

section:only-child

Matches a section element which is an only-child.

This rule (note the space):

section :only-child

Matches an element which is an only-child of a section. Is this what you expected?

like image 155
robertc Avatar answered Feb 24 '26 05:02

robertc


x:only-child means that the x element is the only child of its parent - not that x has only one child.

like image 21
irrelephant Avatar answered Feb 24 '26 04:02

irrelephant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!