Sass math.ceil()

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

Definition and Usage

math.ceil($number) rounds $number up to the next highest whole number (toward +infinity). Whole numbers stay the same. Units such as px are kept. Load it with @use "sass:math" (Dart Sass 1.23+ for modules; global ceil() also exists).

Official samples.

math.ceil(4) → 4 · math.ceil(4.2) → 5 · math.ceil(4.9) → 5.

Compile-time ceiling.

Sass resolves the value when you compile. The CSS file only contains the final whole number—unlike browser CSS rounding functions.

Prefer the module form.

Global ceil() still works but is deprecated in modern Dart Sass. New stylesheets should use math.ceil after @use "sass:math".

Syntax

SCSS
@use "sass:math";

math.ceil($number); //=> number (units preserved)

math.ceil(4);    // 4
math.ceil(4.2);  // 5
math.ceil(4.9);  // 5
math.ceil(4.2px); // 5px

// Global name (legacy / deprecated in modern Dart Sass)
ceil(4.2); // 5

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

Pattern: compute a fractional length or count → math.ceil so you never undershoot → compare with math.floor and math.round when the rounding direction differs.

Units and Signs

Ceiling always moves toward +infinity:

InputResult
math.ceil(4)4
math.ceil(4.2)5
math.ceil(4.9)5
math.ceil(4.2px)5px
math.ceil(-4.2)-4
math.ceil(-4.2px)-4px
ceil vs floor vs round.

For 4.2: ceil → 5, floor → 4, round → 4. For -4.2: ceil → -4, floor → -5.

SCSS Examples

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

Example 1 — Official Docs Samples

Whole numbers stay put; fractions move up.

SCSS
@use "sass:math";

@debug math.ceil(4);   // 4
@debug math.ceil(4.2); // 5
@debug math.ceil(4.9); // 5

.basic {
  --a: #{math.ceil(4)};
  --b: #{math.ceil(4.2)};
  --c: #{math.ceil(4.9)};
}

How It Works

  1. 4 is already whole → stays 4.
  2. 4.2 moves up to 5.
  3. 4.9 also moves up to 5.
  4. Matches the official Sass docs samples.

Example 2 — Units Are Preserved

Ceiling changes the numeric part only.

SCSS
@use "sass:math";

.units {
  --px: #{math.ceil(4.2px)};
}

How It Works

  1. 4.2px is a length with a fraction.
  2. Ceil yields 5 with the same unit.
  3. Output is 5px — ready for CSS properties.
  4. Works the same for rem, %, and other units.

Example 3 — Negative Numbers

Toward +infinity means -4.2 becomes -4, not -5.

SCSS
@use "sass:math";

.neg {
  --n: #{math.ceil(-4.2)};
  --px: #{math.ceil(-4.2px)};
}

How It Works

  1. Ceiling always moves toward +infinity.
  2. -4.2 steps to -4.
  3. Units still stick — -4.2px → -4px.
  4. Not the same as “away from zero.”

Example 4 — ceil vs floor vs round

Same input, three different whole numbers.

SCSS
@use "sass:math";

.compare {
  --ceil: #{math.ceil(4.2)};
  --floor: #{math.floor(4.2)};
  --round: #{math.round(4.2)};
}

How It Works

  1. ceil never undershoots → 5.
  2. floor never overshoots → 4.
  3. round picks nearest → 4 for 4.2.
  4. Choose ceil for counts you must not under-size.

Example 5 — Emit a Whole CSS Length

Guarantee a pixel-snapped width after a fractional token.

SCSS
@use "sass:math";

$w: 12.3px;

.box {
  width: math.ceil($w);
}

How It Works

  1. Store the fractional token in a variable.
  2. math.ceil bumps it to the next whole px.
  3. CSS sees 13px with no fraction left.
  4. Useful for grid tracks, tile sizes, and icon slots.

Edge Cases & Pitfalls

Most ceil bugs are mixing it up with floor on negatives, or using the deprecated global name.

sign

Negatives go toward +infinity

math.ceil(-4.2) is -4. Use math.floor if you need -5.

global

Global ceil() is deprecated

Prefer math.ceil after @use "sass:math" to avoid Dart Sass 3.0 removals.

stage

Compile time, not runtime

Sass cannot ceil a live CSS custom property. Use CSS math if the value must stay dynamic.

units

Units stay attached

math.ceil(4.2px) is 5px, not a unitless 5.

Sass Support

Prefer the module form with Dart Sass 1.23+.

APIStatus
math.ceil()Dart Sass 1.23+ via @use "sass:math"
Global ceil()Works; deprecated in modern Dart Sass
UnitsPreserved on the result
LibSass / Ruby SassUse global ceil() if modules are unavailable

Compile with the Dart Sass CLI.

Key Takeaways

  • Rounds up to the next whole number (toward +infinity).
  • Keeps units such as px on the result.
  • Different from floor/round — pick ceil when undershoot is forbidden.
  • Prefer math.ceil over deprecated global ceil().

One line: when a fractional Sass number must never undershoot, call math.ceil($number).

Compile Notes

Practical checklist:

checklist.txt
1. @use "sass:math"
2. Pass a number (units optional)
3. Expect the next whole value toward +infinity
4. Prefer math.ceil over global ceil()
5. Use floor/round when the direction differs

Frequently Asked Questions

math.ceil($number) rounds a number up to the next highest whole number. Whole numbers stay the same. For example math.ceil(4.2) is 5 and math.ceil(4) is 4. Units such as px are preserved.
Add @use "sass:math"; at the top of your SCSS file, then call math.ceil($number). Example: math.ceil(4.2px) compiles to 5px.
ceil always rounds up (toward +infinity). floor always rounds down (toward -infinity). round goes to the nearest whole number. Pick ceil when you must never undershoot—for example columns or tile counts.
Yes. Ceiling still means toward positive infinity. math.ceil(-4.2) is -4, not -5. That is different from rounding away from zero.
Yes. The global ceil($number) name still works for older stylesheets, but Dart Sass deprecates global built-ins. New projects should prefer math.ceil() from sass:math.
Same idea, different stage. Sass math.ceil() runs when you compile SCSS. CSS rounding functions run in the browser. Use Sass when the value must be fixed in the output CSS.

Next: Sass math.clamp()

Restrict a number to the range between a minimum and a maximum.

math.clamp() →

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