Using @for
In this recipe, you will learn how to create iterations by using for loops in Sass.
Getting ready
Read the Installing Sass for command line usage recipe of Chapter 1, Getting Started with Sass, to find out how to use Ruby Sass on the command line. If you are not familiar with for-loops already, try to study for-loops in other programming languages.
How to do it...
Learn how to use for loops in Sass by performing the following steps:
Create a Sass file called
iteration.scss
that will contain an SCSS code like that shown here:@for $i from 1 to 4 { .font-size-#{$i} { font-size: 0.8em * $i; } }
Then run the following command in your console:
sass --compass iteration.scss
After running the command from the previous step, the following compiled CSS will be outputted in your console:
.font-size-1 { font-size: 0.8em; } .font-size-2 { font-size: 1.6em; } .font-size-3 { font-size: 2.4em; }
How it works...
Those who are already familiar with using for loops in other programming languages...