Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › CSS Snippets › Reference › color

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)#3bfShorthand for doubled values
Hex (6)#3b82f6Most common. Copy-paste from design tools.
Hex (8)#3b82f680With alpha (transparency)
RGBrgb(59 130 246)Programmatic color manipulation
RGBArgb(59 130 246 / 0.5)RGB with transparency
HSLhsl(217 91% 60%)Adjusting hue/saturation/lightness
HSLAhsl(217 91% 60% / 0.5)HSL with transparency
OKLCHoklch(0.65 0.2 260)Perceptually uniform color
NamedtomatoQuick prototypes. 140+ names available.
1

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.

2

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).

3

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.

4

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:17:1
Large text (18pt+ or bold 14pt+)3:14.5:1
UI components, icons, borders3:1—

💡 Test tool: WebAIM's contrast checker (webaim.org/resources/contrastchecker) lets you verify any color pair instantly.

5

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
transparentrgba(0,0,0,0)

⚠️ Common Mistakes

♿ Accessibility Notes

❓ 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.

🎯 What's Next?

← All CSS Reference