Sass Calculations

Intermediate
10 min read
Updated: Nov 2026
5 examples
Dart Sass 1.40+

Definition and Usage

A calculation is the Sass value type for CSS math functions: calc(), clamp(), min(), and max(). Sass parses what is inside, simplifies it as far as it can, and writes the result back as CSS.

If every part has compatible units, the calculation collapses into an ordinary number. If it cannot (for example 100% - 20px), you keep a calculation value that serializes as calc(...).

Official idea.

calc(400px + 10px) becomes 410px, but calc(400px + 10%) stays a calculation because px and % cannot be added at compile time.

SassScript inside.

Variables, function calls, nested calculations, and interpolation all work inside a calculation. Write calc(100% - $gap) without #{}.

Operators stop at the edge.

You cannot apply Sass operators (*, +, …) to an unsimplified calculation. Wrap the math in another calc(), or use a meta function to inspect it.

Syntax

SCSS
$gap: 16px;

.layout {
  width: calc(100% - $gap * 2);        // calc(100% - 32px)
  height: calc(400px + 10px);          // 410px (simplified)
  font-size: clamp(1rem, 2vw, 2rem);   // stays a calculation
  padding: min(1rem, 4vw);
  margin: max(10px, 5px);              // 10px (simplified)
}

// Constants inside calculations
$full-turn: calc(pi * 2);              // 6.2831853072

Pattern: write the CSS math function → let Sass substitute variables and simplify → keep any unsimplified result inside another calc() if you need to scale it.

Calculation Rules

What you can and cannot do:

CaseResultWhy
calc(400px + 10px)410pxCompatible units collapse to a number
calc(400px + 10%)calc(400px + 10%)Different unit types stay a calculation
calc(100% - $gap)calc(100% - 16px)Variables are substituted
$calc * 2ErrorSass operators cannot touch a calculation
calc($calc * 2)calc((…) * 2)Wrap the math in another calculation
calc(pi), calc(e)NumbersConstants resolve to numbers
calc(infinity), calc(NaN)Stay as calc(...)Written back as CSS constants
Inspecting calculations.

Use meta.calc-name() and meta.calc-args() to read the name and arguments of an unsimplified calculation. meta.type-of() returns calculation.

SCSS Examples

Open View Compiled CSS to see which calculations Sass simplifies and which it keeps. Examples target a current Dart Sass.

Example 1 — Compatible Units Simplify

Sass computes what it can and leaves the rest as CSS.

SCSS
.box {
  a: calc(400px + 10px);
  b: calc(400px + 10%);
  c: calc(100% - 20px);
  d: min(10px, 5px);
  e: calc(2 * 3px);
  f: calc(1px + 2px + 3%);
}

How It Works

  1. 400px + 10px share a unit, so Sass returns 410px.
  2. 400px + 10% cannot be combined, so the calculation stays.
  3. min(10px, 5px) is decided at compile time and becomes 5px.
  4. 1px + 2px + 3% merges the pixel parts: calc(3px + 3%).

Example 2 — Variables and Math Inside calc()

Use Sass variables and operators directly—no interpolation needed.

SCSS
$gap: 16px;

.layout {
  a: calc(100% - $gap * 2);
  b: calc(100% - #{$gap});
  c: calc((100% - $gap) / 3);
  d: calc(#{$gap} + 10%);
}

How It Works

  1. $gap * 2 is evaluated first: 32px.
  2. #{$gap} is optional; it produces the same number.
  3. Division inside calc() is plain CSS division, not Sass / division.
  4. Result: only the percentage part remains as a runtime calculation.

Example 3 — Operators Cannot Touch a Calculation

An unsimplified calculation is not a number, so * 2 fails.

SCSS
$sidebar: calc(300px + 10%);

.main {
  width: $sidebar * 2;
}

How It Works

  1. calc(300px + 10%) cannot simplify, so $sidebar holds a calculation value.
  2. Sass * only works on numbers, so the compiler stops with an error.
  3. Fix: write calc($sidebar * 2)—the math moves into the CSS calculation.
  4. Simplified calculations (plain numbers) are not affected by this rule.

Example 4 — Constants pi, e, infinity, NaN

Known constants resolve to numbers; infinity and NaN stay as CSS.

SCSS
.math {
  a: calc(pi);
  b: calc(e);
  c: calc(pi * 2);
  d: calc(pi * 10px);
  e: calc(infinity);
  f: calc(-infinity);
  g: calc(NaN);
}

How It Works

  1. pi and e become 3.1415926536 and 2.7182818285.
  2. Math with a unit keeps the unit: pi * 10px → 31.4159265359px.
  3. infinity, -infinity, NaN cannot be a plain CSS number, so they stay as calc(...).
  4. Outside calculations, use math.$pi and friends.

Example 5 — clamp(), min(), max() and meta Helpers

Mixed units keep the CSS function; meta.calc-name() tells you which one.

SCSS
@use "sass:meta";

$fluid: clamp(1rem, 2vw + 0.5rem, 2rem);
$sum: calc(1px + 10%);

.title {
  font-size: $fluid;
  width: min(100%, 40rem);
  padding: max(1rem, 4vw);
  --fluid-name: #{meta.calc-name($fluid)};
  --sum-name: #{meta.calc-name($sum)};
  --sum-type: #{meta.type-of($sum)};
  --min-type: #{meta.type-of(min(1px, 2px))};
}

How It Works

  1. clamp(1rem, 2vw + 0.5rem, 2rem) has mixed units, so it stays as written.
  2. meta.calc-name() returns clamp and calc.
  3. meta.type-of() reports calculation for $sum.
  4. min(1px, 2px) simplifies to 1px, so its type is number.

Edge Cases & Pitfalls

Most calculation bugs come from using Sass operators on a calculation, or expecting runtime values to be simplified.

ops

No Sass operators on calculations

$calc * 2 fails. Put the math inside another calc() so the browser (or Sass) evaluates it.

units

Mixed units stay unsimplified

% with px, or vw with rem, cannot be combined at compile time. That is expected.

var

var() keeps the calculation alive

calc(var(--w) * 2) cannot be simplified. Sass only knows values it can see at compile time.

slash

/ inside calc() is CSS division

Outside a calculation, use math.div() instead of slash division.

Sass Support

First-class calculations require a modern Dart Sass.

FeatureStatus
calc(), clamp()Dart Sass 1.40+
min(), max() as calculationsDart Sass 1.40+ (when arguments are valid calculation syntax)
Constants pi, e, infinity, NaNDart Sass 1.60+
meta.calc-name() / meta.calc-args()Dart Sass 1.40+
LibSass / Ruby SassNo first-class calculations

Compile with the Dart Sass CLI.

Key Takeaways

  • Calculations are the value type for calc(), clamp(), min(), and max().
  • Compatible units simplify to plain numbers; others stay as CSS.
  • No Sass operators on unsimplified calculations—nest inside calc().
  • Constants pi and e resolve to numbers inside calculations.

One line: let Sass simplify calculations, and keep unsimplified math inside calc().

Compile Notes

Practical checklist:

checklist.txt
1. Write calc() / clamp() / min() / max() as in CSS
2. Use $variables directly inside calculations
3. Expect compatible units to collapse to numbers
4. Never apply Sass operators to an unsimplified calculation
5. Use meta.calc-name() / calc-args() to inspect one

Frequently Asked Questions

A calculation is the value type for CSS math functions calc(), clamp(), min(), and max(). Sass simplifies the contents when it can and otherwise writes the calculation to CSS.
When every operand has compatible units, such as calc(400px + 10px), Sass computes the result and returns a plain number like 410px.
An unsimplified calculation is not a number, so Sass operators cannot be applied to it. Use calc($calc * 2) instead.
Yes. Write calc(100% - $gap) directly. Interpolation with #{} also works but is usually not needed.
Yes. calc(pi) and calc(e) resolve to numbers. infinity, -infinity, and NaN are kept as CSS calc() expressions.
min() is the CSS calculation function that can stay in the output. math.min() is a Sass function that always returns a number at compile time.

Next: Sass Function Values

Store functions in variables with get-function and call them later.

Function Values →

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