Sass @while repeats a block as long as a condition stays true. This page covers how the loop works, when to prefer @for / @each, safe variable updates, truthiness, and five compiled examples.
01
Loop
@while
02
Condition
Until false
03
Mutate
Update vars
04
Prefer
@for / @each
05
Functions
Scale & strip
06
Practice
5 examples
Concept
What Is @while?
Official docs: the @while rule evaluates its block if the expression returns true. Then, if the expression still returns true, it evaluates the block again. This continues until the expression finally returns false.
Write it as @while <condition> { … }. Inside the block you almost always update one or more variables that the condition depends on—otherwise the loop never ends.
Compile-time only—browsers never see @while.
Best for open-ended or complex stop conditions (not simple 1…n counts).
Common inside @function helpers that shrink or grow a value.
Only false and null are falsey in Sass.
💡
Beginner tip
Think of @while as “keep doing this until the test fails.” If you already know the count or the list of items, reach for @for or @each first.
Guidance
⚠️ Prefer @for or @each When You Can
Official docs warn: although @while is necessary for a few particularly complex stylesheets, you are usually better off using either @each or @for if either will work. They are clearer for readers and often faster to compile.
Official docs: anywhere true or false are allowed, you can use other values. Only false and null are falsey. Every other value is truthy—including 0, empty strings, and empty lists.
💡
Fun fact
Checking for a substring with string.index($str, " ") works as a condition because a missing match returns null (falsey) and a found match returns a number (truthy).
Cheat Sheet
⚡ Quick Reference
Goal
Code
Basic loop
@while $i <= 4 { …; $i: $i + 1; }
Shrink until below base
@while $value > $base { $value: math.div(…); }
Build a list
@while $n <= $limit { $out: append(…); }
Stop on null
@while string.index($s, "0") == 1 { … }
Prefer simpler loops
@for / @each when the range or items are known
Hands-On
Examples Gallery
Each example updates loop state so the condition can become false. Open View Compiled CSS for verified output.
📚 Getting Started
Official-style helpers and a simple counter pattern.
Example 1 — scale-below() (Official Pattern)
Divide a value by the golden ratio until it drops below a base size.
Treating 0 as falsey — in Sass, 0 is truthy; only false and null fail.
Huge CSS output — a loose condition can emit thousands of rules.
Mutating shared state unexpectedly — keep loop variables local inside functions when possible.
Pro Tips
💡 Best Practices
✅ Do
Reach for @for / @each first when they fit
Update every variable the condition depends on
Keep complex loops inside @function helpers
Use clear stop conditions (> $base, length guards)
Test edge cases (exact base, single character, empty input)
❌ Don’t
Write @while true without a guaranteed exit
Copy-paste counters when @for would read better
Assume empty lists or 0 stop the loop
Emit unbounded utility classes into production CSS
Hide loop logic deep in selectors when a function is clearer
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Sass @while
Repeat while a condition is true—and always give the loop a way to stop.
5
Core concepts
📝01
@while
until false
Loop
🔄02
Mutate
update vars
Safety
⚖️03
Prefer
@for / @each
Clarity
✅04
Falsey
false & null
Truth
✓05
Functions
great home
Pattern
❓ Frequently Asked Questions
It evaluates a block while its condition expression returns true, then checks again, until the condition finally returns false.
Use @while for open-ended or complex stopping conditions. Prefer @for for numeric ranges and @each for lists or maps—they are clearer and often faster.
Only false and null are falsey. Empty strings, empty lists, and the number 0 are all truthy.
Yes, if the condition never becomes false. Always update the variables that the condition depends on, or the compiler may hang.
No. @while is compile-time only. Browsers see styles or values produced by the loop.
Yes. The official docs show scale-below(), which divides a value until it drops below a base—perfect inside @function.
Did you know?
Official Sass docs recommend @for and @each over @while whenever they fit—not because @while is deprecated, but because fixed ranges and collections are easier to read and often compile faster.
@while is the flexible control rule for open-ended stop conditions: keep running while a test is true, update your variables, and exit cleanly. Reach for it when @for and @each cannot express the logic.