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
Concept
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.
Foundation
📝 Syntax
Load the math module, then call the function:
styles.scss
@use "sass:math";
math.floor($number)
Parameters
Parameter
Type
Required
Description
$number
Number
Yes
Any Sass number to round down, with or without a unit (for example 4.9, 12.7px, -3.1rem).
Return value
Type
Situation
Result
Number
Already a whole number
Same value (unit kept)
Number
Has a fractional part (positive)
Next lower whole number
Number
Has a fractional part (negative)
Rounded toward -infinity (e.g. -4.2 → -5)
Modules
📦 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.
Numbers
📏 How Units Work
math.floor() rounds the numeric part and keeps the unit attached.
Input
Output
Notes
math.floor(4.9px)
4px
Rounds down; unit kept
math.floor(4px)
4px
Already whole
math.floor(1.9rem)
1rem
Relative unit kept
math.floor(10.99%)
10%
Percentage kept
math.floor(-2.3)
-3
Toward -infinity
Support
🛠 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 — preferred
Yes
LibSass
No built-in modules
Yes (legacy)
Ruby Sass
No built-in modules
Yes (legacy)
Prefer Dart Sass for new work. Older pipelines may still expose the global floor() name.
Cheat Sheet
⚡ Quick Reference
Goal
Code
Import math
@use "sass:math";
Round down
math.floor($n)
Fractional → lower whole
math.floor(4.9) → 4
Use in a property
width: math.floor($raw);
Legacy global call
floor($n)
Debug while learning
@debug math.floor(4.9px);
Compare
📋 floor vs ceil vs round
All three are bounding helpers in sass:math. The difference is direction.
Input
math.floor
math.ceil
math.round
4.2
4
5
4
4.9
4
5
5
4
4
4
4
-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.
Compare
📋 Sass math.floor() vs CSS Rounding
Sass math.floor()
CSS rounding functions
When it runs
Compile time (Dart Sass)
In the browser
Appears in CSS output?
No — only the result
Yes (where supported)
Needs modern browser support?
No
Yes
Best for
Fixed design tokens & SCSS math
Runtime / dynamic values
Hands-On
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.
7.5 → 7px, 15 stays whole, 22.5 → 22px. Each step stays at or below the fractional product.
Applications
🚀 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.
Watch Out
⚠️ Common Pitfalls
Forgetting @use — math.floor is undefined until you load sass:math.
Confusing floor with truncate — math.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.
Pro Tips
💡 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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about math.floor()
Compile-time round-down—units included.
5
Core concepts
📝01
Call
math.floor($n)
API
📦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.
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.