HEXBLOOM COLOR GUIDE

CSS Color Variables and Design Tokens: A Practical Guide for Websites

A palette becomes much easier to maintain when every color has a clear role and a reusable name. CSS custom properties and design tokens let you update a website theme without hunting through dozens of stylesheets. This practical guide shows how to turn raw HEX values into a small, scalable color system.

Home / Articles / CSS Color Variables and Design Tokens: A Practical Guide for Websites

What are CSS color variables and design tokens?

A CSS custom property is a reusable value declared with a name beginning with two hyphens, usually inside a selector such as :root. A design token is the broader idea of naming a design decision—such as a primary action color, page surface, or muted text color—so it can be shared across components and platforms. CSS variables are one way to implement those tokens on the web.

Instead of repeating #6750A4 in multiple button, link, and badge rules, define a semantic token such as --color-primary. Components then refer to the token. If the brand color changes, update one value and the entire interface can inherit the new choice. Semantic names also explain why a color exists, which is more useful than names such as purple-2 when a project grows.

Start with role-based color tokens

A practical starter system includes --color-primary for the main action and brand emphasis, --color-secondary for supporting emphasis, --color-accent for limited highlights, --color-background for the page canvas, --color-surface for cards and panels, --color-text for primary copy, and --color-muted for secondary information. Add border, focus, success, warning, and danger tokens when your interface needs them.

Keep roles distinct from raw palette steps. A brand palette may contain ten purple shades, but the semantic token --color-primary should point to the shade that works for the main action. A lighter shade might become --color-primary-soft for a tinted panel. This separation lets you change the underlying palette without rewriting component styles.

Example: define a CSS theme

Here is a small starting point. Replace the example values with colors from your own tested palette.

:root {
  --color-primary: #5145cd;
  --color-secondary: #d9d6ff;
  --color-accent: #087f8c;
  --color-background: #fbfbfe;
  --color-surface: #ffffff;
  --color-text: #202033;
  --color-muted: #5e6275;
  --color-border: #e5e6ef;

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

.card { color: var(--color-text); background: var(--color-surface); border: 1px solid var(--color-border); } ```

The values are illustrative, not a guarantee that every pair passes WCAG. Test the actual combinations before shipping. Notice that the components use semantic names rather than hard-coded HEX values. This makes the CSS easier to read and gives designers and developers a shared vocabulary.

Add dark mode without duplicating every component

A theme can override the same semantic properties under a class or data attribute. For example, [data-theme="dark"] can change the page background, surface, text, muted text, border, and action colors while the component rules remain unchanged. The component still uses var(--color-surface); only the active theme values change.

When building dark mode, do not simply invert every HEX value. Dark interfaces need deliberate surface layering, readable text, visible borders, and brand colors adjusted for the darker background. Recheck contrast for every theme, including focus rings and disabled states. If the user can switch themes, make the control keyboard accessible and expose its state to assistive technology.

Export tokens to Tailwind CSS v4 and other formats

For projects using Tailwind CSS v4, theme variables can be declared with the @theme directive in your CSS entry file. A simple example is:

@theme { --color-primary: #5145cd; --color-secondary: #d9d6ff; --color-accent: #087f8c; } ```

Tailwind v4 uses theme variables to define utilities, so use the syntax documented for your installed version rather than copying a v3 JavaScript configuration into a v4 project. See the official Tailwind theme documentation. For other workflows, export CSS custom properties, SCSS variables, or JSON tokens and keep the names consistent across files.

HexBloom's palette generator helps create and lock a starting set of colors. The palette-to-CSS and Tailwind exporter can turn selected values into code-oriented output. After export, review token names, test contrast, and integrate them into your own build system.

A maintainable token workflow

Document what each token is for, keep the core set small, and avoid creating a new token for every component unless there is a real design reason. Store source values in one place and generate platform-specific output from that source when a project becomes large. Use version control for token changes, and review screenshots or component states after a theme update. A color system is successful when it helps a team make consistent decisions—not when it contains the most variables.

Frequently asked questions

What is the difference between a CSS variable and a design token?

A CSS custom property is a CSS mechanism for storing and reusing a value. A design token is a named design decision that can be represented in CSS, JSON, design tools, or other platforms.

How many color design tokens should a website have?

Start with a small semantic set for primary, secondary, accent, background, surface, text, muted text, and border. Add semantic states and shades only when the interface needs them.

Can CSS variables support dark mode?

Yes. Define the same semantic custom properties in a dark-theme selector such as `[data-theme="dark"]`, then let components continue using those variables. Test contrast separately in each theme.

How do I use color tokens in Tailwind CSS v4?

Define supported theme variables inside an `@theme` block in your CSS entry file, following the official Tailwind CSS v4 theme documentation.

Should I use HEX values directly in every component?

For a maintainable interface, prefer semantic CSS variables or design tokens. This centralizes changes, improves consistency, and makes light/dark themes easier to manage.

Continue learning with HexBloom

Put the guide into practice.

Explore free color tools to generate palettes, check contrast, convert colors, and export developer-ready values.

Explore HexBloom tools