Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS selector for first instance of child with class? [duplicate]

Is there a CSS selector for the first instance of a child with class? For example, take this code:

<div id="container">
    <div class="nothing">
    </div>
    <div class="child"> <!-- SELECT THIS ONE -->
    </div>
    <div class="child"> <!-- NOT THIS ONE -->
    </div>
    <div class="child"> <!-- NOT THIS ONE -->
    </div>
</div>

Is there a css selector to style the first occurrence of a div with class "child"?

like image 225
Andrew Avatar asked Aug 21 '13 16:08

Andrew


1 Answers

You could style .child and revert styles for subsequent .child siblings using the general sibling combinator, ~:

.child {
  color: #ff0000;
}
.child ~ .child {
  color: inherit;
}
<div id="container">
  <div class="nothing">nothing
  </div>
  <div class="child">SELECT THIS ONE
  </div>
  <div class="child">NOT THIS ONE
  </div>
  <div class="child">NOT THIS ONE
  </div>
</div>
like image 179
canon Avatar answered Sep 24 '22 21:09

canon