1

I'm wrapping my head but couldn't manage to find an answer on Stack Overflow so far about this:

Say a list of these elements is given:

<div class="container">
    <div class="A"></div>
    <div class="B"></div>
    <div class="A"></div>
    <div class="A"></div>
    <div class="B"></div>
</div>

How would you select each first occurence of A in this example?

I tried with siblings selector, :first-of-type, :nth-of-type and :first-child and :nth-child, but couldn't get it to work.

Stefan
  • 317
  • 1
  • 3
  • 11
  • Reliably? Without JavaScript? You can't. Pseudo-classes match elements, not classes of elements, so if the structure isn't fixed, you're out of luck. Mixing pseudo-classes with elements that have a class acts like a filter when selecting elements, rather than just a selector. See http://stackoverflow.com/questions/2717480/css-selector-for-first-element-with-class?rq=1 – j08691 Mar 03 '17 at 20:59
  • 1
    @j08691: It depends on the use case. Unlike the one you link to, the solution to the problem here would potentially match multiple elements, so the structure doesn't have to be fixed, and it is therefore possible with selectors. – BoltClock Mar 04 '17 at 07:16

0 Answers0