Sass math.hypot() Function

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

What You’ll Learn

math.hypot() is a distance helper in the sass:math module. It returns the length of an n-dimensional vector from its components. With two sides of a right triangle, that length is the hypotenuse. You will learn unit rules, Dart Sass 1.25+ notes, list spreading, and five compiled examples.

01

Concept

Vector length

02

Module

@use "sass:math"

03

2D case

√(a² + b²)

04

Units

First arg wins

05

Since

Dart Sass 1.25+

06

Practice

5 examples

What Is math.hypot()?

Imagine each argument as one side of a multi-dimensional arrow. math.hypot() returns how long that arrow is. For two numbers a and b, it is the familiar Pythagorean result:

  • math.hypot(3, 4)5 (because 3² + 4² = 25, and √25 = 5)
  • Three or more arguments extend the same idea: √(a² + b² + c² + …)
  • With lengths that have units, Sass converts compatible units and returns the first argument’s unit

It sits next to math.abs() under distance functions: abs is 1D distance from zero; hypot is multi-dimensional distance from the origin.

💡
Beginner tip

Browsers never see math.hypot. Dart Sass evaluates it and writes a plain length or number into your .css file.

📝 Syntax

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

styles.scss
@use "sass:math";

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

Parameters

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

Return value

TypeSituationResult
NumberUnitless components√(sum of squares)—unitless
NumberCompatible length unitsSame formula; result uses the first argument’s unit
ErrorIncompatible unitsCompile fails

📦 Loading sass:math

styles.scss
// Recommended — namespaced
@use "sass:math";
$d: math.hypot(3, 4);

// Optional — bring members into the current namespace
@use "sass:math" as *;
$d: hypot(3, 4);

// Optional — custom namespace
@use "sass:math" as m;
$d: m.hypot(3, 4);
⚠️
Put @use first

Keep @use "sass:math"; near the top of the file. math.hypot() needs Dart Sass 1.25+.

📏 How Units Work

Compatible absolute lengths can mix. The output unit matches the first number.

CallResult (approx.)Notes
math.hypot(3, 4)5Classic 3–4–5 triangle
math.hypot(6px, 8px)10pxSame unit
math.hypot(1in, 10cm, 50px)~4.095inCompatible lengths; unit from first arg
math.hypot(5)5Single component = abs-like length
math.hypot(10px, 2em)Error: incompatible units

🛠 Compatibility

This is about Sass compilers, not browsers. Compiled CSS receives a plain number.

Implementationmath.hypot()Notes
Dart Sass 1.25+YesPreferred; needs @use "sass:math"
Dart Sass < 1.25NoUpgrade Dart Sass
LibSass / Ruby SassNo module APIUse Dart Sass, or approximate with math.sqrt of summed squares

⚡ Quick Reference

GoalCode
Import math@use "sass:math";
2D hypotenusemath.hypot(3, 4)5
With unitsmath.hypot(6px, 8px)10px
Spread a listmath.hypot($lengths...)
Result unitUnit of the first argument
Debug while learning@debug math.hypot(3, 4);

📋 hypot vs abs vs manual √(a²+b²)

ApproachWhat it doesBest for
math.abs($n)1D distance from zeroSingle signed length
math.hypot($a, $b)Multi-dimensional lengthDiagonals / combined offsets
math.sqrt($a*$a + $b*$b)Same 2D idea, more typingOlder code without hypot

Prefer math.hypot() when you have two or more components—it is clearer and scales cleanly to more dimensions.

📋 Sass math.hypot() vs CSS / JS hypot

Sass math.hypot()JS Math.hypot() / runtime
When it runsCompile time (Dart Sass)In the browser / runtime
Appears in CSS output?No — only the resultN/A (unless you set styles in JS)
Fluid / measured layout?No (values must be known)Yes (element sizes, viewport)
Best forFixed design math in SCSSDynamic UI geometry

Examples Gallery

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

📚 Getting Started

Official-style hypotenuse checks and list spreading.

Example 1 — Basic math.hypot()

The classic 3–4–5 right triangle.

styles.scss
@use "sass:math";

@debug math.hypot(3, 4); // 5

.line {
  // illustrative custom property
  --len: #{math.hypot(3, 4)};
  width: math.hypot(3px, 4px);
}

How It Works

3² + 4² = 25, and the square root is 5. With px, the result is 5px.

Example 2 — Spread Compatible Lengths

Official-style list with mixed absolute units; result unit follows the first item.

styles.scss
@use "sass:math";

$lengths: 1in, 10cm, 50px;
@debug math.hypot($lengths...); // ~4.0952775683in

.box {
  // use the computed distance as a max size
  max-width: math.hypot($lengths...);
}

How It Works

Sass converts cm and px into the same absolute space as 1in, then returns the vector length in inches.

📈 Practical Patterns

Diagonals, 2D offsets, and three-component lengths.

Example 3 — Card Diagonal from Width and Height

Size a decorative diagonal stroke from known card dimensions.

styles.scss
@use "sass:math";

$card-w: 320px;
$card-h: 180px;

.card-diagonal {
  width: math.hypot($card-w, $card-h);
}

How It Works

The diagonal of a 320×180 rectangle is √(320² + 180²), which Sass computes at compile time.

Example 4 — Combined X/Y Shadow Offset Distance

Turn separate horizontal and vertical offsets into one blur-related length.

styles.scss
@use "sass:math";

$x: 6px;
$y: 8px;
$reach: math.hypot($x, $y); // 10px

.elevated {
  box-shadow: #{$x} #{$y} #{$reach} rgba(0, 0, 0, 0.2);
}

How It Works

The blur radius matches the geometric reach of the offset vector (another 6–8–10 triple).

Example 5 — Three Components

Hypot works beyond 2D—pass as many components as you need.

styles.scss
@use "sass:math";

// √(2² + 3² + 6²) = √(4+9+36) = √49 = 7
$depth: math.hypot(2, 3, 6);

.space {
  --depth: #{$depth};
  letter-spacing: math.hypot(0.02em, 0.01em); // unitless-like em family
}

How It Works

Three unitless components produce 7. Two em values stay in the em family for the letter-spacing result.

🚀 Real-World Use Cases

  • Diagonals — size ribbons, strokes, or loaders from known width and height.
  • Offset reach — turn x/y shadow or translate pairs into one distance.
  • Icon geometry — derive stroke lengths from component offsets in a design system.
  • Multi-axis tokens — combine several fixed spacing axes into one magnitude.
  • Teaching distance — show how abs (1D) relates to hypot (nD) in sass:math.
  • Fixed vs measured — use Sass when sides are known; use JS when you measure the DOM.

🧠 How Compilation Works

1

Write SCSS

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

Source
2

Normalize units

Sass checks compatibility and converts absolute lengths as needed.

Validate
3

Square, sum, root

Compute √(a² + b² + …) and attach the first argument’s unit.

Hypot
4

Plain CSS ships

The browser only receives the final length or number.

⚠️ Common Pitfalls

  • Forgetting @usemath.hypot needs sass:math loaded.
  • Incompatible unitspx with em fails; keep a compatible family.
  • Surprised by output unit — the first argument decides the result unit.
  • Expecting fluid layout math — hypot cannot resolve live viewport or measured element sizes.
  • Old Dart Sass — need 1.25+; LibSass lacks the module API.

💡 Best Practices

✅ Do

  • Use @use "sass:math" and math.hypot() in new SCSS
  • Keep arguments in a compatible unit family
  • Put the preferred result unit first in the argument list
  • Spread lists with $list... for generated components
  • Prefer Dart Sass 1.25+

❌ Don’t

  • Expect the browser to re-evaluate math.hypot
  • Mix relative and absolute units carelessly
  • Use Sass hypot for DOM-measured diagonals (use JS instead)
  • Hand-roll √(a²+b²) when hypot is available
  • Rely on LibSass for sass:math

Key Takeaways

Knowledge Unlocked

Five things to remember about math.hypot()

Compile-time vector length—first unit wins.

5
Core concepts
📦 02

Module

sass:math

@use
📏 03

2D

√(a²+b²)

Hypot
🎯 04

Unit

first arg

Rule
05

Since

Dart Sass 1.25+

Tooling

❓ Frequently Asked Questions

math.hypot($number...) returns the length of the n-dimensional vector whose components are the given numbers. For two values it is the hypotenuse: square root of a² + b². Example: math.hypot(3, 4) is 5.
Add @use "sass:math"; then call math.hypot(...) with one or more numbers. You can spread a list: math.hypot($lengths...). Requires Dart Sass 1.25+.
Arguments must all have compatible units or all be unitless. The result uses the unit of the first number. Example: math.hypot(1in, 10cm, 50px) returns a value in inches.
Official docs expose this as math.hypot() on sass:math. Prefer the module form with @use "sass:math".
math.abs() is one-dimensional distance from zero. math.hypot() combines several components into one length—like going from 1D absolute value to multi-dimensional distance.
Yes, when the side lengths are known at compile time. For viewport-based diagonals, compute in CSS or JavaScript instead—Sass hypot cannot use fluid vw values the same way.
Did you know?

Official Sass docs place math.hypot under Distance Functions next to math.abs. Absolute value asks “how far from zero on a line?” Hypot asks “how far from the origin in many directions?”

Conclusion

math.hypot() turns one or more known components into a single length at compile time. Keep units compatible, put the preferred result unit first, and use runtime tools when the sides of your triangle are measured in the browser.

Continue with math.is-unitless(), math.abs(), or math.log().

More Sass Math

Continue with unit presence checks in the math module.

math.is-unitless() →

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