Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SCSS for loop variable not working

Tags:

css

loops

sass

I'm trying to change the opacity of the nth-of-type elements with SCSS and a loop. I have tested manually setting .slick-slide:nth-of-type(2) and it does work just when I try it from the @for loop it will not take the variable $i. What am I doing wrong here, it seems that everything should be correct.

@for $i from 1 through 4 {
    .slick-slide:nth-of-type($i) {
        opacity: 100 / $i;
    }
}
like image 983
Joe Scotto Avatar asked Sep 18 '25 17:09

Joe Scotto


1 Answers

When you're using a variable in a selector in SCSS you need to use the interpolation syntax and wrap it like this #{$myVariable}. Your example would then be:

@for $i from 1 through 4 {
    .slick-slide:nth-of-type(#{$i}) {
        opacity: 100 / $i;
    }
}
like image 120
Karl Laurentius Roos Avatar answered Sep 20 '25 17:09

Karl Laurentius Roos