Class selection with nth-child
I am looking for help using the nth-child CSS selector. If you look at my HTML ...
<div id="report"> <div class="a">A</div> <div class="a">A</div> <div class="a">A</div> <div class="a">A</div> <div class="b">B</div> <div class="a">A</div> <div class="a">A</div> <div class="a">A</div> <div class="a">A</div> <div class="b">B</div> <div class="a">A</div> <div class="a">A</div> <div class="a">A</div> </div> ... I have a string of letters like this:
AAAABAAAABAAA I want to show only the first B and hide the others, but I cannot select the classes as I expect. When I try to use:
.b:nth-child(1){ display: block; } .b:nth-child(n+2){ display: none; } This does not work, and I have to select it using (5) to just get the first B.
Help would be greatly appreciated.
JSFiddle: http://jsfiddle.net/SrM9T/1/
+6
3 answers
No javascript required for this
.b ~ .b{ display:none; } Common combiter
The general sibling combinator selector is very similar to the adjacent sibling combinator selector. The difference is that the selected item does not need to immediately overcome the first item, but may appear somewhere after it.
+17