math.log() is an exponential helper in the sass:math module. It returns the logarithm of a unitless number—natural log by default, or any base you pass. You will learn the optional $base argument, unit rules, pairing with math.pow, Dart Sass 1.25+ notes, and five compiled examples.
01
Concept
Logarithm
02
Module
@use "sass:math"
03
Default
Natural log (base e)
04
Units
Unitless only
05
Since
Dart Sass 1.25+
06
Practice
5 examples
Concept
What Is math.log()?
A logarithm answers: to what power must the base be raised to get this number? In Sass:
math.log(10) → about 2.302585093 (natural log of 10)
math.log(10, 10) → 1 (because 10¹ = 10)
math.log(8, 2) → 3 (because 2³ = 8)
Official docs group it with math.pow and math.sqrt under Exponential Functions. Logs undo powers: if you grow a scale with math.pow, you can recover the exponent with math.log.
💡
Beginner tip
Browsers never see math.log. Dart Sass evaluates it and writes a plain unitless number into your .css file (often via a custom property or multiplied back into a length).
Foundation
📝 Syntax
Load the math module, then pass a unitless number and an optional base:
styles.scss
@use "sass:math";
math.log($number, $base: null)
// $base omitted → natural log (base e)
// $base provided → log with that base
Parameters
Parameter
Type
Required
Description
$number
Number
Yes
Value to take the log of. Must be unitless.
$base
Number or null
No (default null)
Logarithm base. If null, Sass uses the natural log. When set, must be unitless.
Return value
Type
Situation
Result
Number
$base is null
Natural logarithm of $number (unitless)
Number
Custom $base
logbase(number) (unitless)
Error
Arguments have units
Compile fails—strip units first
Modules
📦 Loading sass:math
styles.scss
// Recommended — namespaced
@use "sass:math";
$ln: math.log(10);
// Optional — bring members into the current namespace
@use "sass:math" as *;
$ln: log(10);
// Optional — custom namespace
@use "sass:math" as m;
$ln: m.log(10);
⚠️
Put @use first
Keep @use "sass:math"; near the top of the file. math.log() needs Dart Sass 1.25+.
Numbers
📏 How Units Work
Unlike math.abs() or math.hypot(), math.log() accepts only unitless numbers. Convert lengths to ratios before logging.
Call
Result (approx.)
Notes
math.log(10)
2.302585093
Natural log (official example)
math.log(10, 10)
1
Common log base 10
math.log(8, 2)
3
Binary / power-of-two log
math.log(math.$e)
~1
ln(e) using math.$e
math.log(16px)
—
Error: must be unitless
math.log(math.div(32px, 16px), 2)
1
Strip units with a same-unit ratio first
Support
🛠 Compatibility
This is about Sass compilers, not browsers. Compiled CSS receives a plain number.
Implementation
math.log()
Notes
Dart Sass 1.25+
Yes
Preferred; needs @use "sass:math"
Dart Sass < 1.25
No
Upgrade Dart Sass
LibSass / Ruby Sass
No module API
Use Dart Sass for exponential math helpers
Cheat Sheet
⚡ Quick Reference
Goal
Code
Import math
@use "sass:math";
Natural log
math.log(10) → ~2.302585093
Log base 10
math.log(10, 10) → 1
Log base 2
math.log(8, 2) → 3
From a length ratio
math.log(math.div($a, $b), 2)
Inverse with pow
math.pow(10, math.log(10, 10)) → 10
Debug while learning
@debug math.log(10);
Compare
📋 log vs pow vs sqrt
Function
What it does
Best for
math.log($n, $base?)
Asks for the exponent
Scale steps, growth factors, “how many doublings?”
math.pow($base, $exp)
Raises base to a power
Modular scales, exponential growth
math.sqrt($n)
Square root (special power)
Diagonals / roots when you do not need hypot
All three require unitless inputs (except patterns where you reattach units after the math). Prefer math.log when you already have the result of a power and need the exponent back.
Compare
📋 Sass math.log() vs CSS / JS log
Sass math.log()
JS Math.log / CSS runtime
When it runs
Compile time (Dart Sass)
In the browser / runtime
Appears in CSS output?
No — only the numeric result
N/A (unless you set styles in JS)
Units
Unitless only
Plain numbers in JS
Best for
Fixed design tokens & scales in SCSS
Dynamic UI math from measured 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 natural log and custom base checks.
Example 1 — Basic Natural math.log()
Omit $base to get the natural logarithm (same as the official docs).
math.log(100, 10) is 2 because 10² = 100. math.log(16, 2) is 4 because 2⁴ = 16.
📈 Practical Patterns
Ratios, inverse with pow, and modular type scales.
Example 3 — Log of a Length Ratio
Strip units with math.div, then count doublings from a base size.
styles.scss
@use "sass:math";
$base: 16px;
$size: 32px;
$steps: math.log(math.div($size, $base), 2); // 1
.icon {
// one step up from the base on a binary scale
font-size: $base * math.pow(2, $steps);
--steps-from-base: #{$steps};
}
📤 Compiled CSS:
.icon {
font-size: 32px;
--steps-from-base: 1;
}
How It Works
math.div(32px, 16px) is the unitless ratio 2. Log base 2 of that ratio is one doubling step.
Example 4 — Pair with math.pow() (Inverse)
Recover a value after taking its log—useful when validating scale math.
Confusing natural log with log10 — omit base for ln; pass 10 for common log.
Invalid domains — non-positive numbers or bad bases can yield NaN or errors; keep inputs meaningful.
Old Dart Sass — need 1.25+; LibSass lacks the module API.
Pro Tips
💡 Best Practices
✅ Do
Use @use "sass:math" and math.log() in new SCSS
Keep arguments unitless (ratios from math.div)
Name the base clearly when it is not natural log
Pair with math.pow for scale round-trips
Prefer Dart Sass 1.25+
❌ Don’t
Expect the browser to re-evaluate math.log
Pass lengths with units directly into math.log
Assume omitted base means 10 (it means e)
Use Sass log for live measured layout (use JS instead)
Rely on LibSass for sass:math
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about math.log()
Compile-time logarithm—unitless only; default base is e.
5
Core concepts
📝01
Call
math.log($n, $base?)
API
📦02
Module
sass:math
@use
🔢03
Default
Natural log
Base e
📏04
Units
unitless only
Rule
⚡05
Since
Dart Sass 1.25+
Tooling
❓ Frequently Asked Questions
math.log($number, $base: null) returns the logarithm of $number. If $base is null (the default), it returns the natural logarithm (base e). Example: math.log(10) is about 2.302585093; math.log(10, 10) is 1.
Add @use "sass:math"; then call math.log($number) or math.log($number, $base). Requires Dart Sass 1.25+.
No. Official docs require $number and $base to be unitless. Strip units first, for example with math.div($a, $b) when both share a unit.
Official docs expose this as math.log() on sass:math. Prefer the module form with @use "sass:math".
They are inverse ideas: if y = math.log(x, b), then math.pow(b, y) recovers x (within floating-point precision). Pair them for scale exponents and growth math.
Natural log uses base e (about 2.718…). In Sass, math.log(10) with no second argument is ln(10). You can also write math.log($n, math.$e) for the same idea explicitly.
Did you know?
Official Sass docs place math.log under Exponential Functions with math.pow and math.sqrt. The constant math.$e (also Dart Sass 1.25+) is the natural base those logs use by default.
math.log() gives you compile-time logarithms: natural log by default, or any unitless base you choose. Strip units with ratios, pair with math.pow for scales, and keep Dart Sass at 1.25+.