Skip to main content

Sass: @while

@while

The @while rule, written @while <expression> { ... }, evaluates its block if its expression returns true. Then, if its expression still returns true, it evaluates its block again. This continues until the expression finally returns false.

The .scss file

@use "sass:math";

/// Divides `$value` by `$ratio` until it's below `$base`.
@function scale-below($value, $base, $ratio: 1.618) {
@while $value > $base {
$value: math.div($value, $ratio);
}
@return $value;
}

$normal-font-size: 16px;
sup {
font-size: scale-below(20px, 16px);
}

and the .css file

sup {
font-size: 12.36094px;
}
note

@each or @for are clearer for the reader and often faster to compile.

Table of Contents