Sass rgb()

Beginner
12 min read
Updated: Nov 2026
5 examples
Global constructor

Definition and Usage

rgb() is a global Sass color constructor. It builds an RGB color from red, green, and blue channels—with or without alpha—or sets a new alpha on an existing color with rgb($color, $alpha).

Official Sass docs list it under Global Functions (CSS-compatible). It is not deprecated. Alias: rgba() (same signatures in modern Dart Sass).

Official samples.

rgb(0 51 102) → #036. rgb(95%, 92.5%, 89.5%) → #f2ece4. rgb(0 51 102 / 50%) → rgba(0, 51, 102, 0.5). rgb(#f2ece4, 50%) → rgba(242, 236, 228, 0.5).

No @use required.

rgb() stays global so it can extend CSS rgb(). For module-style alpha writes, prefer color.change($c, $alpha: …).

Syntax

SCSS
// Classic channels (alpha optional)
rgb($red, $green, $blue, $alpha: 1)
rgba($red, $green, $blue, $alpha)

// CSS Color Level 4 (Dart Sass 1.15+)
rgb($red $green $blue)
rgb($red $green $blue / $alpha)

// Set alpha on an existing color
rgb($color, $alpha)
rgba($color, $alpha)

Channels: unitless 0–255 or percentages 0%–100%. Alpha: unitless 0–1 or a percentage.

Forms & Aliases

Pick a signature by what you already have:

NeedPrefer
Opaque RGB from numbersrgb(0 51 102) or rgb(0, 51, 102)
RGB + alpha (literals)rgb(0 51 102 / 50%) or comma form
Channels are variablesrgb($r, $g, $b, $a) — avoid slash form
Keep channels, set opacityrgb($brand, 0.5) or color.change($brand, $alpha: 0.5)
Same API, historical namergba(…) — alias in Dart Sass
Slash + variables.

Official docs warn that Sass slash rules make rgb($r $g $b / $alpha) awkward when $b or $alpha is a variable. Use commas instead.

SCSS Examples

Examples use global rgb() / rgba(). Open View Compiled CSS for verified output (matches official Sass docs samples where noted).

Example 1 — Space-Separated Channels (Official Docs)

Level 4 syntax builds an opaque navy from three numbers.

SCSS
@debug rgb(0 51 102); // #036

.navy {
  color: rgb(0 51 102);
}

How It Works

  1. Three channels are space-separated (no commas).
  2. Sass evaluates them at compile time into an RGB color.
  3. Legacy emission often shortens to hex #036.
  4. Same color as rgb(0, 51, 102).

Example 2 — Percentage Channels (Official Docs)

Channels can be percentages instead of 0–255.

SCSS
@debug rgb(95%, 92.5%, 89.5%); // #f2ece4

.cream {
  background: rgb(95%, 92.5%, 89.5%);
}

How It Works

  1. Percentages map onto the 0–255 RGB scale.
  2. Comma form works the same as space form for opaque colors.
  3. Result matches the official sample hex #f2ece4.
  4. Useful when design tokens are already percentages.

Example 3 — Slash Alpha (Official Docs)

CSS Color Level 4 puts alpha after a slash.

SCSS
@debug rgb(0 51 102 / 50%); // rgba(0, 51, 102, 0.5)

.panel {
  background: rgb(0 51 102 / 50%);
}

How It Works

  1. Spaces separate R, G, and B.
  2. Slash introduces the alpha channel.
  3. 50% means half-opaque (0.5).
  4. Compiled CSS uses the classic rgba() form for broad support.

Example 4 — rgb($color, $alpha) (Official Docs)

Keep the brand channels; only replace opacity.

SCSS
@debug rgb(#f2ece4, 50%); // rgba(242, 236, 228, 0.5)
@debug rgba(rgba(0, 51, 102, 0.5), 1); // #003366

.overlay {
  background: rgb(#f2ece4, 50%);
}

.solid {
  color: rgba(rgba(0, 51, 102, 0.5), 1);
}

How It Works

  1. Two-argument form takes a color plus a new alpha.
  2. RGB channels stay the same; only opacity changes.
  3. Alpha 1 restores a fully opaque navy.
  4. Module twin: color.change($c, $alpha: 0.5).

Example 5 — Prefer Commas with Variables

When channels live in variables, use the comma signature.

SCSS
$r: 0;
$g: 51;
$b: 102;
$a: 0.75;

.card {
  // Prefer commas when any channel is a variable
  background: rgb($r, $g, $b, $a);
  border-color: rgb($r, $g, $b);
}

How It Works

  1. Variables hold each channel (and alpha).
  2. Comma form avoids slash-as-division ambiguity.
  3. With alpha, Sass emits rgba(…).
  4. Without alpha, opaque navy can emit as hex.

Edge Cases & Pitfalls

Most rgb() bugs are slash + variables, old LibSass name rules, or confusing constructors with readers.

slash

Avoid slash form with variables

Use rgb($r, $g, $b, $a). The Level 4 slash form is fine for literals.

libsass

Older compilers split rgb / rgba

LibSass still expects alpha only on rgba() and forbids it on rgb(). Dart Sass treats them as aliases.

var

var() stays for the browser

rgb(0 51 102 / var(--opacity)) returns an unquoted string so CSS variables resolve at runtime.

read

rgb() builds; color.red() reads

Do not confuse the constructor with deprecated channel readers like color.red().

Sass Support

rgb() / rgba() are current global constructors. Level 4 space/slash forms need Dart Sass 1.15+.

APIStatus
rgb() / rgba() (Dart Sass)Current aliases — all listed signatures
Space / slash syntaxDart Sass 1.15+
LibSass / Ruby SassStricter: rgb without alpha, rgba with alpha (plus rgba($color, $alpha))
color.change($c, $alpha: …)Module alternative for alpha writes

Compile with the Dart Sass CLI.

Key Takeaways

  • Global constructor — builds RGB colors at compile time.
  • Not deprecated — listed under Global Functions.
  • Alias: rgba() in modern Dart Sass.
  • Use commas when channels are variables; slash is for literals.

One line: use rgb() to build (or re-alpha) RGB tokens — prefer commas with variables, and color.change when you already live in sass:color.

Compile Notes

Practical checklist:

checklist.txt
1. Build opaque colors with rgb($r, $g, $b) or Level 4 spaces
2. Add alpha with commas or rgb(... / $alpha) for literals
3. Prefer rgb($r, $g, $b, $a) whenever variables are involved
4. Use rgb($color, $alpha) to keep channels and set opacity
5. Or color.change($color, $alpha: ...) inside @use "sass:color"

Frequently Asked Questions

rgb() builds an RGB color from red, green, and blue channels, optionally with alpha. Example: rgb(0 51 102) is #036; rgb(#f2ece4, 50%) is rgba(242, 236, 228, 0.5).
In modern Dart Sass, yes—they are aliases and accept the same signatures. Historically rgba required alpha and rgb forbade it; LibSass still follows those stricter name rules.
No. Official Sass docs list rgb()/rgba() under Global Functions as CSS-compatible color constructors. They are not in the Deprecated Functions list.
When you already use @use "sass:color" and only need to set alpha (or other channels) on a token. color.change(#6b717f, $alpha: 0.5) matches rgb(#6b717f, 0.5).
Sass slash-parsing rules make rgb($r $g $b / $alpha) awkward when $b or $alpha is a variable. Official docs recommend rgb($r, $g, $b, $alpha) instead.
If you pass special CSS functions like var() for a channel, Sass returns an unquoted string with the same signature so the browser can resolve it at runtime.

Next: Sass rgba()

Same global constructor under the historical rgba name.

rgba() →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

8 people found this page helpful