Sass @for Directive with To Keyword

In Sass @for directive, the "to" keyword is used to specify the range formvalue to the value beforevalue.

Syntax:

Parameter explanation:

$var: It specifies the name of the variable like $i.

and:andare SassScript expressions which will return integers. If theis greater thanthen it decreases the value of counter variable and whenis lesser thanthen it increases the value of counter variable.


Sass @for Directive Example with to keyword

Let's take an example to demonstrate the usage of Sass @for directive with to keyword. We have an HTML file named "simple.html", having the following data.

HTML file: simple.html

Create a SCSS file named "simple.scss", having the following data.

SCSS file: simple.scss

Put the both file inside the root folder.

Now, open command prompt and run the watch command to tell SASS to watch the file and update the CSS whenever SASS file is changed.

Execute the following code: sass --watch simple.scss:simple.css

It will create a normal CSS file named "simple.css" in the same directory automatically.

For example:

Sass  to keyword1

The created CSS file "simple.css" contains the following code:

Now, execute the above html file, it will read the CSS value.

Output:

Sass  to keyword2

Now, you can see that padding is done only for 3 statements although we use the rule @for $i from 1 to 4 {.

This is the difference between through and to keyword in @for directive.





Latest Courses