Color contrast with Web Components

David Darnes
Written by David Darnes
Jun 5, 2024

Looking for a straightforward way to show the contrast levels of two colors on a web page, website, or design system documentation? Here, take this <contrast-details> Web Component, drop it onto your page, and add two colors to it. But hold up! Let's take a minute to talk about why I made this, and why it could be more useful than you think.

Forgive me for the abrupt introduction, but I like to try to balance “immediately useful” and more in-depth thinking. Now that we have that out of the way, I’d like to talk about the somewhat meta-problem of how developers can document web code using web code.

The problem

A list of color design tokens with their description and CSS variable
Credit: Nord Design System: https://nordhealth.design/tokens/

Here, we have a list of color tokens on a design system documentation page. Pretty commonplace in a design system... Both designers and developers need those colors (and variables for Figma and CSS) to help design and build UI.

Wait though, enhance  

A close up of one of the color tokens showing that there's a pair of colors overlayed along with their contrast ratio

They’re showing the contrast levels of that color compared to other colors! Very cool, but how? If I were the designer, I could get that information from a Figma plugin (eg Contrast Checker plugin from Zach Inglis). This needs to be placed on a web page though. The contrast levels could be passed onto the developer of the documentation page, adding it themselves. However, that seems awfully manual, and could get really tedious to maintain. What if we could get the web page to do it for us? Enter Web Components.

<contrast-details> Web Component

People who follow my work will know I have a keen interest in Web Components, mainly down to how versatile it is when building small useful tools for the web. So, once again, I’m back on my “bullship.” I’ve created and open sourced <contrast-details>, a Web Component that takes two colors and presents their contrast ratio and grade based on the  WCAG contrast levels specification. Here’s how you would use it on a typical web page:

See the Pen <contrast-details> Web Component by David Darnes (@daviddarnes) on CodePen.

Without going too deep Web Components are a technology for making portable and versatile tools for the web. Think of them like a YouTube embed, only tinier and more precise—an ideal technology to build upon for a tool such as this.

The Case for Web Components

Watn to learn more about Web Components and how they could help you and your design system? Download a our free guide to find out more.

<!-- single script tag to use the component -->
<script type="module" src="https://esm.sh/@daviddarnes/contrast-details@1.1.0"></script>

<!-- Web Component usage with foreground and background colors applied -->
<contrast-details style="--foreground: #444; --background: #ccc"></contrast-details>

I’ve created the component so developers can apply the colors using a simplified style attribute, and directly use those colors within the component. This is mostly thanks to CSS Custom Properties. If you’re interested in learning more, check out this article on exporting your design tokens as CSS Custom Properties using zeroheight.

In addition, an attribute has been added to the Web Component that can be hooked into using CSS, meaning you can style the content depending on whether the contrast levels pass or fail.

For the folks out there who are interested in more details and functionality, there's a write-up on my own blog. I hope you and your development team find this tool useful! Please let me know if you use this Web Component, it would be great to see it in action.

David Darnes
David Darnes
Try zeroheight
The design systems platform that keeps teams and agents in sync
Start for free
Share this post