Sass math.log() Function

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

What You’ll Learn

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

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).

📝 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

ParameterTypeRequiredDescription
$numberNumberYesValue to take the log of. Must be unitless.
$baseNumber or nullNo (default null)Logarithm base. If null, Sass uses the natural log. When set, must be unitless.

Return value

TypeSituationResult
Number$base is nullNatural logarithm of $number (unitless)
NumberCustom $baselogbase(number) (unitless)
ErrorArguments have unitsCompile fails—strip units first

📦 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+.

📏 How Units Work

Unlike math.abs() or math.hypot(), math.log() accepts only unitless numbers. Convert lengths to ratios before logging.

CallResult (approx.)Notes
math.log(10)2.302585093Natural log (official example)
math.log(10, 10)1Common log base 10
math.log(8, 2)3Binary / power-of-two log
math.log(math.$e)~1ln(e) using math.$e
math.log(16px)Error: must be unitless
math.log(math.div(32px, 16px), 2)1Strip units with a same-unit ratio first

🛠 Compatibility

This is about Sass compilers, not browsers. Compiled CSS receives a plain number.

Implementationmath.log()Notes
Dart Sass 1.25+YesPreferred; needs @use "sass:math"
Dart Sass < 1.25NoUpgrade Dart Sass
LibSass / Ruby SassNo module APIUse Dart Sass for exponential math helpers

⚡ Quick Reference

GoalCode
Import math@use "sass:math";
Natural logmath.log(10)~2.302585093
Log base 10math.log(10, 10)1
Log base 2math.log(8, 2)3
From a length ratiomath.log(math.div($a, $b), 2)
Inverse with powmath.pow(10, math.log(10, 10))10
Debug while learning@debug math.log(10);

📋 log vs pow vs sqrt

FunctionWhat it doesBest for
math.log($n, $base?)Asks for the exponentScale steps, growth factors, “how many doublings?”
math.pow($base, $exp)Raises base to a powerModular 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.

📋 Sass math.log() vs CSS / JS log

Sass math.log()JS Math.log / CSS runtime
When it runsCompile time (Dart Sass)In the browser / runtime
Appears in CSS output?No — only the numeric resultN/A (unless you set styles in JS)
UnitsUnitless onlyPlain numbers in JS
Best forFixed design tokens & scales in SCSSDynamic UI math from measured values

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).

styles.scss
@use "sass:math";

@debug math.log(10); // 2.302585093

.demo {
  // illustrative custom property
  --ln-10: #{math.log(10)};
  opacity: math.log(math.$e); // ~1 → fully opaque idea
}

How It Works

With no second argument, Sass computes ln(10). math.log(math.$e) is ln(e), which is 1 (within Sass precision).

Example 2 — Custom Base (log10 and log2)

Pass a second argument to choose the base—common for decade and binary scales.

styles.scss
@use "sass:math";

@debug math.log(10, 10); // 1
@debug math.log(8, 2);   // 3

.metrics {
  --log10-100: #{math.log(100, 10)};
  --log2-16: #{math.log(16, 2)};
}

How It Works

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};
}

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.

styles.scss
@use "sass:math";

$value: 10;
$base: 10;
$exp: math.log($value, $base); // 1
$back: math.pow($base, $exp);  // 10

.check {
  --exp: #{$exp};
  --round-trip: #{$back};
}

How It Works

Log and pow cancel for these nice integers. With non-integers, expect tiny floating-point differences—same as in any calculator.

Example 5 — Find a Scale Step Index

Given a target size and ratio, solve for the step index with log.

styles.scss
@use "sass:math";

$base-size: 1rem;
$ratio: 1.25; // major third-ish modular scale
$target: 1.953125rem; // ≈ base × ratio^3

$index: math.log(math.div($target, $base-size), $ratio);

.heading {
  // rebuild from index to prove the step
  font-size: $base-size * math.pow($ratio, $index);
  --scale-index: #{$index};
}

How It Works

If size = base × ration, then n = logratio(size / base). Sass solves for n at compile time.

🚀 Real-World Use Cases

  • Modular type scales — find which step a size sits on.
  • Binary spacing — count doublings from a base length with log base 2.
  • Token validation — round-trip with math.pow to check scale math.
  • Opacity / weight curves — map unitless factors through log for softer ramps (then clamp).
  • Design docs in SCSS — expose --log-* custom properties for tooling.
  • Teaching exponentials — show how log undoes pow alongside math.sqrt.

🧠 How Compilation Works

1

Write SCSS

Call math.log($n) or math.log($n, $base) after @use.

Source
2

Require unitless

Sass rejects numbers with units; convert ratios first if needed.

Validate
3

Compute log

Natural log when base is null; otherwise log with your base.

Log
4

Plain CSS ships

The browser only receives the final unitless number (or a length you rebuild).

⚠️ Common Pitfalls

  • Passing px / remmath.log needs unitless values; use math.div first.
  • Forgetting @usemath.log needs sass:math loaded.
  • 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.

💡 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

Key Takeaways

Knowledge Unlocked

Five things to remember about math.log()

Compile-time logarithm—unitless only; default base is e.

5
Core concepts
📦 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.

Conclusion

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+.

Continue with math.is-unitless(), math.pow(), or math.max().

More Sass Math

Continue with bounding helpers in the math module.

math.max() →

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