CSS Colors
Color is one of the most powerful tools in web design. CSS gives you six distinct color formats, full alpha transparency control, and a simple path to building dark-mode ready theming systems — all without any design software.
opacity property vs alpha
channelCSS Color Properties
Before exploring color formats, you need to know which CSS properties accept a color value. Any of the six formats covered in this chapter work in all of them.
| Property | What it colors | Example |
|---|---|---|
| color | Text (foreground) color | color: #264de4; |
| background-color | Element background fill | background-color: #f0f4ff; |
| border-color | Border line color | border-color: rgba(38,77,228,.3); |
| outline-color | Outline ring color (focus state) | outline-color: #264de4; |
| text-decoration-color | Underline / strikethrough color | text-decoration-color: red; |
| box-shadow | Shadow color (4th value) | box-shadow: 0 4px 16px rgba(0,0,0,.1); |
| caret-color | Input text cursor color | caret-color: #264de4; |
| fill / stroke | SVG element fill and outline | fill: #264de4; |
All CSS Color Formats
CSS supports six main color formats. All six produce valid colors; the best choice depends on your use case.
Named Colors
CSS defines 147 named color keywords. You can use them directly by
name — no codes needed. They are case-insensitive: Red, red and RED all work.
h1 { color: tomato; }
p { color: dimgray; }
.badge { background-color: gold; color: black; }
.danger { background-color: crimson; color: white; }
.overlay { background-color: transparent; }
/* Special keyword — inherits the element's own color value */
.icon {
color: #264de4;
border: 1px solid currentColor; /* border uses the same blue */
}
currentColor is a special keyword that references the element's own
color value. It lets borders, shadows and SVG fills automatically match
the text color — great for icons and consistent component styling.
HEX Colors
HEX (hexadecimal) is the most widely used CSS color format. A HEX
color code starts with # followed by six characters — two each for Red,
Green and Blue channels, in base-16 (0–9 + A–F).
Each pair is a hexadecimal number from 00 (0 decimal = no channel) to FF (255
decimal = full channel).
/* 6-digit HEX — most common */
h1 { color: #264de4; } /* CSS blue */
p { color: #52524a; } /* dark grey */
/* 3-digit shorthand — only when both digits are identical */
/* #rgb is shorthand for #rrggbb */
.white { color: #fff; } /* same as #ffffff */
.black { color: #000; } /* same as #000000 */
.red { color: #f00; } /* same as #ff0000 */
/* 8-digit HEX — includes alpha channel (CSS4) */
.overlay { background: #264de480; } /* 80 hex = 50% opacity */
/* Common palette examples */
:root {
--brand: #264de4;
--success: #22c55e;
--warning: #f59e0b;
--danger: #ef4444;
--dark: #0f172a;
--light: #f8fafc;
}
#264DE4, #264de4 and #264De4 are all identical. Most developers prefer lowercase for consistency.
Design tools like Figma export uppercase — both are valid.
RGB & RGBA
RGB defines a color by specifying its Red, Green and Blue channel intensities as integers from 0 (no light) to 255 (full intensity). RGBA adds a fourth Alpha channel for transparency.
Above: channel breakdown of
rgb(38, 77, 228) — the CSS blue used throughout this course.
/* RGB — channels 0–255 */
h1 { color: rgb(38, 77, 228); } /* CSS blue */
p { color: rgb(82, 82, 74); } /* dark grey */
.red { color: rgb(255, 0, 0); } /* pure red */
.white{ color: rgb(255, 255, 255); } /* white */
.black{ color: rgb(0, 0, 0); } /* black */
/* RGBA — 4th value is alpha: 0 (transparent) → 1 (opaque) */
.overlay { background: rgba(0, 0, 0, 0.5); } /* 50% black */
.card-glow { box-shadow: 0 8px 24px rgba(38, 77, 228, 0.3); }
.badge { background: rgba(38, 77, 228, 0.12); } /* tinted bg */
.transparent{ background: rgba(255, 255, 255, 0); } /* invisible */
/* Modern space-separated syntax (CSS4, all browsers 2022+) */
.modern { color: rgb(38 77 228 / 80%); } /* commas optional */
Alpha channel values from 0 → 1 on the brand blue:
HSL & HSLA
HSL stands for Hue, Saturation and Lightness. Unlike RGB which mixes light channels, HSL maps to how humans actually perceive color — making it the most intuitive format for creating color palettes and variations.
/* HSL — intuitive color variations */
.brand { color: hsl(228, 72%, 52%); } /* CSS blue */
.green { color: hsl(142, 71%, 45%); } /* success green */
.yellow { color: hsl(38, 92%, 50%); } /* warning amber */
.grey { color: hsl(228, 5%, 50%); } /* desaturated = grey */
.black { color: hsl(0, 0%, 0%); } /* L=0 = black */
.white { color: hsl(0, 0%, 100%); } /* L=100% = white */
/* Easy color scale — just change the L value */
.blue-100 { background: hsl(228, 72%, 95%); } /* very light */
.blue-300 { background: hsl(228, 72%, 75%); } /* light */
.blue-500 { background: hsl(228, 72%, 52%); } /* base */
.blue-700 { background: hsl(228, 72%, 35%); } /* dark */
.blue-900 { background: hsl(228, 72%, 18%); } /* very dark */
/* HSLA — with alpha transparency */
.modal-bg { background: hsla(228, 72%, 10%, 0.85); }
.badge-tint{ background: hsla(228, 72%, 52%, 0.12); }
Hue value (0° → 360°) at full saturation and 50% lightness:
hsl(228, 72%, 95%) to hsl(228, 72%, 10%) gives you a complete blue scale from near-white to
near-black, all perfectly on-brand.
The opacity Property vs Alpha Channel
There are two ways to make an element semi-transparent in CSS. They look similar but behave very differently:
/* opacity — fades the WHOLE element including children */
.modal-overlay {
background: #000;
opacity: 0.5; /* text inside will also be 50% visible — bad! */
}
/* rgba — fades ONLY the background, text stays fully opaque */
.modal-overlay {
background: rgba(0, 0, 0, 0.5); /* text inside is still 100% visible — correct */
}
/* opacity is useful for disabling/fading an entire component */
.btn:disabled {
opacity: 0.4;
cursor: not-allowed;
}
/* opacity 0 = invisible but still takes up space */
/* visibility: hidden = same as opacity 0 */
/* display: none = invisible AND removed from layout */
opacity: 0.5 on a card or modal hoping only the background becomes
transparent — but all the text, buttons and images inside also fade. Use rgba() or hsla() on the background-color property instead.
CSS Variables for a Color System
The most powerful pattern for managing colors in a real project is to define all
colors as CSS Custom Properties (variables) on the :root element. This gives you a single source of truth for your entire
palette — and makes dark mode trivially easy.
/* ── Light theme (default) ── */
:root {
--color-brand: #264de4;
--color-text: #1a1a14;
--color-text-2: #52524a;
--color-bg: #ffffff;
--color-bg-2: #f8fafc;
--color-border: #e5e7eb;
--color-success: #22c55e;
--color-warning: #f59e0b;
--color-danger: #ef4444;
}
/* ── Dark theme — just override the same variables ── */
[data-theme="dark"] {
--color-brand: #4a8af4;
--color-text: #f0f0e8;
--color-text-2: #a8a89c;
--color-bg: #0f172a;
--color-bg-2: #1e293b;
--color-border: #334155;
}
/* ── Components reference variables, not raw values ── */
body { background: var(--color-bg); color: var(--color-text); }
a { color: var(--color-brand); }
.card { background: var(--color-bg-2); border: 1px solid var(--color-border); }
document.documentElement.setAttribute('data-theme', 'dark'); — this
single JS line switches every color on the page because all components use CSS variables, not hard-coded
values.
Modern CSS Color Formats
CSS Color Level 4 (2022+) introduced newer formats with wider color gamuts and more predictable perceptual results. They are supported in all modern browsers.
/* oklch() — perceptually uniform, widest gamut, recommended for 2025+ */
.brand { color: oklch(55% 0.2 264); } /* L% · C · H° */
.green { color: oklch(65% 0.18 142); }
/* color-mix() — mix two colors by percentage */
.mixed { color: color-mix(in srgb, #264de4 70%, white); }
.dimmed { color: color-mix(in srgb, currentColor 40%, transparent); }
/* light-dark() — automatic light/dark switching (CSS4) */
body {
color-scheme: light dark;
background: light-dark(#ffffff, #0f172a);
color: light-dark(#1a1a14, #f0f0e8);
}
Try It Yourself
The editor below shows all six color formats used together. Try changing the --brand variable value and watch every component update at once.
Quick Check — Chapter 5
tomato) — 147 keywords, great
for quick prototyping or well-known colors.HEX (e.g.
#264de4) — most commonly used, direct
output from design tools like Figma, compact notation.RGB (e.g.
rgb(38,77,228)) — explicit channel
control, easy to adjust programmatically.RGBA (e.g.
rgba(38,77,228,0.5)) — best for
transparent overlays, tinted backgrounds, shadows. Children stay fully opaque.HSL (e.g.
hsl(228,72%,52%)) — best for design
systems and color scales. Change only L to get a complete shade range.HSLA (e.g.
hsla(228,72%,52%,0.12)) — HSL with
transparency, ideal for theming systems.
#RR GG BB.#264de4 means: Red = 26 hex (38
decimal), Green = 4d hex (77 decimal), Blue = e4 hex (228 decimal).Each pair goes from
00 (0, no channel) to ff (255, full channel). A higher blue value means more blue — which
gives this particular code its vivid blue appearance.The 3-digit shorthand (
#fff) works only when both digits of each
pair are identical — #fff expands to #ffffff.
opacity: 0.5 applies transparency to the entire
element and all its children. Text, images and child elements all become 50% transparent — you
cannot override this on a child element.rgba(0,0,0,0.5) applies transparency only to that
specific color value. If used on a card's background-color,
the text and other content inside the card remain fully opaque.Use
rgba() / hsla() for overlays
and tinted backgrounds. Use opacity for fading entire components
like a disabled button.
With HSL, you keep the Hue (H) and Saturation (S) constant and simply increase or decrease the Lightness (L) value. For example,
hsl(228, 72%, 95%) through hsl(228, 72%, 10%) gives you a perfect blue scale — all perfectly
on-brand with no manual calculation.
:root for the light
theme. Then in a [data-theme="dark"] selector, redefine the same
variable names with dark theme values.Every component that uses
var(--color-bg) instead of a hard-coded
#ffffff will automatically update when the theme switches — because
the variable's value changes but the component code stays identical.Switching dark mode then becomes one line of JavaScript:
document.documentElement.setAttribute('data-theme', 'dark');
#ffcc00?
opacity: 0.5 affect?color value?- CSS supports six color formats: Named colors, HEX, RGB, RGBA, HSL and HSLA. All work in any property that accepts a color value.
- Named colors — 147 keywords including
transparentandcurrentColor. Good for quick work, limited palette. - HEX
#RRGGBB— most widely used. Compact, direct from design tools. 3-digit shorthand when pairs repeat (#fff). - RGBA
rgba(r,g,b,a)— best for transparent overlays, tinted backgrounds and shadows. Children stay fully opaque unlikeopacity. - HSL
hsl(H,S%,L%)— most intuitive for design systems. Vary only L to generate a complete color scale. opacityfades the whole element including children. RGBA/HSLA alpha fades only that color value — children stay opaque.- CSS variables on
:rootare the best practice for color systems. Redefine the same variables in[data-theme="dark"]for one-switch dark mode.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.