The Sass @while rule is written @while <expression> { … }. It evaluates its block while the expression is truthy, then checks again, until the expression finally becomes falsey.
Inside the block you almost always update variables the condition depends on — otherwise the loop never ends. Prefer @for for numeric ranges and @each for lists or maps when either will work.
Official pattern.
Per the Sass docs: scale-below() divides a value until it drops below a base — a classic @while use inside @function.
Compile-time only.
Browsers never see @while — only CSS or values the loop produces.
Prefer @for / @each.
Official docs: @while is needed for a few complex cases, but @for and @each are clearer and often faster to compile.
Most @while bugs are missing updates, wrong stop tests, or using it when @for/@each would do.
infinite
Always mutate the condition
If you forget $i: $i + 1 (or the equivalent), the compiler can hang. Update every variable the test uses.
prefer
Known range? Use @for
Manual counters are harder to read. Reach for @for / @each first, per the official guidance.
truthy
0 is still truthy
Only false and null stop a condition. Do not rely on 0 alone as a stop signal.
bounds
> vs >= changes the last step
Threshold loops often miss or include the floor by one step. Check exclusive vs inclusive carefully.
Toolchain
Sass Support
@while is a core Dart Sass control rule. It is not a browser CSS feature — support means your Sass compiler understands it.
Implementation
Status
Dart Sass
Supported (recommended)
LibSass / node-sass
Supported (deprecated toolchain)
Ruby Sass
Supported (end-of-life)
Browsers
N/A — only see compiled CSS
Prefer Dart Sass for new projects. SCSS and indented syntax both support @while.
Remember
Key Takeaways
Repeat:@while runs while the condition stays truthy.
Mutate state every pass so the loop can stop.
Prefer @for / @each when the range or items are known.
Great in functions that shrink or grow until a threshold.
One line:@while keeps going until the condition fails — update variables so it can.
Compile
Compile Notes
@while never reaches the browser. Conceptual flow:
flow.txt
1. Evaluate the condition
2. If falsey → exit the loop
3. If truthy → evaluate the block (emit CSS / update vars)
4. Go back to step 1
5. Continue compiling after the loop
Frequently Asked Questions
It evaluates a block while its condition expression is truthy, then checks again, until the condition finally becomes falsey.
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 falsey. 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.