Sass math.sqrt() Function

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

What You’ll Learn

math.sqrt() is an exponential helper in the sass:math module. It returns the square root of a unitless number. You will learn unit rules, NaN behavior, comparisons with math.pow and math.hypot, Dart Sass 1.25+ notes, and five compiled examples.

01

Concept

Square root

02

Module

@use "sass:math"

03

Call

math.sqrt($n)

04

Units

Unitless only

05

Since

Dart Sass 1.25+

06

Practice

5 examples

What Is math.sqrt()?

A square root answers: what number, multiplied by itself, gives this value? In Sass:

  • math.sqrt(100)10 (because 10 × 10 = 100)
  • math.sqrt(math.div(1, 3)) → about 0.5773502692
  • math.sqrt(-1)NaN (no real square root)

Official docs group it with math.pow() and math.log() under Exponential Functions. It is the readable form of math.pow($n, 0.5).

💡
Beginner tip

Browsers never see math.sqrt. Dart Sass evaluates it and writes a plain number into your .css file. Reattach units yourself when needed: $side * math.sqrt(2) for a square’s diagonal factor.

📝 Syntax

Load the math module, then pass a unitless number:

styles.scss
@use "sass:math";

math.sqrt($number)
// $number must be unitless

Parameters

ParameterTypeRequiredDescription
$numberNumberYesValue to take the square root of. Must be unitless.

Return value

TypeSituationResult
NumberNon-negative unitless input$number (unitless)
NumberNegative inputNaN (as in the official docs)
ErrorArgument has unitsCompile fails—strip units first or multiply a length after

📦 Loading sass:math

styles.scss
// Recommended — namespaced
@use "sass:math";
$r: math.sqrt(100);

// Optional — bring members into the current namespace
@use "sass:math" as *;
$r: sqrt(100);

// Optional — custom namespace
@use "sass:math" as m;
$r: m.sqrt(100);
⚠️
Put @use first

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

📏 How Units Work

Like math.pow() and math.log(), math.sqrt() accepts only unitless numbers. Use ratios for length math, or multiply the root by a length.

CallResult (approx.)Notes
math.sqrt(100)10Official example
math.sqrt(math.div(1, 3))~0.5773502692Official fractional example
math.sqrt(-1)NaNNo real square root
16px * math.sqrt(2)~22.627416998pxReattach units after sqrt
math.sqrt(math.div(9px, 1px))3Strip units with a same-unit ratio first
math.sqrt(100px)Error: must be unitless

🛠 Compatibility

This is about Sass compilers, not browsers. Compiled CSS receives a plain number (or NaN if the input was negative).

Implementationmath.sqrt()Notes
Dart Sass 1.25+YesPreferred; needs @use "sass:math"
Dart Sass < 1.25NoUpgrade Dart Sass
LibSass / Ruby SassNo module APIUse Dart Sass for exponential math helpers

⚡ Quick Reference

GoalCode
Import math@use "sass:math";
Perfect squaremath.sqrt(100)10
Fractionmath.sqrt(math.div(1, 3))
Negative inputmath.sqrt(-1)NaN
Same as half powermath.pow($n, 0.5)math.sqrt($n)
Diagonal factor$side * math.sqrt(2)
Debug while learning@debug math.sqrt(100);

📋 sqrt vs pow vs hypot

FunctionWhat it doesBest for
math.sqrt($n)Square root of one unitless numberClear √n when you already have a single value
math.pow($n, 0.5)Same math, more general APIWhen you already use pow for other exponents
math.hypot($a, $b)√(a² + b² …) with unit supportDiagonals / vector length from components

Prefer math.sqrt for a single square root. Prefer math.hypot() when you have two or more sides and want Sass to handle compatible length units.

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

Sass math.sqrt()JS Math.sqrt / CSS sqrt()
When it runsCompile time (Dart Sass)In the browser / runtime
Appears in CSS output?No — only the numeric resultCSS sqrt() can stay in the stylesheet (modern browsers)
UnitsUnitless onlyJS: plain numbers; CSS: follows CSS math rules
Best forFixed design tokens in SCSSDynamic or fluid values that must resolve at runtime

Examples Gallery

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

📚 Getting Started

Official-style square roots, fractions, and NaN.

Example 1 — Basic math.sqrt()

Take the square root of a perfect square—same as the official docs.

styles.scss
@use "sass:math";

@debug math.sqrt(100); // 10

.demo {
  --root-100: #{math.sqrt(100)};
  --root-81: #{math.sqrt(81)};
}

How It Works

10 × 10 = 100 and 9 × 9 = 81, so the square roots are 10 and 9.

Example 2 — Square Root of a Fraction

Use math.div for clear division, then take the root.

styles.scss
@use "sass:math";

@debug math.sqrt(math.div(1, 3)); // ~0.5773502692

.ratio {
  --sqrt-one-third: #{math.sqrt(math.div(1, 3))};
  // same idea via pow
  --also: #{math.pow(math.div(1, 3), 0.5)};
}

How It Works

√(1/3) is about 0.577…. math.pow(..., 0.5) matches math.sqrt for the same input.

Example 3 — Negative Input Returns NaN

Real square roots are not defined for negatives in Sass math.

styles.scss
@use "sass:math";

@debug math.sqrt(-1); // NaN

.guard {
  // illustrative — prefer non-negative inputs in real UI tokens
  --bad-root: #{math.sqrt(-1)};
  --safe-root: #{math.sqrt(math.abs(-9))}; // 3
}

How It Works

Official docs show math.sqrt(-1) as NaN. Wrapping with math.abs() first keeps the root real when you only care about magnitude.

📈 Practical Patterns

Diagonals with reattached units and hypot comparisons.

Example 4 — Square Diagonal Factor

A square’s diagonal is side × √2. Keep √2 unitless, then multiply.

styles.scss
@use "sass:math";

$side: 120px;
$diagonal: $side * math.sqrt(2);

.tile-stroke {
  width: $diagonal;
  --sqrt-2: #{math.sqrt(2)};
}

How It Works

math.sqrt(2) is about 1.414…. Multiplying by 120px puts the unit back on the length.

Example 5 — Manual Hypotenuse vs math.hypot()

For two unitless sides, √(a² + b²) matches hypot. Prefer hypot when units matter.

styles.scss
@use "sass:math";

$a: 3;
$b: 4;

$manual: math.sqrt($a * $a + $b * $b); // 5
$built-in: math.hypot($a, $b);          // 5

.line {
  --manual: #{$manual};
  --hypot: #{$built-in};
  width: math.hypot(3px, 4px); // hypot keeps units
}

How It Works

Both paths give the classic 3–4–5 result. Use math.hypot() when arguments already have compatible length units.

🚀 Real-World Use Cases

  • Diagonal factors — size strokes or ribbons with $side * math.sqrt(2).
  • Geometry tokens — store √2, √3, and other constants as custom properties.
  • Area → side — recover a side length from a known square area (unitless ratio first).
  • Teaching roots — show how sqrt relates to math.pow($n, 0.5).
  • Safe magnitudes — pair with math.abs before sqrt when inputs may be signed.
  • Hypot fallback — understand √(a²+b²) before switching to math.hypot.

🧠 How Compilation Works

1

Write SCSS

Call math.sqrt($number) after @use.

Source
2

Require unitless

Sass rejects numbers with units; convert ratios first if needed.

Validate
3

Take square root

Compute √n, or produce NaN for negative inputs.

Sqrt
4

Plain CSS ships

The browser only receives the final number (or a length you rebuild).

⚠️ Common Pitfalls

  • Passing px / remmath.sqrt needs unitless values; use ratios or multiply after.
  • Forgetting @usemath.sqrt needs sass:math loaded.
  • Negative roots — you get NaN, not an error; guard with math.abs when needed.
  • Reinventing hypot — for multi-component lengths with units, prefer math.hypot.
  • Old Dart Sass — need 1.25+; LibSass lacks the module API.

💡 Best Practices

✅ Do

  • Use @use "sass:math" and math.sqrt() in new SCSS
  • Keep the argument unitless
  • Multiply by rem/px after computing the root factor
  • Prefer math.hypot for multi-side length math
  • Prefer Dart Sass 1.25+

❌ Don’t

  • Expect the browser to re-evaluate math.sqrt
  • Pass lengths with units directly into math.sqrt
  • Ignore NaN from negative inputs in production tokens
  • Use Sass sqrt for live measured layout (use JS / CSS instead)
  • Rely on LibSass for sass:math

Key Takeaways

Knowledge Unlocked

Five things to remember about math.sqrt()

Compile-time square root—unitless only; negatives become NaN.

5
Core concepts
📦 02

Module

sass:math

@use
03

Same as

pow($n, 0.5)

Alias idea
📏 04

Units

unitless only

Rule
05

Since

Dart Sass 1.25+

Tooling

❓ Frequently Asked Questions

math.sqrt($number) returns the square root of a unitless number. Example: math.sqrt(100) is 10; math.sqrt(math.div(1, 3)) is about 0.5773502692.
Add @use "sass:math"; then call math.sqrt($number). Requires Dart Sass 1.25+.
No. Official docs require $number to be unitless. Strip units with a ratio first, or multiply the result by a length afterward—for example 16px * math.sqrt(2).
math.sqrt(-1) returns NaN (not a number), matching the official Sass docs. Avoid negatives unless you intentionally handle NaN.
math.sqrt($n) is the clear square-root form of math.pow($n, 0.5). math.hypot(a, b) is √(a² + b²) and can keep compatible length units; sqrt alone needs unitless input.
Official docs expose this as math.sqrt() on sass:math. Prefer the module form with @use "sass:math".
Did you know?

Official Sass docs place math.sqrt under Exponential Functions with math.pow and math.log. The example math.sqrt(-1) returning NaN is intentional—Sass stays in real numbers, not complex ones.

Conclusion

math.sqrt() gives you compile-time square roots for unitless numbers. Reattach lengths afterward, watch for NaN on negatives, and reach for math.hypot when you are combining multiple length components.

Continue with math.tan(), math.pow(), or math.hypot().

More Sass Math

Continue with tangent and slope helpers in the math module.

math.tan() →

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