@for
directive adds styles in a loop, very similar to a for
loop in JavaScript.
@for
is used in two ways: "start through end" or "start to end". The main difference is that "start to end" excludes the end number, and "start through end" includes the end number.
Here's a start through end example:
@for $i from 1 through 12 {The
.col-#{$i} { width: 100%/12 * $i; }
}
#{$i}
part is the syntax to combine a variable (i
) with text to make a string. When the Sass file is converted to CSS, it looks like this:
.col-1 {This is a powerful way to create a grid layout. Now you have twelve options for column widths available as CSS classes.
width: 8.33333%;
}
.col-2 {
width: 16.66667%;
}
...
.col-12 {
width: 100%;
}
@for
directive that takes a variable $j
that goes from 1 to 6.
It should create 5 classes called .text-1
to .text-5
where each has a font-size
set to 10px multiplied by the index.
@for
directive.
testString: 'assert(code.match(/@for /g), "Your code should use the @for
directive.");'
- text: Your .text-1
class should have a font-size
of 10px.
testString: 'assert($(".text-1").css("font-size") == "10px", "Your .text-1
class should have a font-size
of 10px.");'
- text: Your .text-2
class should have a font-size
of 20px.
testString: 'assert($(".text-2").css("font-size") == "20px", "Your .text-2
class should have a font-size
of 20px.");'
- text: Your .text-3
class should have a font-size
of 30px.
testString: 'assert($(".text-3").css("font-size") == "30px", "Your .text-3
class should have a font-size
of 30px.");'
- text: Your .text-4
class should have a font-size
of 40px.
testString: 'assert($(".text-4").css("font-size") == "40px", "Your .text-4
class should have a font-size
of 40px.");'
- text: Your .text-5
class should have a font-size
of 50px.
testString: 'assert($(".text-5").css("font-size") == "50px", "Your .text-5
class should have a font-size
of 50px.");'
```
Hello
Hello
Hello
Hello
Hello
```