CSS Reference
color
Sets the foreground color of text. The most-used visual property on the web.
Last updated: October 2026
Definition: The color property sets the foreground color of an element's text and text decorations. It accepts hex, RGB, RGBA, HSL, HSLA, OKLCH, and 140+ named colors. It is inherited by child elements.
🎬 Live Example
The same text, styled with different color formats.
Hex: #3b82f6 — blue-500
RGB: rgb(59, 130, 246)
HSL: hsl(217, 91%, 60%)
OKLCH: oklch(0.65 0.2 260)
Named: tomato
currentColor (matches text)
💡 All five formats produce similar blue. OKLCH is newer but widely supported.
Basic Syntax
p {
color: #3b82f6;
}
.heading {
color: rgb(255, 100, 50);
}
.accent {
color: oklch(0.7 0.15 200);
}
Inherits to children. Change once on a parent, and all nested text updates.
🎯 Color Formats Cheatsheet
| Format | Example | When to use |
|---|---|---|
| Hex (3) | #3bf | Shorthand for doubled values |
| Hex (6) | #3b82f6 | Most common. Copy-paste from design tools. |
| Hex (8) | #3b82f680 | With alpha (transparency) |
| RGB | rgb(59 130 246) | Programmatic color manipulation |
| RGBA | rgb(59 130 246 / 0.5) | RGB with transparency |
| HSL | hsl(217 91% 60%) | Adjusting hue/saturation/lightness |
| HSLA | hsl(217 91% 60% / 0.5) | HSL with transparency |
| OKLCH | oklch(0.65 0.2 260) | Perceptually uniform color |
| Named | tomato | Quick prototypes. 140+ names available. |
color vs background-color
Two properties that always pair together.
.badge {
color: #ffffff; /* text color */
background-color: #3b82f6; /* background fill */
padding: 0.25rem 0.75rem;
border-radius: 0.25rem;
}
color sets the foreground. background-color sets the background. You almost always set both together to guarantee readability.
Transparency with Alpha
Modern syntax uses a slash to separate the alpha channel.
/* Modern RGB with alpha */
.text {
color: rgb(59 130 246 / 0.5);
}
/* HSL with alpha */
.text {
color: hsl(217 91% 60% / 0.5);
}
/* 8-digit hex */
.text {
color: #3b82f680;
}
Alpha values range from 0 (fully transparent) to 1 (fully opaque).
currentColor — Auto-Match Text
currentColor resolves to the value of the element's own color property. Perfect for icons and borders that should match text.
.button {
color: #3b82f6;
border: 2px solid currentColor; /* matches text */
}
.icon {
fill: currentColor; /* SVG fills with text color */
}
.link:hover {
color: #ef4444;
border-bottom: 1px solid currentColor; /* updates with color */
}
This is how Tailwind's icon libraries work — the icon inherits the surrounding text color.
Color Contrast — WCAG Rules
Contrast ratio is the difference in luminance between text and background. WCAG defines minimum ratios.
| Text Type | WCAG AA | WCAG AAA |
|---|---|---|
| Normal text (under 18pt) | 4.5:1 | 7:1 |
| Large text (18pt+ or bold 14pt+) | 3:1 | 4.5:1 |
| UI components, icons, borders | 3:1 | — |
💡 Test tool: WebAIM's contrast checker (webaim.org/resources/contrastchecker) lets you verify any color pair instantly.
CSS Variables for Color Systems
The right way to manage a color palette across a site.
:root {
--color-primary: #3b82f6;
--color-success: #10b981;
--color-danger: #ef4444;
--color-text: #e2e8f0;
--color-text-muted: #94a3b8;
--color-bg: #020617;
}
.button {
color: var(--color-primary);
border: 1px solid var(--color-primary);
}
.error {
color: var(--color-danger);
}
.muted {
color: var(--color-text-muted);
}
Change one variable in :root and the entire site updates. This is how design systems work.
🎯 Common Named Colors
| Name | Hex | Preview |
|---|---|---|
| black | #000000 | |
| white | #ffffff | |
| red | #ff0000 | |
| tomato | #ff6347 | |
| dodgerblue | #1e90ff | |
| transparent | rgba(0,0,0,0) |
⚠️ Common Mistakes
- Low contrast text. Gray text on light backgrounds fails WCAG. Verify with a contrast checker.
- Hardcoding colors everywhere. Use CSS variables so the palette can be updated in one place.
- Relying only on color. Never use color alone to convey meaning — add icons, labels, or text.
- Forgetting background-color with color. Setting text color without checking the background can produce unreadable text.
- Using
color: rgba()syntax. Modern syntax usesrgb(... / alpha). Both work, but the modern form is preferred. - Missing hex digits.
#3b82f(5 digits) is invalid. Use 3, 4, 6, or 8 digits. - Inheriting color into invisible text. Placeholder text, disabled buttons, and empty states all need explicit color handling.
♿ Accessibility Notes
- Contrast ratio is mandatory. 4.5:1 for normal text, 3:1 for large text (WCAG AA).
- Never rely on color alone. Colorblind users see 1 in 12 men. Always pair color with a symbol, text, or pattern.
- Focus states must be visible. Yellow-on-yellow focus rings are invisible. Test with keyboard navigation.
- Dark mode needs its own palette. Colors that work on white backgrounds often fail on black.
- Test with a color blindness simulator. Chrome DevTools and Figma both have built-in simulators.
- Avoid pure red and pure green for critical UI. They are indistinguishable to red-green colorblind users.
❓ Frequently Asked Questions
What is the CSS color property?
The CSS color property sets the foreground color of text and text decorations. It accepts many formats: hex codes, RGB, RGBA, HSL, HSLA, OKLCH, and 140+ named colors. It is one of the most-used CSS properties.
What is the difference between color and background-color?
color sets the foreground text color. background-color sets the background fill. They are separate properties and often used together to ensure text is readable against its background.
What is the best color format to use?
Modern CSS supports hex (#3b82f6), RGB, HSL, and OKLCH. HSL and OKLCH are easier to work with when you need to adjust lightness or saturation. Hex is the most compact. Pick the format that fits your workflow.
What is currentColor in CSS?
currentColor is a keyword that resolves to the value of the color property. It is used on border-color, background-color, and SVG fill/stroke to make them automatically match the current text color.
How do I check color contrast?
Use the Web Content Accessibility Guidelines (WCAG) contrast ratio. Normal text needs at least 4.5:1 contrast. Large text (18pt+) needs at least 3:1. Tools like WebAIM's contrast checker help you verify this instantly.