Home Sass Color color.opacity() Sass color.opacity() Definition
Definition and Usage color.opacity($color) returns the alpha channel of a legacy color as a number between 0 and 1 . It is an alias of color.alpha($color) (and the global opacity($color) form).
Official Sass docs mark it deprecated — prefer color.channel($color, "alpha") on Dart Sass 1.79+.
No longer recommended. Because color.opacity() / color.alpha() are redundant with color.channel(), migrate opacity reads to the channel API in new code.
Official samples. color.opacity(rgb(210, 225, 221, 0.4)) → 0.4. color.alpha(#e1d7d2) → 1.
Not the CSS property. Sass color.opacity() reads one color token at compile time. CSS opacity fades a whole element (and its children) in the browser.
Syntax
Syntax @use "sass:color";
// Deprecated readers (still work on legacy colors)
color.opacity(rgb(210, 225, 221, 0.4)); // 0.4
color.alpha(#e1d7d2); // 1
opacity(rgba(#036, 0.5)); // 0.5
// Prefer today (Dart Sass 1.79+)
color.channel(rgb(210, 225, 221, 0.4), "alpha"); // 0.4 Pattern: recognize color.opacity / opacity in old files → rewrite with color.channel($c, "alpha").
Code
SCSS Examples Examples use color.opacity() / opacity() where noted. Open View Compiled CSS for verified output (matches official Sass docs samples).
1. Partial 2. Opaque 3. Global alias 4. Range 5. Prefer channel Example 1 — Partial Opacity (Official Docs) The docs highlight color.opacity on a translucent rgb color.
@use "sass:color";
@debug color.opacity(rgb(210, 225, 221, 0.4)); // 0.4
.mint {
--opacity: #{color.opacity(rgb(210, 225, 221, 0.4))};
} .mint {
--opacity: 0.4;
} How It Works RGBA color carries a fourth channel of 0.4.color.opacity reads that channel as a unitless number.Interpolation writes 0.4 into a custom property.Same result via color.alpha or color.channel(…, "alpha").Example 2 — Opaque Hex Returns 1 A normal hex color has full opacity — same sample the docs use with color.alpha.
@use "sass:color";
@debug color.opacity(#e1d7d2); // 1
.swatch {
--opacity: #{color.opacity(#e1d7d2)};
} .swatch {
--opacity: 1;
} How It Works Hex without alpha is treated as fully opaque.color.opacity reports 1.Identical to color.alpha(#e1d7d2).On 1.79+ , prefer color.channel(#e1d7d2, "alpha").Example 3 — Global opacity() Alias Without @use, the built-in global name still reads 0–1 for a color.
.panel {
--opacity: #{opacity(rgba(#036, 0.5))};
} .panel {
--opacity: 0.5;
} How It Works Global opacity() is listed beside color.alpha in the docs.Color argument yields the same 0–1 number.No module import is required for this legacy form.New code should still migrate to color.channel.Example 4 — Transparent, Mid, and Half Values Compare 0, 0.75, and 0.5 in one report block.
@use "sass:color";
.range {
--zero: #{color.opacity(rgba(3, 51, 102, 0))};
--mid: #{color.opacity(hsla(210, 100%, 20%, 0.75))};
--half: #{opacity(rgba(#036, 0.5))};
} .range {
--zero: 0;
--mid: 0.75;
--half: 0.5;
} How It Works Any legacy color with alpha reports a unitless 0…1 number.Fully transparent is 0.HSLA mid values keep their alpha channel.Module and global forms agree for color arguments.Example 5 — Prefer color.channel Same opacity read, modern API — drop the deprecated reader name.
@use "sass:color";
$brand: rgba(#336699, 0.8);
// Old: color.opacity($brand)
.report {
--opacity: #{color.channel($brand, "alpha")};
--opaque: #{if(color.channel($brand, "alpha") >= 1, "yes", "no")};
} .report {
--opacity: 0.8;
--opaque: no;
} How It Works Quote the channel name: "alpha".Result is still a plain number, so comparisons work.0.8 >= 1 is false → no.Requires Dart Sass 1.79+ for color.channel. Edge Cases & Pitfalls Most opacity-reader bugs are CSS confusion, writing vs reading, or modern color spaces.
css Sass opacity ≠ CSS opacity color.opacity($brand) answers how see-through a color token is. It does not emit opacity: 0.5; for an element.
read Readers do not change opacity color.opacity only returns a number. To fade a color, use color.adjust / color.change / color.scale.
ie IE alpha(opacity=…) is not opacity() That special filter string belongs to global alpha(). See the color.alpha page — opacity() does not use that form.
space Legacy colors for color.opacity Official docs require rgb / hsl / hwb. Prefer color.channel for modern spaces on Dart Sass 1.79+.
Toolchain
Sass Support color.opacity() remains for compatibility. New readers should use color.channel on Dart Sass 1.79+.
API Status color.opacity() / opacity()Deprecated reader — still works on legacy colors color.alpha()Same deprecated reader (alias) color.channel($c, "alpha")Preferred on Dart Sass 1.79+ LibSass / Ruby Sass Global opacity() only — no modern module path
Compile with the Dart Sass CLI .
Remember
Key Takeaways Reads opacity as a unitless number from 0 to 1.
Deprecated in favor of color.channel($c, "alpha").
Alias of color.alpha() for color arguments.
Not CSS opacity — compile-time color read only.
One line: use color.opacity to understand old code — write new opacity reads with color.channel.
Migrate
Migrate Notes Practical checklist:
1. Find color.opacity($c) / opacity($c) in legacy SCSS
2. Require Dart Sass 1.79+ for color.channel
3. Replace with color.channel($c, "alpha")
4. Do not confuse with CSS opacity on elements
5. Use adjust/change/scale when you need to write alpha Frequently Asked Questions What does Sass color.opacity() do? color.opacity($color) returns the alpha channel as a number between 0 and 1. Example: color.opacity(rgb(210, 225, 221, 0.4)) is 0.4; opaque hex like #e1d7d2 reports 1.
Is color.opacity() deprecated? Yes. Official Sass docs list opacity() with color.alpha() under Deprecated Functions because it is redundant with color.channel($color, "alpha").
Is opacity() the same as color.alpha()? Yes for a color argument. They are aliases that both return 0–1. Official samples use color.opacity() and color.alpha() interchangeably.
How is Sass opacity() different from CSS opacity? Sass color.opacity() reads one color’s alpha at compile time and returns a number. The CSS opacity property fades a whole element (and its children) in the browser.
Does color.opacity() work on oklch colors? Official docs require a legacy color space (rgb, hsl, or hwb). Convert first with color.to-space(), or read alpha with color.channel on Dart Sass 1.79+.
How do I migrate old color.opacity() calls? Replace color.opacity($color) with color.channel($color, "alpha") after @use "sass:color" on Dart Sass 1.79+. Keep opacity() only when maintaining older compilers or legacy SCSS.
Next: Sass color.blackness() Read HWB blackness as 0%–100% on legacy colors.
blackness() → 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