Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk4 min

Use CSS Color Tokens Instead of Scattering Hex Codes

Hex remains valid CSS, but repeated literals hide color roles and make palette changes harder. Use semantic custom properties to centralize colors without giving up hex.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Stop scattering raw hex values throughout your CSS—not because hex is invalid, but because repeated, opaque literals make a palette harder to understand and update. Define reusable, role-based custom properties such as --color-text and --color-surface, then use those tokens wherever the roles appear. The token values can still be hex.

Why replace repeated hex values?

A value such as #202124 tells you the color components, but not what the color is for. A stylesheet with many such literals makes it harder to see which color serves as body text, a surface, a link, or a border. If the palette changes, you may also have to find and update each occurrence separately.

As an Amazon Associate I earn from qualifying purchases.

Reusable design tokens address both problems: they give colors meaningful roles and create a central place to change their values. The W3C Design System advises using its palette variables rather than copying hex values, so later CSS updates can carry through to a site. That is practical design-system guidance, not a CSS requirement.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How to define and use color tokens

Start with the roles your interface actually uses. Define them once, commonly on :root, and refer to them with var() where needed:

#1 Best Overall
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option
:root {
  --color-text: #202124;
  --color-surface: #fff;
  --color-link: #005a9c;
}

body {
  color: var(--color-text);
  background: var(--color-surface);
}

a {
  color: var(--color-link);
}

These values demonstrate syntax only; they have not been checked for contrast in a particular context. The benefit here is the relationship between a semantic name and its uses, not a claim that these specific colors are suitable for every design.

  1. Identify roles. Name tokens by purpose, such as text, surface, link, or border, rather than by a particular shade that may change.
  2. Define each value once. Put shared tokens in a common scope such as :root, or in the scope appropriate to your project.
  3. Replace repeated literals. Use the matching token wherever that design role appears. Keep one-off colors literal only when they genuinely have no reusable role.
  4. Check the rendered pairings. Review foregrounds against their actual backgrounds and check the interface colors that communicate control boundaries or states.

Does hex need to go too?

No. Hex is valid CSS and represents red, green, and blue components in sRGB, with optional alpha. Its common forms are #RGB, #RGBA, #RRGGBB, and #RRGGBBAA. MDN describes the feature as widely available across browsers since July 2015; that compatibility history does not make hex the best notation for every team or task. See MDN’s reference for the CSS hex-color type.

You can keep hex as the value of a custom property and still make the stylesheet easier to maintain. If your team finds another notation clearer, use it consistently for the token values. The important distinction is between how a color is written and how its purpose is represented in the code.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Which CSS color notation should you choose?

CSS supports named colors and functions including rgb(), hsl(), hwb(), lab(), and oklch(). Different notations can describe the same color, but they make different trade-offs in compactness, readability, and ease of adjustment. MDN’s CSS code style guide recommends common named colors when an exact palette is unimportant, defaults to rgb() for a specific color, and suggests HSL or other functions when their representation is useful, such as for a color wheel or gradient. It also notes that hex is terser but may be less readable and is interchangeable with rgb() depending on convenience. That is MDN’s style guidance, not a universal CSS rule. Read its section on choosing a notation.

Rank #3
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
  • Use hex if its compact form is easy for your team to scan and your project already uses it consistently.
  • Use rgb() if spelling out channels makes a specific sRGB color easier to read or work with.
  • Use HSL or another color function when that model makes the color’s properties or intended adjustments clearer.
  • Use named colors sparingly for precise palettes. CSS’s color names are a fixed vocabulary, not project-specific roles such as “link” or “surface.”

For any notation, consider team readability, the adjustments you need to make, your browser-support requirements, and the color space expected by your accessibility review. MDN’s CSS color values guide describes the available notations. Check compatibility against your project’s support matrix before adopting modern color functions; support should not be assumed for every target browser.

Will changing notation make colors accessible?

No. Accessibility depends on the rendered colors and how they are used, not whether their CSS values are written in hex, RGB, or HSL. Check each foreground/background pairing and ensure controls and other meaningful information remain distinguishable.

The W3C Design System’s summary of WCAG 2.2 minimum contrast guidance gives a ratio of 4.5:1 for regular text, 3:1 for large text, and 3:1 for relevant non-text interface component boundaries and states and graphical objects. These are contrast requirements to evaluate against actual colors, not guarantees provided by any particular notation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For accessibility review, sRGB remains a practical baseline: MDN describes it as the default and preferred color space because it is ubiquitous, and notes that contrast standards and guidance are predominantly written around it. See MDN’s guide to colors and luminance.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When are advanced color functions useful?

If you need derived colors or theme-aware values, CSS offers tools beyond choosing a literal notation. Relative color syntax and color-mix() can derive colors from existing values; light-dark() can choose between light and dark values according to a selected or requested color scheme. These can help with more expressive color systems, but they are optional—not prerequisites for centralizing palette values. Confirm browser support for your project before relying on them.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.