Sass
CSS preprocessor

Sass color.space() Function

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
sass:color · Dart Sass 1.79+

What You’ll Learn

color.space() belongs to the built-in sass:color module. It answers: which color space is this color in? This page covers the unquoted string return value, hex / hsl / xyz / oklch examples, how it pairs with is-legacy, the Dart Sass 1.79+ requirement, and five examples.

01

Concept

Space name reader

02

Module

@use "sass:color"

03

Returns

Unquoted string

04

Legacy

rgb · hsl · hwb

05

Vs

is-legacy()

06

Practice

5 examples

What Is color.space()?

Space means “tell me the coordinate system this color lives in.” Official docs: returns the name of $color’s space as an unquoted string. A hex brand reports rgb; an HSL literal reports hsl; an XYZ color reports xyz.

  • color.space(#036)rgb
  • color.space(hsl(120deg 40% 50%))hsl
  • color.space(color(xyz-d65 0.1 0.2 0.3))xyz
💡
Beginner tip

Writing a color as hex does not create a special “hex space.” Hex is just a notation for rgb—so color.space(#036) is rgb.

📝 Syntax

Load the color module, then pass one color:

styles.scss
@use "sass:color";

// Dart Sass 1.79+
color.space($color)

Parameters

ParameterTypeDescription
$colorColorThe color whose space name you want (required).

Return value

TypeMeaning
Unquoted stringThe space name, e.g. rgb, hsl, oklch, xyz, srgb.

📦 Loading sass:color

There is no separate global space() built-in. Use the module form on Dart Sass 1.79+.

styles.scss
// Recommended — namespaced (Dart Sass 1.79+)
@use "sass:color";
$name: color.space(#036);

// Optional — bring members into the current namespace
@use "sass:color" as *;
$name: space(#036);

// Optional — custom namespace
@use "sass:color" as c;
$name: c.space(#036);
⚠️
Version gate

If Dart Sass reports Undefined function for color.space, upgrade to 1.79+.

🎨 Common Space Names You Will See

Official samples cover rgb, hsl, and xyz. In real projects you will also meet modern names like oklch and srgb.

How you write the colorTypical color.space()
Hex / rgb() / named colorrgb
hsl(…) / hsla(…)hsl
hwb(…)hwb
oklch(…)oklch
color(srgb …)srgb (modern—not legacy rgb)
color(xyz-d65 …)xyz

📋 space vs is-legacy

Both inspect a color’s space, but they answer different questions.

HelperReturnsTypical use
color.space()Space name string“Is this oklch or rgb?”
color.is-legacy()Boolean“Is this rgb/hsl/hwb?”
💡
Pair them

Use space when you need the exact name for branching or debugging. Use is-legacy when you only care about the legacy trio for adjust / change behavior.

🛠 Compatibility

This is about Sass compilers, not browsers. The space name is resolved at compile time—CSS never sees a color.space() call (unless you interpolate the string into a custom property).

FeatureDart SassNotes
color.space()1.79+Part of the modern color-spaces work
Related color.is-legacy()1.79+Boolean for rgb/hsl/hwb
Related color.to-space()1.79+Convert a color into another space
LibSass / Ruby SassNo modern module color-space API

⚡ Quick Reference

GoalCode
Import color@use "sass:color";
Hex spacecolor.space(#036)rgb
HSL spacecolor.space(hsl(120deg 40% 50%))hsl
XYZ spacecolor.space(color(xyz-d65 0.1 0.2 0.3))xyz
Branch in SCSSif(color.space($c) == oklch, …, …)
Legacy checkcolor.is-legacy($c)

Examples Gallery

Examples target Dart Sass 1.79+. Open View Compiled CSS to see the space names Sass emits (official docs results where noted).

📚 Getting Started

Official docs samples for rgb, hsl, and xyz.

Example 1 — Hex, HSL, and XYZ Space Names

Three notations, three space names (official docs).

styles.scss
@use "sass:color";

@debug color.space(#036); // rgb
@debug color.space(hsl(120deg 40% 50%)); // hsl
@debug color.space(color(xyz-d65 0.1 0.2 0.3)); // xyz

.report {
  --hex: #{color.space(#036)};
  --hsl: #{color.space(hsl(120deg 40% 50%))};
  --xyz: #{color.space(color(xyz-d65 0.1 0.2 0.3))};
}

How It Works

Interpolation writes the unquoted space name into CSS custom properties—handy for debugging tokens in DevTools.

Example 2 — Named Colors and Oklch

Named colors are rgb; modern polar colors report their own space.

styles.scss
@use "sass:color";

.tokens {
  --navy: #{color.space(navy)};
  --oklch: #{color.space(oklch(70% 0.1 120deg))};
  --hwb: #{color.space(hwb(120deg 20% 30%))};
}

How It Works

navy is a CSS named color in the legacy rgb space. oklch(...) and hwb(...) keep their own space names.

📈 Practical Patterns

srgb vs rgb, mixin guards, and to-space migrations.

Example 3 — Legacy rgb vs Modern srgb

Similar color models, different space names—important for migrations.

styles.scss
@use "sass:color";

$hex: #336699;
$modern: color(srgb 0.2 0.4 0.6);

.compare {
  --legacy-rgb: #{color.space($hex)};
  --modern-srgb: #{color.space($modern)};
  --legacy-flag: #{color.is-legacy($hex)};
  --srgb-legacy: #{color.is-legacy($modern)};
}

How It Works

Hex reports rgb and is legacy. color(srgb …) reports srgb and is not legacy—even though the models are related.

Example 4 — Branch on Space Name

Pick different mixin paths for rgb tokens vs everything else.

styles.scss
@use "sass:color";

@mixin space-note($c) {
  &::after {
    content: if(
      color.space($c) == rgb,
      "legacy-rgb",
      "other-space"
    );
  }
}

.chip-hex {
  @include space-note(#036);
}

.chip-oklch {
  @include space-note(oklch(70% 0.1 120deg));
}

How It Works

Compare against unquoted identifiers like rgb or oklch. Real mixins might choose different $space arguments for adjust / scale based on this check.

Example 5 — Space Changes After to-space

Converting a hex into oklch updates what space() reports.

styles.scss
@use "sass:color";

$brand: #036;
$brand-oklch: color.to-space($brand, oklch);

.migrate {
  --before: #{color.space($brand)};
  --after: #{color.space($brand-oklch)};
  --same-paint: #{color.same($brand, $brand-oklch)};
}

How It Works

The paint can stay the same (color.sametrue) while the space name changes from rgb to oklch. That is the core story of space migrations.

🚀 Real-World Use Cases

  • Token audits — log which spaces your design tokens currently use.
  • Mixin branching — choose rgb-safe vs oklch-aware color math.
  • Migration checks — confirm to-space actually changed the space name.
  • Debugging — emit --space custom properties in local builds.
  • Docs / teaching — show hex is rgb, not a separate space.
  • Pair with is-legacy — name for detail, boolean for quick gates.

🧠 How Compilation Works

1

Write SCSS

Call color.space($color) after @use.

Source
2

Inspect the color

Sass reads which space the value is stored in.

Inspect
3

Return the name

You get an unquoted string such as rgb or oklch.

Name
4

String ships in CSS

Interpolated names become plain identifiers in the stylesheet.

⚠️ Common Pitfalls

  • Expecting a “hex” space — hex reports rgb.
  • Confusing rgb with srgb — modern color(srgb …) is not legacy rgb.
  • Calling on older Dart Sass — needs 1.79+; expect Undefined function earlier.
  • Looking for a global space() — there is none; use the module form.
  • Using only space when you need a boolean — prefer is-legacy for yes/no legacy checks.

💡 Best Practices

✅ Do

  • Use @use "sass:color" and color.space() on Dart Sass 1.79+
  • Compare against unquoted names (== rgb, == oklch)
  • Pair with is-legacy and to-space during migrations
  • Emit space names in debug builds when auditing tokens
  • Upgrade the compiler before shipping modern color helpers

❌ Don’t

  • Expect the browser to re-run color.space
  • Assume LibSass/Ruby Sass support this API
  • Treat srgb as interchangeable with legacy rgb
  • Ignore Undefined function errors—upgrade instead
  • Skip documenting which spaces your tokens are supposed to use

Key Takeaways

Knowledge Unlocked

Five things to remember about color.space()

Read the space name—hex is rgb, modern spaces speak for themselves.

5
Core concepts
📦 02

Module

sass:color

@use
🎨 03

Returns

unquoted name

String
04

Hex

reports rgb

Legacy
05

Needs

Dart Sass 1.79+

Version

❓ Frequently Asked Questions

color.space($color) returns the name of that color’s space as an unquoted string. Example: color.space(#036) is rgb; color.space(hsl(120deg 40% 50%)) is hsl.
color.space() needs Dart Sass 1.79+. Older compilers report Undefined function.
Hex colors live in the legacy rgb space, so color.space(#036) returns rgb. Named colors like navy also report rgb.
space() returns the space name string (rgb, oklch, xyz, …). is-legacy() returns a boolean that is true only for rgb, hsl, and hwb.
Yes. Compare against unquoted names such as if(color.space($c) == rgb, …, …) or == oklch.
No. Use the module form color.space() after @use "sass:color".
Did you know?

Official Sass docs show color(xyz-d65 …) reporting the space name xyz—not xyz-d65. The function returns the space family name Sass uses for that color, which is what you compare in if branches and pass around as an unquoted identifier.

Conclusion

color.space() is the space-name reader for modern Sass colors. Use it to audit tokens, branch mixins, and confirm migrations after to-space—on Dart Sass 1.79+.

Continue with color.to-gamut(), color.same(), or the CSS introduction.

Explore color.to-gamut() Next

Map out-of-gamut colors with local-minde or clip (Dart Sass 1.79+).

Sass color.to-gamut() →

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