Sass Numeric Functions
Sass Numeric Functions
Sass Numeric Functions are used to work with numbers such as sizes, spacing, calculations, and responsive values.
They help you perform math operations, rounding, and unit handling in a clean and readable way.
🔹 Types of Numbers in Sass
-
Unitless numbers →
10,1.5 -
Numbers with units →
10px,2em,50%
🔹 Common Sass Numeric Functions
1️⃣ abs()
Returns the absolute value.
✔ Output:
2️⃣ ceil()
Rounds a number up.
✔ Output:
3️⃣ floor()
Rounds a number down.
✔ Output:
4️⃣ round()
Rounds to the nearest whole number.
✔ Output:
5️⃣ max()
Returns the largest number.
✔ Output:
6️⃣ min()
Returns the smallest number.
✔ Output:
7️⃣ percentage()
Converts a unitless number to percentage.
✔ Output:
8️⃣ random()
Generates a random number.
✔ Output:
9️⃣ unit()
Returns the unit of a number.
✔ Output:
🔟 unitless()
Checks if a number has no unit.
✔ Output:
🔹 Mathematical Operations
Sass supports basic math:
🔹 Real-Life Example (Responsive Calculation)
🔹 Clamp-Like Calculation Example
❗ Important Notes
✔ Units must be compatible (px + px ✔, px + % ❌)
✔ Sass uses real math, not CSS math
✔ Division / needs clarity (use parentheses if needed)
📌 Summary
-
Its numeric functions help with math & layout
-
Useful for responsive design
-
Supports rounding, unit checking, random values
-
Improves precision & consistency
