0

I want to set second child with class active special rules but I cannot target it. I have got 10 items and few of them have got active classes. I need to target second child with active class.
Is there any way to use nth-child and combine two classes?

.container {
  border: 2px solid red;
  display: grid;
  grid-auto-columns: 1fr;
  grid-auto-flow: column;
  column-gap: 3rem;
}

.item {
  background-color: pink;
}
.item.active {
  font-size: 2rem;
}
  // Works when passed 5
.item.active:nth-child(2) {
  background-color: yellow;
}
<div class="container">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
  <div class="item active">4</div>
  <div class="item active">5</div>
  <div class="item active">6</div>
  <div class="item">7</div>
  <div class="item">8</div>
  <div class="item">9</div>
  <div class="item">10</div>
</div>
Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34
Freestyle09
  • 4,894
  • 8
  • 52
  • 83

1 Answers1

1

Note: Currently only Safari supports the following feature

Use :nth-child and specify the .active requirement.

If you are not using Safari, the following snippet will not work.

.container {
  display: flex;
}

.item {
  background-color: pink;
}

.item.active {
  font-size: 2rem;
}

.item:nth-child(2 of .active) {
  background-color: blue;
}
<div class="container">
  <div class="item">1</div>
  <div class="item active">2</div>
  <div class="item active">3</div>
  <div class="item active">4</div>
  <div class="item">5</div>
  <div class="item">6</div>
</div>
David Callanan
  • 5,601
  • 7
  • 63
  • 105