Directive @for in SASS
The @for directive can be used to output specific styles in the amount specified by the counter variable. The first version of @for looks like this:
@for $var from (begin) through (end)
In this case, the range strictly includes the values (start) and (end). It is worth paying attention to the fact that (start) and (end) - must return integers, since they set the counting direction, i.e. if (start) is less than (end), the counter will increase and, accordingly, if vice versa - decrease.
Let's consider the operation of the @for directive in the first syntax variant using the following example:
@for $count from 1 through 5 {
.li-#{$count} {
width: $count + px;
}
}
Compilation result:
.li-1 {
width: 1px;
}
.li-2 {
width: 2px;
}
.li-3 {
width: 3px;
}
.li-4 {
width: 4px;
}
.li-5 {
width: 5px;
}
In the second version, the entry looks like this:
@for $var from (start) to (end)
Here the value (end) is discarded.
Let's take the previous example and write it in the second version:
@for $count from 1 to 5 {
.li-#{$count} {
width: $count + px;
}
}
Now take a look at the resulting code:
.li-1 {
width: 1px;
}
.li-2 {
width: 2px;
}
.li-3 {
width: 3px;
}
.li-4 {
width: 4px;
}
Tell me what the result of compiling the following code will be:
@for $var from 5 through 1 {
li-#{$var} {
padding: 10 * $var + px;
}
}
Tell me what the result of compiling the following code will be:
@for $var from 1 to 3 {
.navbar-link-#{$var} {
color: blue;
width: 10px * $var;
}
}