Sass math.max() Function

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
sass:math · Dart Sass

What You’ll Learn

math.max() is a bounding helper in the sass:math module. It returns the highest of one or more numbers and keeps compatible CSS units. You will learn module loading, list spreading with ..., how it differs from CSS max(), and five compiled examples.

01

Concept

Highest value

02

Module

@use "sass:math"

03

Args

One or more numbers

04

Spread

$list...

05

Vs CSS

max()

06

Practice

5 examples

What Is math.max()?

math.max() compares its arguments and returns the largest one. That is useful when several tokens or calculations compete and you want the winner baked into CSS.

  • math.max(1px, 4px)4px
  • math.max(50px, 30px, 100px)100px
  • math.max($widths...) → highest item in the list

Pair it with math.min() when you need the smallest value, or with math.clamp() when you need both a floor and a ceiling around one preferred number.

💡
Beginner tip

Browsers never see math.max. Dart Sass evaluates it and writes only the winning number into your .css file.

📝 Syntax

Load the math module, then pass one or more numbers:

styles.scss
@use "sass:math";

math.max($number...)
// also: math.max($list...)

Parameters

ParameterTypeRequiredDescription
$number...Number(s)YesOne or more Sass numbers to compare. All must have compatible units, or all be unitless. Spread a list with $list....

Return value

TypeSituationResult
NumberTwo or more comparable valuesThe highest argument (unit kept)
NumberSingle argumentThat same number
ErrorIncompatible unitsCompile fails (cannot compare)

📦 Loading sass:math

styles.scss
// Recommended — namespaced
@use "sass:math";
$w: math.max(12px, 24px);

// Optional — bring members into the current namespace
@use "sass:math" as *;
$w: max(12px, 24px);

// Optional — custom namespace
@use "sass:math" as m;
$w: m.max(12px, 24px);
⚠️
Put @use first

Keep @use "sass:math"; near the top of the file. Do not confuse Sass math.max with CSS max() left in the output stylesheet.

📏 How Units Work

Arguments must be comparable. Compatible absolute lengths can mix; relative vs absolute often cannot.

CallResultNotes
math.max(1px, 4px)4pxSame unit
math.max(50px, 30px, 100px)100pxMany arguments
math.max(1cm, 15mm)15mm (or equal in cm)Compatible absolute lengths
math.max(2, 9, -1)9Unitless
math.max(10px, 2em)Error: incompatible units

🛠 Compatibility

This is about Sass compilers, not browsers. Compiled CSS receives a plain number unless you wrote CSS max() yourself.

Implementation@use "sass:math"Global max()
Dart Sass (1.23+)Yes — preferredYes
LibSassNo built-in modulesYes (legacy)
Ruby SassNo built-in modulesYes (legacy)

⚡ Quick Reference

GoalCode
Import math@use "sass:math";
Highest of twomath.max(1px, 4px)4px
Highest of manymath.max(50px, 30px, 100px)
Spread a listmath.max($widths...)
Legacy global callmax($a, $b)
Debug while learning@debug math.max(1px, 4px);

📋 max vs min vs clamp

HelperReturnsTypical use
math.max(...)Highest argumentPick the largest width, gap, or token
math.min(...)Lowest argumentPick the smallest bound
math.clamp(min, n, max)Value inside a rangeForce one number between two bounds

📋 Sass math.max() vs CSS max()

Sass math.max()CSS max()
When it runsCompile time (Dart Sass)In the browser
Appears in CSS output?No — only the resultYes — as max(...)
Viewport / fluid values?No (values must be known)Yes (e.g. max(2rem, 4vw))
Best forFixed tokens & SCSS formulasResponsive minimum floors at runtime

If any candidate depends on the viewport, use CSS max(). If every candidate is a known Sass number, use math.max() and ship one length.

Examples Gallery

Each example uses @use "sass:math". Open View Compiled CSS to see what Dart Sass emits.

📚 Getting Started

Official-style max calls and list spreading.

Example 1 — Basic math.max()

Compare two lengths and keep the larger one.

styles.scss
@use "sass:math";

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

.box {
  border-width: math.max(1px, 4px);
}

How It Works

Sass compares 1px and 4px, keeps 4px, and writes that into CSS.

Example 2 — Spread a List with ...

Pass every item in a list as a separate argument.

styles.scss
@use "sass:math";

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

.hero {
  max-width: math.max($widths...);
}

How It Works

$widths... expands to 50px, 30px, 100px. The largest is 100px.

📈 Practical Patterns

Widths, gaps, and pairing with min.

Example 3 — Ensure a Minimum Usable Width

When a computed size might be tiny, take the max with a floor token.

styles.scss
@use "sass:math";

$computed: 48px;
$floor: 64px;

.button {
  min-width: math.max($computed, $floor);
}

How It Works

64px wins over 48px, so the button never goes below the design floor. (For a full min/preferred/max corridor, prefer math.clamp().)

Example 4 — Largest Gap from a Scale

Pick the biggest spacing token for a section that needs more air.

styles.scss
@use "sass:math";

$space-sm: 8px;
$space-md: 16px;
$space-lg: 24px;

.section {
  gap: math.max($space-sm, $space-md, $space-lg);
}

How It Works

Among 8px, 16px, and 24px, Sass keeps 24px.

Example 5 — Max and Min Side by Side

Use both helpers when a layout needs the largest and smallest of the same set.

styles.scss
@use "sass:math";

$sizes: 12px, 40px, 24px;

.card {
  padding: math.min($sizes...);
  margin-block: math.max($sizes...);
}

How It Works

The same list yields 12px for min and 40px for max—compact padding, generous outer margin.

🚀 Real-World Use Cases

  • Token selection — pick the largest spacing, radius, or type size from a set.
  • Minimum floorsmath.max($computed, $floor) so sizes never go too small.
  • Map / list pipelines — spread generated lists of lengths and take the peak.
  • Border and stroke — ensure a visible minimum thickness.
  • Paired with min — report both ends of a scale for padding vs margin.
  • Fixed vs fluid — use Sass max for known numbers; CSS max for viewport math.

🧠 How Compilation Works

1

Write SCSS

Call math.max(...) or math.max($list...) after @use.

Source
2

Check units

Sass verifies the arguments can be compared safely.

Validate
3

Pick the highest

The largest comparable number wins; its unit stays attached.

Max
4

Plain CSS ships

The browser only receives the final winning number.

⚠️ Common Pitfalls

  • Forgetting @usemath.max needs sass:math loaded.
  • Incompatible unitspx vs em fails; keep a shared family.
  • Expecting fluid CSS — Sass max cannot mix vw the way CSS max can.
  • Confusing with clamp — max picks a winner among candidates; clamp bounds one preferred value.
  • LibSass pipelines — prefer Dart Sass for the module API.

💡 Best Practices

✅ Do

  • Use @use "sass:math" and math.max() in new SCSS
  • Keep arguments in a compatible unit family
  • Spread lists with $list... for generated values
  • Use CSS max() when viewport math is required
  • Prefer Dart Sass for built-in modules

❌ Don’t

  • Expect the browser to re-evaluate math.max
  • Mix relative and absolute units carelessly
  • Replace CSS fluid max() with Sass when vw is involved
  • Use max when you really need a min–max corridor (clamp)
  • Rely on LibSass for sass:math

Key Takeaways

Knowledge Unlocked

Five things to remember about math.max()

Compile-time highest value—compatible units required.

5
Core concepts
📦 02

Module

sass:math

@use
03

Result

highest

Max
📏 04

Units

compatible

Rule
05

When

compile time

Sass

❓ 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 two 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.
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.
Yes. Put the values in a list and spread it with ... — for example math.max($widths...) where $widths is 50px, 30px, 100px.
Did you know?

Official Sass docs place math.max under Bounding Functions next to min, clamp, ceil, floor, and round. Max and min are the simplest bounds: one winner from a set of candidates.

Conclusion

math.max() picks the highest comparable number at compile time. Spread lists when you need to, keep units compatible, and switch to CSS max() when the candidates must stay fluid with the viewport.

Continue with math.floor(), math.clamp(), or the Sass introduction.

More Sass Math

Continue with rounding down in the math module.

math.floor() →

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.

5 people found this page helpful