Sass math.floor() Function

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

What You’ll Learn

math.floor() is a bounding helper in the sass:math module. It rounds a number down to the next lowest whole number and keeps CSS units. You will learn module loading, how floor differs from ceil and round, compiler notes, and five compiled SCSS examples.

01

Concept

Round down

02

Module

@use "sass:math"

03

Units

Preserved

04

Vs ceil

Opposite direction

05

When

Compile time

06

Practice

5 examples

What Is math.floor()?

Floor means “round toward negative infinity”—always down to the next whole number if there is any fractional part. Whole numbers are left alone.

  • math.floor(4)4
  • math.floor(4.2)4
  • math.floor(4.9)4
  • math.floor(-4.2)-5 (still toward -infinity)

Use it when overshooting is worse than undershooting—for example how many full columns fit in a width, how many whole steps fit in a budget, or a pixel size that must never exceed a maximum after division.

💡
Beginner tip

Browsers never see math.floor. Dart Sass evaluates it and writes the final whole number into your .css file.

📝 Syntax

Load the math module, then call the function:

styles.scss
@use "sass:math";

math.floor($number)

Parameters

ParameterTypeRequiredDescription
$numberNumberYesAny Sass number to round down, with or without a unit (for example 4.9, 12.7px, -3.1rem).

Return value

TypeSituationResult
NumberAlready a whole numberSame value (unit kept)
NumberHas a fractional part (positive)Next lower whole number
NumberHas a fractional part (negative)Rounded toward -infinity (e.g. -4.2-5)

📦 Loading sass:math

Like other math helpers, load the module with @use so the call is clearly namespaced.

styles.scss
// Recommended — namespaced
@use "sass:math";
$cols: math.floor(4.9);

// Optional — bring members into the current namespace
@use "sass:math" as *;
$cols: floor(4.9);

// Optional — custom namespace
@use "sass:math" as m;
$cols: m.floor(4.9);
⚠️
Put @use first

Keep @use "sass:math"; near the top of the file, before most other rules.

📏 How Units Work

math.floor() rounds the numeric part and keeps the unit attached.

InputOutputNotes
math.floor(4.9px)4pxRounds down; unit kept
math.floor(4px)4pxAlready whole
math.floor(1.9rem)1remRelative unit kept
math.floor(10.99%)10%Percentage kept
math.floor(-2.3)-3Toward -infinity

🛠 Compatibility

This is about Sass compilers, not browsers. Compiled CSS is always plain numbers.

Implementation@use "sass:math"Global floor()
Dart Sass (1.23+)Yes — preferredYes
LibSassNo built-in modulesYes (legacy)
Ruby SassNo built-in modulesYes (legacy)

Prefer Dart Sass for new work. Older pipelines may still expose the global floor() name.

⚡ Quick Reference

GoalCode
Import math@use "sass:math";
Round downmath.floor($n)
Fractional → lower wholemath.floor(4.9)4
Use in a propertywidth: math.floor($raw);
Legacy global callfloor($n)
Debug while learning@debug math.floor(4.9px);

📋 floor vs ceil vs round

All three are bounding helpers in sass:math. The difference is direction.

Inputmath.floormath.ceilmath.round
4.2454
4.9455
4444
-4.2-5-4-4

Choose floor when you must never go above the true size (fit counts, max widths). Choose ceil when you must never go below. Choose round for typical nearest-integer sizing.

📋 Sass math.floor() vs CSS Rounding

Sass math.floor()CSS rounding functions
When it runsCompile time (Dart Sass)In the browser
Appears in CSS output?No — only the resultYes (where supported)
Needs modern browser support?NoYes
Best forFixed design tokens & SCSS mathRuntime / dynamic values

Examples Gallery

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

📚 Getting Started

Official-style floor checks and unit preservation.

Example 1 — Basic math.floor()

Whole numbers stay put; fractions round down.

styles.scss
@use "sass:math";

@debug math.floor(4);    // 4
@debug math.floor(4.2);  // 4
@debug math.floor(4.9);  // 4

.panel {
  width: math.floor(119.8px);
}

How It Works

119.8px is not whole, so floor moves it to 119px. The browser only receives that final length.

Example 2 — Units Stay Attached

Floor does not strip rem or %.

styles.scss
@use "sass:math";

$gap: 1.75rem;
$share: 33.9%;

.card {
  margin-block: math.floor($gap);
  flex-basis: math.floor($share);
}

How It Works

1.75rem becomes 1rem; 33.9% becomes 33%. Only the numeric part is rounded down.

📈 Practical Patterns

Negatives, fit counts, and spacing scales.

Example 3 — Negative Numbers

Floor always moves toward negative infinity.

styles.scss
@use "sass:math";

@debug math.floor(-4.2);  // -5
@debug math.floor(-4);    // -4

.offset {
  // -3.2px → -4px (toward -infinity)
  margin-top: math.floor(-3.2px);
}

How It Works

Truncating toward zero would give -3, but floor on -3.2 is -4. Use math.ceil if you need the other direction.

Example 4 — How Many Full Columns Fit

After division, floor so you never claim more tracks than the width allows.

styles.scss
@use "sass:math";

$container: 1000px;
$card: 220px;
$columns: math.floor(math.div($container, $card)); // floor(4.545…) → 4

.grid {
  --columns: #{$columns};
  grid-template-columns: repeat(#{$columns}, 1fr);
}

How It Works

Four full 220px cards fit in 1000px. math.ceil would claim five and overflow.

Example 5 — Step Spacing Snapped Down

Build stepped margins that never exceed the fractional product.

styles.scss
@use "sass:math";

$base: 7.5px;

@for $i from 1 through 3 {
  .space-#{$i} {
    margin-bottom: math.floor($base * $i);
  }
}

How It Works

7.57px, 15 stays whole, 22.522px. Each step stays at or below the fractional product.

🚀 Real-World Use Cases

  • Fit counts — after division, floor so leftover space is unused instead of overflowing.
  • Maximum pixel sizes — round fractional widths down so content never exceeds a budget.
  • Grid tracks that must fit — claim only whole columns that actually fit.
  • Spacing scales — snap fractional steps down to whole CSS lengths.
  • Pagination leftovers — sometimes you need full pages only (pair with ceil for total pages).
  • Paired with ceil/round — pick the bounding rule that matches your design contract.

🧠 How Compilation Works

1

Write SCSS

Add @use "sass:math"; and call math.floor($n).

Source
2

Dart Sass runs

The compiler loads the math module and evaluates the call.

Compile
3

Round toward -infinity

Any fractional part moves down to the next whole number; units stay.

Floor
4

Plain CSS ships

The browser only receives the final whole number or length.

⚠️ Common Pitfalls

  • Forgetting @usemath.floor is undefined until you load sass:math.
  • Confusing floor with truncatemath.floor(-4.2) is -5, not -4.
  • Using floor when ceil is needed — undershooting can clip content or leave leftover items without a row.
  • Expecting runtime behavior — changing a CSS variable later will not re-run Sass math.floor.
  • LibSass pipelines — built-in modules need Dart Sass; migrate or use global floor() carefully.

💡 Best Practices

✅ Do

  • Use @use "sass:math" and math.floor() in new SCSS
  • Use floor for fit counts and sizes that must never overshoot
  • Compare with ceil / round when choosing a rule
  • Check edge cases with @debug, including negatives
  • Prefer Dart Sass for built-in modules

❌ Don’t

  • Expect the browser to evaluate math.floor
  • Assume negatives truncate toward zero
  • Reach for floor when nearest (round) is what design asked for
  • Rely on LibSass for @use "sass:math"
  • Floor every length blindly—it can shrink spacing unexpectedly

Key Takeaways

Knowledge Unlocked

Five things to remember about math.floor()

Compile-time round-down—units included.

5
Core concepts
📦 02

Module

sass:math

@use
03

Direction

toward -∞

Floor
📏 04

Units

preserved

CSS
05

When

compile time

Sass

❓ Frequently Asked Questions

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

In the official Sass docs, math.floor sits under Bounding Functions with ceil, round, clamp, min, and max. Floor and ceil are mirrors: one never overshoots, the other never undershoots.

Conclusion

math.floor() rounds toward negative infinity at compile time and keeps units. Reach for it when a count or size must never overshoot—and pair it with ceil or round when your design needs a different bound.

Continue with math.ceil(), math.clamp(), or the Sass introduction.

More Sass Math

Compare with rounding up in the math module.

math.ceil() →

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