Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

Sass Flow Control

Sass @while

Posted in Sass Tutorial
Updated on Sep 29, 2024
By Mari Selvan
👁️ 13 - Views
⏳ 4 mins
💬 1 Comment
Sass @while

Photo Credit to CodeToFun

🙋 Introduction

The @while directive in Sass allows you to create loops that continue to execute as long as a specified condition is true.

This is particularly useful when you need to perform repetitive tasks, such as generating multiple styles with slight variations or dynamically creating classes. With @while, you can leverage the power of loops to reduce redundancy in your stylesheets.

💡 Syntax

The syntax for the @while directive is similar to traditional programming languages. It includes a condition that is evaluated before each iteration of the loop.

Syntax
Copied
Copy To Clipboard
@while <condition> {
  // Code to be executed repeatedly
}

🔢 Parameters

  • condition: The expression that controls the loop's execution. It typically involves a counter variable that is updated within the loop.

📝 Example Usage

To better understand how @while works, let's look at some examples.

📜 Example 1: Simple Counter

example.scss
Copied
Copy To Clipboard
$i: 1;

@while $i <= 5 {
  .item-#{$i} {
    width: 10px * $i;
  }
  $i: $i + 1;
}

In this example, a loop runs five times, creating five classes (.item-1 to .item-5). The width of each class increases by 10 pixels with each iteration.

📜 Example 2: Loop with Color Shades

example.scss
Copied
Copy To Clipboard
$shade: 100;

@while $shade >= 0 {
  .bg-shade-#{$shade} {
    background-color: lighten(#333, $shade%);
  }
  $shade: $shade - 20;
}

Here, a loop generates classes with background colors that become progressively lighter. The loop runs until the shade value is no longer greater than or equal to zero.

📜 Example 3: Generating a Responsive Grid

example.scss
Copied
Copy To Clipboard
$columns: 1;

@while $columns <= 12 {
  .col-#{$columns} {
    width: 100% / 12 * $columns;
  }
  $columns: $columns + 1;
}

In this example, a responsive grid system is created with columns ranging from .col-1 to .col-12. Each class defines the width of a column as a percentage of the total grid width.

⚠️ Common Pitfalls

  1. Infinite Loops: Be cautious when writing the loop condition. If the condition never becomes false, the loop will run indefinitely, potentially causing the compiler to crash.
  2. Overuse: While loops are powerful, overusing them can lead to complex and hard-to-maintain stylesheets. Always consider if a loop is the best solution for your problem.

🎉 Conclusion

The @while directive in Sass is a powerful tool for managing repetitive tasks within your stylesheets. It allows you to create dynamic, scalable styles with minimal code, making your CSS more efficient and easier to maintain. Whether you're generating grid systems, color palettes, or repetitive style rules, @while offers a flexible and intuitive way to handle these tasks.

By mastering the @while loop, you can write more sophisticated Sass code, reduce redundancy, and improve the maintainability of your stylesheets. Experiment with different loop conditions and tasks to fully explore the capabilities of @while.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy