DEV Community

Nikhil Chandra Roy
Nikhil Chandra Roy

Posted on

Easiest way to loop the css :nth-child pseudo class by scss

There are couple of way to loop the pseudo class by scss but the while loop make it more easy to understand and the process.

$count: 1;
@while $count < 10+1{
  .classname:nth-of-type(#{$count}){
    width: auto;
  }
    $count: $count+1
}

Enter fullscreen mode Exit fullscreen mode

this output is:


.classname:nth-of-type(1) {
  width: auto;
}

.classname:nth-of-type(2) {
  width: auto;
}

.classname:nth-of-type(3) {
  width: auto;
}

.classname:nth-of-type(4) {
  width: auto;
}

.classname:nth-of-type(5) {
  width: auto;
}

.classname:nth-of-type(6) {
  width: auto;
}

.classname:nth-of-type(7) {
  width: auto;
}

.classname:nth-of-type(8) {
  width: auto;
}

.classname:nth-of-type(9) {
  width: auto;
}

.classname:nth-of-type(10) {
  width: auto;
}
Enter fullscreen mode Exit fullscreen mode

Thanks.

Discussion (0)