Sass math.max()

Beginner
8 min read
Updated: Nov 2026
5 examples
Dart Sass 1.23+

Definition and Usage

math.max($number...) returns the highest of one or more numbers. Arguments must have compatible units or all be unitless. Units on the winning value are kept. Load it with @use "sass:math" (Dart Sass 1.23+ for modules; global max() also exists).

Official samples.

math.max(1px, 4px) → 4px · math.max(50px, 30px, 100px) via list spread → 100px.

Compile-time maximum.

Sass writes a fixed number into CSS. For fluid values like max(10vw, 2rem), leave CSS max() in the stylesheet.

Prefer the module form.

Global max() still works for older stylesheets. New code should use math.max after @use "sass:math" so the helper clearly comes from the math module.

Syntax

SCSS
@use "sass:math";

math.max($number...); //=> number

math.max(1px, 4px); // 4px

$widths: 50px, 30px, 100px;
math.max($widths...); // 100px

math.max(3, 9, 1); // 9

// Global name (legacy)
max(1px, 4px); // 4px

// Module API — prefer math.max after @use "sass:math"

Pattern: collect comparable tokens → call math.max (optionally with list spread) → emit the winning length. Pair with math.min and math.clamp.

Units and Lists

How arguments behave:

CallResult
math.max(1px, 4px)4px
math.max($widths...) with 50px, 30px, 100px100px
math.max(3, 9, 1)9
math.max(-2px, -8px, -1px)-1px (least negative)
Incompatible units (e.g. px/em)Compile error
List spread.

Official docs show spreading a list with $widths... so you can store candidates in one variable.

SCSS Examples

Examples target Dart Sass with @use "sass:math". Open View Compiled CSS for verified output (matches official Sass docs samples).

Example 1 — Official Docs Sample

Pick the larger of two lengths.

SCSS
@use "sass:math";

@debug math.max(1px, 4px); // 4px

.basic {
  --a: #{math.max(1px, 4px)};
}

How It Works

  1. Both args are px lengths.
  2. 4px is higher than 1px.
  3. Unit stays on the result.
  4. Matches the official Sass docs sample.

Example 2 — Official Docs Sample (List Spread)

Spread a list of candidates with ....

SCSS
@use "sass:math";

$widths: 50px, 30px, 100px;

@debug math.max($widths...); // 100px

.spread {
  --b: #{math.max($widths...)};
}

How It Works

  1. Store candidates in a list.
  2. Spread with $widths....
  3. Winner is 100px.
  4. Matches the official Sass docs sample.

Example 3 — Unitless Numbers

Works the same without units.

SCSS
@use "sass:math";

.unitless {
  --n: #{math.max(3, 9, 1)};
}

How It Works

  1. Three unitless values.
  2. Highest is 9.
  3. Result stays unitless.
  4. Useful for counts and scale indexes.

Example 4 — Negative Lengths

“Highest” means closest to +infinity, even among negatives.

SCSS
@use "sass:math";

.neg {
  --n: #{math.max(-2px, -8px, -1px)};
}

How It Works

  1. All values are negative px.
  2. -1px is the highest.
  3. Not the largest absolute magnitude.
  4. Use math.abs first if you need magnitude.

Example 5 — Design Tokens into CSS

Guarantee a minimum floor for width and font-size tokens.

SCSS
@use "sass:math";

$min: 12px;
$preferred: 18px;

.box {
  width: math.max($min, $preferred);
  font-size: math.max(0.875rem, 1rem);
}

How It Works

  1. Preferred values win when they are larger.
  2. Sass emits fixed lengths — no CSS max() left.
  3. Useful for build-time token floors.
  4. Keep CSS max() when a term needs vw/vh at runtime.

Edge Cases & Pitfalls

Most max bugs are incompatible units or expecting a live CSS function in the output.

units

Compatible units required

px with em fails. Check with math.compatible.

css

Sass vs CSS max()

Need viewport math? Leave CSS max() in the stylesheet instead of resolving it in Sass.

sign

Negatives use numeric order

math.max(-8px, -1px) is -1px, not the larger absolute value.

clamp

Need both bounds?

Use math.clamp when a value must sit between a min and a max.

Sass Support

Prefer the module form with Dart Sass 1.23+.

APIStatus
math.max()Dart Sass 1.23+ via @use "sass:math"
Global max()Works for older stylesheets; prefer math.max
InputOne or more numbers: unitless or compatible units
LibSass / Ruby SassUse global max() if modules are unavailable

Compile with the Dart Sass CLI.

Key Takeaways

  • Returns the highest of one or more numbers.
  • Keeps units on the winning value.
  • Supports list spread with $list....
  • Compile-time — use CSS max() for fluid values.

One line: when several Sass numbers must collapse to the largest, call math.max($number...).

Compile Notes

Practical checklist:

checklist.txt
1. @use "sass:math"
2. Pass comparable numbers (or spread a list)
3. Expect the highest value with its units
4. Prefer math.max over bare max() in new code
5. Use CSS max() when values must stay fluid

Frequently Asked Questions

math.max($number...) returns the highest of one or more Sass numbers. For example math.max(1px, 4px) is 4px. Arguments need compatible units (or all be unitless).
Add @use "sass:math"; then call math.max(...) with one or more numbers. You can also spread a list: math.max($widths...).
Yes. The global max($number...) name still works for older stylesheets. New projects should prefer math.max() from sass:math so the helper clearly comes from the math module.
Same idea, different stage. Sass math.max() runs at compile time and writes a fixed number. CSS max() stays in the stylesheet and can mix lengths like max(10vw, 2rem) in the browser.
If the numbers cannot be compared (for example 10px and 2em), Sass raises a compile error. Keep arguments in a compatible unit family or all unitless. Check with math.compatible().
Yes. Put the values in a list and spread it with ... — for example math.max($widths...) where $widths is 50px, 30px, 100px.

Next: Sass math.min()

Return the lowest of one or more numbers.

math.min() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful