Sass math.sqrt()

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

Definition and Usage

math.sqrt($number) returns the square root of $number. The argument must be unitless. Negative inputs return NaN. Load it with @use "sass:math" on Dart Sass 1.25+.

Official samples.

math.sqrt(100) → 10 · math.sqrt(math.div(1, 3)) → 0.5773502692 · math.sqrt(-1) → NaN.

Compile-time root.

Sass writes a fixed number into CSS. For dynamic values, prefer CSS sqrt() in supporting browsers.

Unitless only.

math.sqrt(100px) fails. Strip units first, or multiply the result by a length: 16px * math.sqrt(2).

Syntax

SCSS
@use "sass:math";

math.sqrt($number); //=> unitless number

math.sqrt(100);              // 10
math.sqrt(math.div(1, 3));   // 0.5773502692
math.sqrt(-1);               // NaN
math.sqrt(0);                // 0

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

Pattern: keep ratios unitless → call math.sqrt → multiply by a length when you need CSS sizes. Pair with math.pow and math.hypot.

Inputs and Results

How common inputs behave:

InputMeaningExample result
100Perfect square10
math.div(1, 3)Fractional ratio0.5773502692
0Zero0
2Irrational rootabout 1.4142135624
-1NegativeNaN (often calc(NaN) in CSS)
100pxHas unitsError
vs math.pow / math.hypot.

math.pow($n, 0.5) is the same idea as math.sqrt($n). Prefer sqrt for clarity. Use math.hypot when you need √(a² + b²) with compatible length units.

SCSS Examples

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

Example 1 — Official Docs Sample

Square root of 100 is 10.

SCSS
@use "sass:math";

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

.basic {
  --hundred: #{math.sqrt(100)};
}

How It Works

  1. Input is unitless 100.
  2. Square root returns 10.
  3. Custom property receives the fixed number.
  4. Matches the official Sass docs sample.

Example 2 — Fractional Ratio

Official sample: root of 1/3 via math.div.

SCSS
@use "sass:math";

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

.third {
  --root: #{math.sqrt(math.div(1, 3))};
}

How It Works

  1. math.div(1, 3) builds a unitless ratio.
  2. math.sqrt takes that ratio.
  3. Result is about 0.5773502692.
  4. Prefer math.div over / for division.

Example 3 — Negative Input Returns NaN

Official docs: math.sqrt(-1) is NaN. Dart Sass may emit calc(NaN).

SCSS
@use "sass:math";

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

.neg {
  --nan: #{math.sqrt(-1)};
}

How It Works

  1. Negative input has no real square root.
  2. Sass returns NaN.
  3. Verified CSS shows calc(NaN).
  4. Guard with math.abs or avoid negatives.

Example 4 — Same Idea as math.pow($n, 0.5)

Prefer math.sqrt when you specifically need a square root.

SCSS
@use "sass:math";

.compare {
  --sqrt: #{math.sqrt(9)};
  --pow: #{math.pow(9, 0.5)};
}

How It Works

  1. math.sqrt(9) → 3.
  2. math.pow(9, 0.5) matches.
  3. Intent is clearer with sqrt.
  4. Use math.pow for other exponents.

Example 5 — Emit a Fixed CSS Length

Multiply the unitless root by a length unit after the call.

SCSS
@use "sass:math";

.box {
  width: math.sqrt(100) * 1px;
  --diagonal-factor: #{math.sqrt(2)};
}

How It Works

  1. math.sqrt(100) is unitless 10.
  2. Multiply by 1px for a CSS length.
  3. √2 is a useful diagonal scale factor.
  4. Keep CSS sqrt() when the value must stay dynamic.

Edge Cases & Pitfalls

Most sqrt bugs are passing units, negatives, or expecting a length back.

units

Unitless only

math.sqrt(100px) errors. Divide lengths to a ratio first, then reattach a unit.

NaN

Negatives become NaN

math.sqrt(-1) is not usable as a size. Filter or take math.abs first if needed.

stage

Compile time, not runtime

Sass cannot sqrt a live CSS custom property. Leave CSS sqrt() in the stylesheet for that.

module

Module-only helper

Use math.sqrt after @use "sass:math".

Sass Support

Prefer the module form with Dart Sass 1.25+.

APIStatus
math.sqrt()Dart Sass 1.25+ via @use "sass:math"
InputUnitless number
Return typeUnitless number (or NaN)
LibSass / Ruby SassNo modern sass:math sqrt

Compile with the Dart Sass CLI.

Key Takeaways

  • Returns the square root of a unitless number.
  • No CSS units on input — reattach lengths after the call.
  • Negatives → NaN; prefer math.sqrt over math.pow($n, 0.5).
  • Dart Sass 1.25+ via @use "sass:math".

One line: when a unitless ratio must become a fixed square root in CSS, call math.sqrt($number).

Compile Notes

Practical checklist:

checklist.txt
1. @use "sass:math"
2. Pass a unitless number (use math.div for ratios)
3. Expect a unitless root (or NaN for negatives)
4. Multiply by a length when you need CSS sizes
5. Use CSS sqrt() for runtime values

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. Dart Sass may serialize that as calc(NaN) in CSS. 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".

Next: Sass math.tan()

Get the tangent of an angle at compile time.

math.tan() →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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