Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

CSS apply style to all child except if multiple of x

I want to apply style of to box but not which is a multiple of 3 (box 3, 6, 9, 12…. should not be styled)

Or else how do I do it the other way around. Styling only box which is multiple of 3 (box 3, 6, 9, 12…. should only be styled)

The snippet below only works for the first x box.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

.box {
  display: inline-block;
  background-color: #ffff00;
  height: 60px;
  width: 60px;
}
.box:not(:nth-child(3)) {
   background-color: #ff0000;
}
<div class="ctr">
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>

</div>

>Solution :

Use :nth-child(3n)

.box {
  display: inline-block;
  background-color: #ffff00;
  height: 60px;
  width: 60px;
}
.box:not(:nth-child(3n)) {
   background-color: #ff0000;
}
<div class="ctr">
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>

</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading