Home Sass Colors Sass Colors Definition
Definition and Usage Sass colors are points in a color space (like rgb or lab), just like CSS. Write them as hex, names, or functions such as rgb(), hsl(), lab(), and color().
Legacy vs modern spaces. rgb, hsl, and hwb are legacy. Modern spaces (oklch, lab, display-p3, …) need recent Dart Sass (color spaces since 1.79).
Quote channel names. Write "red" as a quoted string so Sass does not treat it as a color. Spaces like oklch stay unquoted.
Syntax
Syntax @use "sass:color";
$brand: #998099; // hex
$name: midnightblue; // CSS name
$rgb: rgb(204 102 153); // modern rgb()
$lab: lab(32.4% 38.4 -47.7 / 0.7);
$lighter: color.scale($brand, $lightness: +15%, $space: oklch);
$mixed: color.mix($brand, midnightblue, $method: oklch); Pattern: write a color → store it in a variable → transform with sass:color (optionally naming a $space / $method).
Code
SCSS Examples Open View Compiled CSS for verified Dart Sass output (matches official Sass docs samples where shown).
1. Literals 2. UI 3. scale/mix 4. Missing 5. change Example 1 — Official Color Literals Hex, names, rgb(), lab(), and color() all count as color values.
.swatches {
--hex: #{#f2ece4};
--hex-alpha: #{#b37399aa};
--name: #{midnightblue};
--rgb: #{rgb(204 102 153)};
--lab: #{lab(32.4% 38.4 -47.7 / 0.7)};
--p3: #{color(display-p3 0.597 0.732 0.576)};
} .swatches {
--hex: #f2ece4;
--hex-alpha: rgba(179, 115, 153, 0.6666666667);
--name: midnightblue;
--rgb: rgb(204, 102, 153);
--lab: lab(32.4% 38.4 -47.7 / 0.7);
--p3: color(display-p3 0.597 0.732 0.576);
} How It Works Hex / name / functions are all color values.8-digit hex includes alpha.lab / display-p3 keep their spaces in output.Matches the official literals sample idea.Example 2 — Everyday UI Colors Use colors directly in property declarations.
.btn {
color: #191970;
background: #b37399aa;
border-color: rgba(204, 102, 153, 0.5);
} .btn {
color: #191970;
background: rgba(179, 115, 153, 0.6666666667);
border-color: rgba(204, 102, 153, 0.5);
} How It Works #191970 is opaque hex (midnightblue).#b37399aa compiles with alpha.rgba() also works as a value.Store repeated colors in variables .Example 3 — Official color.scale + color.mix Transform in oklch, still return a color in the original space.
@use "sass:color";
$venus: #998099;
.demo {
--scaled: #{color.scale($venus, $lightness: +15%, $space: oklch)};
--mixed: #{color.mix($venus, midnightblue, $method: oklch)};
} .demo {
--scaled: rgb(66.7264198057%, 56.710619738%, 66.7126514142%);
--mixed: rgb(37.622088384%, 29.2426327658%, 52.2385328393%);
} How It Works $space: oklch scales in a perceptual space.$method: oklch mixes in that space.Result stays in the input’s space (rgb here).Matches the official functions sample idea.Example 4 — Official Missing Channels none marks a channel that does not matter; mix / convert can fill it in.
@use "sass:color";
$grey: hsl(none 0% 50%);
.demo {
--mixed: #{color.mix($grey, blue, $method: hsl)};
--lch: #{color.to-space($grey, lch)};
} .demo {
--mixed: hsl(240, 50%, 50%);
--lch: lch(53.3889647411% 0 none);
} How It Works Hue is none because grey has no hue.mix takes blue’s hue when mixing.to-space can keep an analogous none.Check with color.is-missing() .Example 5 — color.change Alpha Set individual channels (including alpha) without deprecated globals.
@use "sass:color";
$base: #c69;
.demo {
--changed: #{color.change($base, $alpha: 0.5)};
--space: #{color.space($base)};
} .demo {
--changed: rgba(204, 102, 153, 0.5);
--space: rgb;
} How It Works #c69 expands to rgb channels.color.change sets alpha to 0.5.color.space reports rgb.More on color.change() . Edge Cases & Pitfalls Most color bugs are deprecated globals or mixing modern spaces on old compilers.
space Name the space when transforming Pass $space / $method (for example oklch) so results stay predictable.
quotes Quote channel names "red" is a channel; bare red is a color name.
1.79 Modern spaces need Dart Sass 1.79+ lab, oklch, and color() are not available on LibSass / old Dart Sass.
Toolchain
Sass Support Color values are core Sass; modern spaces need recent Dart Sass.
Feature Status Hex / names / rgb / hsl / hwb Supported 8-digit hex alpha Dart Sass 1.14+ (not LibSass) Modern color spaces Dart Sass 1.79+ sass:color moduleSupported—prefer over globals
Compile with the Dart Sass CLI .
Remember
Key Takeaways Write hex, names, or color functions.
Transform with sass:color (scale, mix, change).
Name a space like oklch when scaling or mixing.
none marks missing channels in modern color math.
One line: store brand colors as values, then reshape them with sass:color instead of hand-tuned hex variants.
Practice
Compile Notes Practical checklist:
1. Write colors as hex, names, or rgb()/hsl()/…
2. @use "sass:color" for transforms
3. Prefer color.scale / color.mix / color.change
4. Pass $space or $method when using modern spaces
5. Quote channel names ("red"); leave spaces unquoted (oklch) Frequently Asked Questions How do I write a color in Sass? Use hex codes (#f2ece4), CSS color names (midnightblue), or functions like rgb(), hsl(), hwb(), and—on modern Dart Sass—lab(), oklch(), and color().
What is a legacy color space? rgb, hsl, and hwb are legacy spaces that share the classic RGB gamut. Older Sass color functions freely work across these three. Newer spaces need recent Dart Sass.
How should I transform colors? Load @use "sass:color" and prefer color.scale(), color.mix(), and color.change() over deprecated globals like darken() or lighten().
Why quote channel names in color functions? Channel names like "red" must be quoted so Sass does not parse them as color values. Color spaces should be unquoted strings to match CSS (for example oklch).
Do hex colors support alpha? Yes on modern Sass: #b37399aa is an 8-digit hex with alpha. Older LibSass may not support that syntax.
Does color.scale() change the color space of the result? Sass can transform in another space internally, but it returns a color in the same space you gave it unless you call color.to-space().
Next: Sass Lists Store sequences of values—including lists of colors—for loops and helpers.
Lists → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion