2018-09-30 23:01:58 +01:00
---
id: 587d7dbe367417b2b2512bb9
title: Use @for to Create a Sass Loop
challengeType: 0
---
## Description
< section id = 'description' >
The < code > @for </ code > directive adds styles in a loop, very similar to a < code > for</ code > loop in JavaScript.
2018-10-20 09:38:39 +00:00
< code > @for </ code > is used in two ways: "start through end" or "start to end". The main difference is that the "start < b > to</ b > end" < em > excludes</ em > the end number as part of the count, and "start < b > through</ b > end" < em > includes</ em > the end number as part of the count.
2018-09-30 23:01:58 +01:00
Here's a start < b > through< / b > end example:
< blockquote > @for $i from 1 through 12 {< br > .col-#{$i} { width: 100%/12 * $i; }< br > }</ blockquote >
The < code > #{$i}< / code > part is the syntax to combine a variable (< code > i< / code > ) with text to make a string. When the Sass file is converted to CSS, it looks like this:
< blockquote > .col-1 {< br > width: 8.33333%;< br > }< br > < br > .col-2 {< br > width: 16.66667%;< br > }< br > < br > ...< br > < br > .col-12 {< br > width: 100%;< br > }< / blockquote >
This is a powerful way to create a grid layout. Now you have twelve options for column widths available as CSS classes.
< / section >
## Instructions
< section id = 'instructions' >
Write a < code > @for </ code > directive that takes a variable < code > $j</ code > that goes from 1 < b > to</ b > 6.
It should create 5 classes called < code > .text-1< / code > to < code > .text-5< / code > where each has a < code > font-size< / code > set to 10px multiplied by the index.
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
- text: Your code should use the < code > @for </ code > directive.
2018-10-20 21:02:47 +03:00
testString: assert(code.match(/@for /g), 'Your code should use the < code > @for </ code > directive.');
2018-10-04 14:37:37 +01:00
- text: Your < code > .text-1</ code > class should have a < code > font-size</ code > of 10px.
2018-10-20 21:02:47 +03:00
testString: assert($('.text-1').css('font-size') == '10px', 'Your < code > .text-1< / code > class should have a < code > font-size< / code > of 10px.');
2018-10-04 14:37:37 +01:00
- text: Your < code > .text-2</ code > class should have a < code > font-size</ code > of 20px.
2018-10-20 21:02:47 +03:00
testString: assert($('.text-2').css('font-size') == '20px', 'Your < code > .text-2< / code > class should have a < code > font-size< / code > of 20px.');
2018-10-04 14:37:37 +01:00
- text: Your < code > .text-3</ code > class should have a < code > font-size</ code > of 30px.
2018-10-20 21:02:47 +03:00
testString: assert($('.text-3').css('font-size') == '30px', 'Your < code > .text-3< / code > class should have a < code > font-size< / code > of 30px.');
2018-10-04 14:37:37 +01:00
- text: Your < code > .text-4</ code > class should have a < code > font-size</ code > of 40px.
2018-10-20 21:02:47 +03:00
testString: assert($('.text-4').css('font-size') == '40px', 'Your < code > .text-4< / code > class should have a < code > font-size< / code > of 40px.');
2018-10-04 14:37:37 +01:00
- text: Your < code > .text-5</ code > class should have a < code > font-size</ code > of 50px.
2018-10-20 21:02:47 +03:00
testString: assert($('.text-5').css('font-size') == '50px', 'Your < code > .text-5< / code > class should have a < code > font-size< / code > of 50px.');
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'html-seed' >
```html
< style type = 'text/sass' >
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
< / style >
< p class = "text-1" > Hello< / p >
< p class = "text-2" > Hello< / p >
< p class = "text-3" > Hello< / p >
< p class = "text-4" > Hello< / p >
< p class = "text-5" > Hello< / p >
```
< / div >
< / section >
## Solution
< section id = 'solution' >
2018-10-24 14:15:00 +03:00
```html
< style type = 'text/sass' >
@for $i from 1 through 5 {
.text-#{$i} { font-size: 10px * $i; }
}
< / style >
< p class = "text-1" > Hello< / p >
< p class = "text-2" > Hello< / p >
< p class = "text-3" > Hello< / p >
< p class = "text-4" > Hello< / p >
< p class = "text-5" > Hello< / p >
```
```html
< style type = 'text/sass' >
@for $i from 1 to 6 {
.text-#{$i} { font-size: 10px * $i; }
}
< / style >
< p class = "text-1" > Hello< / p >
< p class = "text-2" > Hello< / p >
< p class = "text-3" > Hello< / p >
< p class = "text-4" > Hello< / p >
< p class = "text-5" > Hello< / p >
2018-09-30 23:01:58 +01:00
```
< / section >