Colors in CSS

Colors in CSS control the visual identity, readability, hierarchy, and mood of a web page. CSS can apply color to text, backgrounds, borders, outlines, shadows, gradients, SVG fills, and many other visual parts of a design. A good color system makes a page easier to read and easier to use. A poor color system can make even correct content difficult to understand.

CSS supports several ways to write colors. Beginners usually start with color names and hex codes, then move to RGB, RGBA, HSL, alpha transparency, and custom properties. Each format has a practical use. The best format depends on whether you need readability, design precision, transparency, or easy theme management.

Where Colors Are Used in CSS

Color values appear in many CSS properties. The most obvious one is color, which changes text color. But colors are also used for backgrounds, borders, outlines, shadows, gradients, text decoration, and form states.

body {
  color: #222222;
  background-color: #ffffff;
}

.button {
  background-color: #111160;
  color: white;
  border: 1px solid #111160;
}

In this example, the page text is dark, the page background is white, and the button uses a navy background with white text. The button border uses the same navy color to keep the component visually consistent.

CSS Color Names

CSS has built-in color names such as red, blue, black, white, gray, navy, crimson, and transparent. Color names are easy to read and useful for quick examples.

h1 {
  color: navy;
}

.warning {
  background-color: yellow;
}

Color names are convenient, but they are not ideal for precise branding. A design system usually needs exact colors. For that, hex, RGB, or HSL values are more reliable.

Hex Colors in CSS

Hex color values start with a hash symbol and use hexadecimal digits. A common hex color has six characters: two for red, two for green, and two for blue.

.title {
  color: #111160;
}

.box {
  background-color: #f5f5f5;
}

Hex is popular because it is compact and widely used in design tools. For example, #000000 is black, #ffffff is white, and #ff0000 is red. Some hex colors can be shortened. #ffffff can be written as #fff, and #000000 can be written as #000.

FormatExampleMeaning
Named colorblueReadable built-in color keyword
Hex#111160Compact red-green-blue value
RGBrgb(17, 17, 96)Red, green, and blue channels
RGBArgba(17, 17, 96, 0.8)RGB with alpha transparency
HSLhsl(240, 70%, 22%)Hue, saturation, and lightness

RGB and RGBA Colors

RGB colors describe red, green, and blue channels. Each channel usually ranges from 0 to 255. Higher values add more of that channel.

.text {
  color: rgb(34, 34, 34);
}

.overlay {
  background-color: rgba(0, 0, 0, 0.5);
}

The rgba() format adds alpha transparency. The alpha value ranges from 0 to 1, where 0 is fully transparent and 1 is fully opaque. A value like 0.5 creates 50 percent opacity for that color.

Modern CSS also supports space-separated RGB syntax with optional alpha. You may see values such as rgb(0 0 0 / 50%). Older comma-based syntax is still common and easy for beginners to recognize.

HSL and HSLA Colors

HSL stands for hue, saturation, and lightness. Hue represents the color angle, saturation controls intensity, and lightness controls how dark or light the color is. Many designers find HSL easier for adjusting variations of a color.

.badge {
  background-color: hsl(210, 90%, 45%);
  color: white;
}

.badge-light {
  background-color: hsl(210, 90%, 92%);
  color: hsl(210, 90%, 25%);
}

In this example, both colors share the same hue and saturation, but the lightness changes. This makes it easier to create related dark and light versions of the same color family.

Alpha Transparency

Alpha controls transparency. It is useful for overlays, shadows, disabled states, layered backgrounds, and subtle UI effects. Transparency should be used carefully because the final visible color depends on what is behind it.

.modal-backdrop {
  background-color: rgba(0, 0, 0, 0.65);
}

.disabled {
  color: rgba(0, 0, 0, 0.4);
}

Transparent colors can reduce contrast. Always check readability when placing transparent text or overlays on variable backgrounds.

The transparent Keyword

The transparent keyword represents a fully transparent color. It is useful when a border, background, or outline needs to exist in layout or transition but not be visible at first.

.card {
  border: 2px solid transparent;
}

.card:hover {
  border-color: #111160;
}

This pattern avoids layout shifting because the border exists before hover, but it is transparent. On hover, only the border color changes.

currentColor in CSS

The currentColor keyword uses the current text color of the element. It is useful when borders, icons, or outlines should match the text color automatically.

.link-card {
  color: #111160;
  border: 1px solid currentColor;
}

If the text color changes, the border color changes with it. This can reduce repetition and keep components consistent.

Using CSS Custom Properties for Colors

Real projects often store important colors in CSS custom properties. This creates a small color system that can be reused across the stylesheet.

:root {
  --color-primary: #111160;
  --color-accent: #2c8ffa;
  --color-text: #222222;
  --color-surface: #ffffff;
}

.button {
  background-color: var(--color-primary);
  color: var(--color-surface);
}

Custom properties make theme changes easier. If the primary brand color changes, update the variable once instead of hunting through the full stylesheet.

Color Contrast and Accessibility

Color is not only a design choice. It affects accessibility. Text must have enough contrast against its background so users can read it comfortably. Low contrast text may look subtle, but it can become unreadable for users with low vision, poor screens, bright sunlight, or color vision differences.

  • Use strong contrast for normal body text.
  • Do not communicate important information using color alone.
  • Add icons, labels, patterns, or text when showing error and success states.
  • Check hover and focus states for readability.
  • Test colors on both light and dark backgrounds if the design supports themes.

For example, red text alone may not be enough to show an error. Add a clear message such as “Email is required” and use proper form semantics. Color should support meaning, not be the only source of meaning.

Common Color Mistakes in CSS

  • Using too many unrelated colors on one page.
  • Choosing colors without checking text contrast.
  • Using transparent text over complex images.
  • Hardcoding the same brand color in many places instead of using variables.
  • Using color alone to communicate success, warning, or error states.
  • Forgetting that hover colors also need good contrast.

Color Inheritance

The color property is inherited by default. If you set text color on the body, many child elements inherit that color unless they define their own. This is why global text color is usually placed on body or a main wrapper. Background colors, borders, and shadows do not inherit in the same way.

body {
  color: #222222;
}

.muted {
  color: #666666;
}

In this example, normal text can inherit the body color, while elements with the class muted use a softer color. Understanding inheritance prevents unnecessary repeated color declarations.

Theme Colors and Design Tokens

For a real website, colors should be treated as a system. A site may have a primary color, accent color, text color, muted text color, background color, border color, warning color, and success color. Naming these values with custom properties makes the stylesheet easier to maintain.

:root {
  --color-primary: #111160;
  --color-border: #dddddd;
  --color-success: #008a3d;
  --color-danger: #c62828;
}

These names describe purpose instead of only describing appearance. A variable named --color-danger is more meaningful than --red because the design purpose remains clear even if the exact shade changes later.

Practical Color Workflow

A practical color workflow starts with a small palette, not dozens of random values. Choose core brand colors, define text and surface colors, check contrast, then reuse the same values consistently. If every card, button, and label uses a slightly different shade, the page starts to feel unplanned.

When adding a new color, ask whether it represents a new purpose or whether an existing token already fits. This keeps the design consistent and prevents the stylesheet from becoming a collection of unrelated color values.

Colors for Interaction States

Interactive elements need colors for normal, hover, focus, active, disabled, success, and error states. These states should feel related, not random. A button hover color may be a slightly darker version of the normal color, while a focus outline should be clearly visible against the page background.

.button {
  background-color: var(--color-primary);
  color: white;
}

.button:hover {
  background-color: #09094a;
}

.button:focus-visible {
  outline: 3px solid var(--color-accent);
}

Focus color is especially important because keyboard users rely on it to know where they are on the page. Do not remove outlines unless you replace them with an equally clear custom focus style.

Disabled colors also need care. A disabled button can look muted, but its text should still be readable. Avoid making disabled text so faint that users cannot understand what action is unavailable.

Choosing Text and Background Colors

The most important color decision on any content page is the relationship between text and background. Body text should usually be dark on a light background or light on a dark background, with enough contrast for comfortable reading. Decorative colors can be softer, but reading colors should stay strong.

For long articles, avoid pure visual experiments that make paragraphs harder to scan. A beautiful color palette fails if users struggle to read headings, links, code blocks, tables, and form messages. Readability should win over novelty in content-heavy pages.

Colors in CSS FAQ

Which CSS color format should I use?

Use hex for compact fixed colors, RGB or RGBA when working with channel values and transparency, HSL when adjusting hue, saturation, and lightness, and custom properties for reusable design systems.

What is the difference between color and background-color?

The color property controls text color. The background-color property controls the background behind the element content and padding area.

Can CSS colors be transparent?

Yes. Use rgba(), hsl() with alpha syntax, or the transparent keyword when you need transparency.

Why does the same transparent color look different in two places?

Transparent colors blend with whatever is behind them, so the final visible result changes depending on the background.


Continue learning CSS in order
Follow the topic sequence with the previous and next lesson.