0

I want to select first and last element with class "x" in each div.

When I use the CSS code below, only last element in first container and first element in last container are selected. How do I select first and last elements of same class in parent elements?

div>.x:first-child {
  background: green;
}

div>.x:last-child {
  background: blue;
}
<div>
  <p>
    123
  </p>
  <p class="x">
    123
  </p>
  <p class="x">
    123
  </p>
</div>
<div>
  <p class="x">
    123
  </p>
  <p class="x">
    123
  </p>
  <p>
    123
  </p>
</div>
Alon Eitan
  • 11,997
  • 8
  • 49
  • 58

0 Answers0