Color Basics: Names, Hex, rgb(), hsl(), hwb(), currentColor and Transparency
The classic ways to write CSS colors: named colors, hex codes with alpha, rgb(), hsl() and hwb() in the modern space-separated syntax, the legacy comma syntax, out-of-range values, transparent, currentColor and how it inherits, and alpha versus opacity.
kvi5hnuCode Sprout team member9 min readOn this page
Colors appear everywhere in CSS: text, backgrounds, borders, shadows, gradients, outlines, SVG fills. Every property that takes a <color> accepts the same notations. This article covers the classic ones, which describe colors in the sRGB color space that screens have used for decades. Article 32 covers the newer notations that reach wider gamuts.
Named colors
CSS has 148 named colors, from black and white to rebeccapurple, tomato, cornflowerblue and papayawhip. Names are case-insensitive.
.alert { color: firebrick; background: mistyrose; }
They're handy for prototypes and examples, but most designs need exact brand values, which names rarely match. Also beware of a few oddities inherited from old X11 color lists: darkgray is lighter than gray.
Hex colors
A hex color gives red, green and blue as two hexadecimal digits each, from 00 to ff:
| Form | Example | Meaning |
|---|---|---|
#rrggbb | #8a3b12 | red 8a (138), green 3b (59), blue 12 (18) |
#rgb | #c90 | each digit doubled: #cc9900 |
#rrggbbaa | #8a3b1280 | with alpha: 80 is about 50% opaque |
#rgba | #c908 | short form with alpha: #cc990088 |
Hex is compact and easy to copy between design tools, but hard to read: few people can picture #2f7d4f. The letters are case-insensitive.
rgb()
rgb() gives the same red, green and blue channels as numbers from 0 to 255, or as percentages:
.brand { color: rgb(138 59 18); }
.overlay { background: rgb(0 0 0 / 0.6); } /* 60% opaque black */
.tint { background: rgb(100% 95% 85% / 50%); }
- The modern syntax separates channels with spaces and puts the alpha after a slash. Alpha is a number from 0 to 1 or a percentage.
- In the modern syntax you can mix numbers and percentages, and use decimals:
rgb(255 66.7% 50.5). - Values out of range are clamped:
rgb(300 -20 128)becomesrgb(255 0 128). rgba()is now an alias ofrgb(): both accept an optional alpha.
The legacy comma syntax
Older CSS wrote rgb(138, 59, 18) and rgba(0, 0, 0, 0.6). That still works in rgb() and hsl(), but it has stricter rules: all channels must be numbers or all percentages, and there's no none. The newer color functions only accept the space syntax, so use it everywhere for consistency.
hsl()
hsl() describes a color by hue, saturation and lightness, which is closer to how people think about color:

- Hue is an angle around the color wheel: red at 0, yellow at 60, green at 120, cyan at 180, blue at 240, magenta at 300. It can be written as a plain number or with an angle unit (
25,25deg,0.07turn). Values beyond 360 wrap around. - Saturation goes from
0%, a gray, to100%, the most vivid color. - Lightness goes from
0%, black, to100%, white.50%gives the purest color.
:root {
--accent: hsl(25 77% 31%);
--accent-light: hsl(25 77% 90%); /* same hue, much lighter */
--accent-muted: hsl(25 20% 45%); /* same hue, grayer */
}
Changing one channel at a time makes hsl() good for building palettes and hover states. Its weakness is that lightness isn't perceptual: hsl(60 100% 50%) (yellow) looks far brighter than hsl(240 100% 50%) (blue), although both have 50% lightness. That's why newer palettes often use oklch() instead (article 32).
hwb()
hwb() uses a hue, then how much whiteness and blackness to mix in:
.pale { color: hwb(25 60% 10%); }
.deep { color: hwb(25 0% 45%); }
Low whiteness and blackness give vivid colors. When whiteness and blackness add up to 100% or more, the result is a gray. hwb() is intuitive for tints and shades, though less common than hsl(). It has never had a comma syntax.
Missing components: none
In the modern syntax, any channel can be none, meaning "no value". It behaves like 0 when the color is displayed, but it matters when colors are interpolated in a space that has that channel: a none channel takes the value from the other color. In a gradient or color-mix() written in hsl, hsl(none 0% 50%) fades to blue without passing through other hues. Hex, rgb() and hsl() colors interpolate in sRGB by default, which has no hue channel, so a missing hue makes no difference there. Article 51 shows how this matters for gradients.
Transparency
Every notation accepts an alpha value, the opacity of the color itself:
.scrim { background: rgb(0 0 0 / 50%); }
.highlight { background: hsl(45 90% 60% / 0.35); }
.glass { background: #ffffff99; }
transparent is a fully transparent black, rgb(0 0 0 / 0). It's the initial value of background-color.
Alpha compared with opacity
A color's alpha only affects that color. The opacity property makes the whole element transparent: its text, children, borders and images.
body { font-family: system-ui, sans-serif; display: flex; gap: 1rem; padding: 1rem;
background: repeating-linear-gradient(45deg, #f4c95d 0 12px, #fff 12px 24px); }
.card { padding: 1rem; border-radius: 8px; color: white; font-weight: 700; width: 14rem; }
.alpha { background: rgb(138 59 18 / 0.5); }
.opacity { background: rgb(138 59 18); opacity: 0.5; }

Look at the badges: alpha only thinned the card's background, while opacity made everything inside the card see-through, and a child can't opt out by setting opacity: 1, because the parent and its contents are faded as one group. To fade only the background, use alpha on the background color.
Use alpha for see-through backgrounds and overlays. Use opacity to fade a whole element, for example in a transition. opacity also creates a stacking context (article 58).
currentColor
The keyword currentColor means "this element's color value". It lets other properties follow the text color:
.tag { color: #2f7d4f; border: 1px solid currentColor; } /* the border matches the text */
.icon { fill: currentColor; } /* SVG icons follow the link color */
.button { color: white; background: #8a3b12; }
.button:hover { color: #f4c95d; } /* the icon inside changes too */

One declaration, color, recolors four other properties, so a variant needs only one line.
Several properties already default to it: border-color, outline-color, text-decoration-color, caret-color and column-rule-color all use the element's text color unless set.
currentColor stays a keyword when it inherits. If an element inherits border-color: currentColor, it uses its own text color, not its parent's. The same is true when it's stored in a custom property: --line: currentColor resolves to the text color of each element that uses var(--line). On the color property itself, color: currentColor is the same as color: inherit.
The color property
color sets the text color, and it inherits, so setting it on body colors all text that has no other color. Links and form controls are exceptions: they get their colors from the browser's default stylesheet. Buttons, inputs and selects don't inherit the page's text color and font unless you tell them to:
button, input, select, textarea { color: inherit; font: inherit; }
When you set a text color, also set the background it sits on. If you only set one, a user's browser settings, a dark mode extension or a forced colors mode (article 35) may give you light gray text on a white page.
Which notation to use
| Notation | Good for |
|---|---|
| hex | values copied from design tools, compact tokens |
rgb() | values from code or image data, alpha overlays |
hsl() | quick palettes and variations in sRGB |
oklch() (article 32) | perceptually even palettes, wide-gamut colors |
| named colors | examples and prototypes |
Most projects store their palette as custom properties (article 27) in one notation and refer to them everywhere else. Article 33 shows how to derive tints, shades and transparent versions from one base color with color-mix() and relative colors.
What's current
- The space-separated syntax, the slash for alpha,
none, and mixing numbers with percentages are Baseline. - Eight-digit and four-digit hex colors are Baseline.
- Wide-gamut notations and color spaces are covered in article 32, and
light-dark()in article 34.
Common questions
What's the difference between rgb() and rgba()?
None today. rgba() is an alias: both take an optional alpha. Use rgb() with a slash: rgb(0 0 0 / 0.5).
Why is darkgray lighter than gray?
The names come from two different historical lists. gray is #808080, from HTML, while darkgray (#a9a9a9) came from the X11 color list, where gray was lighter. green, maroon and purple have the same clash: the HTML values won.
Why does my child element's text fade when I set opacity on the parent?
opacity applies to the element and everything inside it, and children can't undo it. Use an alpha color on the parent's background instead.
Should I write hex or hsl()?
Either works for sRGB colors. hsl() is easier to adjust by hand. For building palettes, oklch() gives more even results.
Why doesn't my button use the page's font and text color?
Form controls get their own font and color from the browser's default styles. Add font: inherit; color: inherit; to them.
Summary
- CSS has 148 named colors. They're case-insensitive, and some, like
darkgray, are misleading. - Hex colors use
#rrggbb,#rgband alpha versions#rrggbbaaand#rgba. rgb(),hsl()andhwb()use spaces between channels and a slash before the alpha. The comma syntax is legacy.hsl()uses hue, saturation and lightness. Its lightness isn't perceptually even across hues.- Out-of-range channels are clamped, and hues wrap around.
nonemarks a missing channel. - Alpha makes one color transparent, while
opacityfades the whole element. currentColoris the element's text color. It inherits as a keyword, so each element uses its own color.
Practice questions
Write #cc9900 in the short hex form, then as rgb() with 50% opacity.
Answer
#c90, and rgb(204 153 0 / 50%). With hex alpha, the same color is #cc990080 or, approximately, #c908.
A card has the rule below, and its text looks faded. How do you make only the background see-through?
.card { background: white; opacity: 0.8; }
Answer
Remove opacity and use an alpha color: background: rgb(255 255 255 / 0.8);. Alpha affects only that color, not the text.
What border color does the <span> get, and why?
<style>
div { color: navy; border: 2px solid currentColor; }
span { color: crimson; border: inherit; }
</style>
<div><span>Hi</span></div>
Answer
Crimson. The inherited value is the keyword currentColor, not the parent's navy, so it resolves to the <span>'s own color.
Using hsl(), write a base color and a lighter and a grayer variant of the same hue.
Answer
For example hsl(145 45% 34%), hsl(145 45% 88%) and hsl(145 12% 40%). Keep the hue and change the lightness or the saturation.
Previous: Dynamic Values: attr(), if(), Custom Functions and sibling-index()
Next: Color Spaces and Gamuts: lab(), lch(), oklab(), oklch() and color()

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.