Yogesh Chauhan's Blog

How does while loop work in SCSS?

in SCSS on July 20, 2021

Similar posts:

for @each loop in SCSS

for loop in SCSS

SassScript has a rule @while that is used for writing a while loop with a code block.

Syntax


@while {
  ...
}

SassScript @while loop works same as JavaScript while loop.

  • If the expression returns true then run the code block inside the curly brackets.
  • After the execution of the code block inside the loop, check again if the expression is true.
  • If not, do not then stop execution of the loop.
  • Keep executing the code block inside the brackets till the expression is true.

Example


$min: 0;

@while $min < 5 {
    .p-#{$min*5} {
        padding: (5*$min) + px;
    }

    $min: $min + 1;
}

The code above will compile into the following CSS code:


.p-0 {
	 padding: 0px;
}
 .p-5 {
	 padding: 5px;
}
 .p-10 {
	 padding: 10px;
}
 .p-15 {
	 padding: 15px;
}
 .p-20 {
	 padding: 20px;
}


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
WordPress: How to get ACF field values from another post?WordPressINTERSECT and EXCEPT in PostgresPostgresHow to make a UILabel clickable in Swift 5?SwiftAlternate Style Sheets in CSSCSSHow to disable right click and drag and drop of images using jQuery?jQueryAn Example of Cross-site Scripting (XSS) Attack in PHP and How to Avoid It?PHPHow to show slider value in HTML5 range input using JavaScript?HTMLHow to avoid element shift on border change while hovering in CSS?CSSWhat are Controlled Components in React?ReactHow does AdSense calculate page loading time?JavaScriptHow to enable GD library support for PHP on windows server?PHPHow to import a CSS file using PHP code and not HTML code?PHP