Sub topic 11
Checking contrast and accessibility
This chapter assumes you can already write dark: classes, have a working light, dark and system toggle, and know the basics of the WCAG (Web Content Accessibility Guidelines). The goal is to confirm that the dark theme you built is readable and usable, not just that it switches.
Why dark mode needs its own accessibility check
A dark theme is a second design, not an inverted copy of the first. Contrast depends on the relationship between a foreground color and its background. When both change, a pair that passed in light mode can fail in dark mode.
Palette colors do not map symmetrically. In Tailwind's default gray scale, gray-500 on white is readable, but the same gray-500 on gray-900 is not. Swapping text-gray-600 for dark:text-gray-400 by feel is a guess until you measure it.
Automated tools usually test only the theme that is active when they run. If you audit only in light mode, the dark theme goes unchecked.
The numbers to meet
WCAG 2.x defines contrast as a ratio between 1:1 (identical colors) and 21:1 (black on white). The thresholds for level AA are:
| Element | Minimum ratio | WCAG criterion |
|---|---|---|
| Body text under 18pt (24px), or under 14pt (about 18.66px) if bold | 4.5:1 | 1.4.3 |
| Large text (18pt or larger, or 14pt bold or larger) | 3:1 | 1.4.3 |
| Borders of inputs, icons, focus indicators | 3:1 against adjacent colors | 1.4.11 |
| Disabled controls, pure decoration, logos | No requirement | none |
Level AAA asks for 7:1 for normal text. Treat it as a goal for long-form reading, not a requirement for every label.
The ratio comes from relative luminance, a measure of perceived brightness. It is computed from the linearized sRGB channels:
L = 0.2126 R + 0.7152 G + 0.0722 B
ratio = (Llighter + 0.05) / (Ldarker + 0.05)
Green dominates the formula. That is why saturated blues and reds on a dark background often fail even when they look bright.
Measuring a pair in code
Measuring takes about twenty lines of JavaScript. This script uses Tailwind's default hex values and runs under Node 18 or later.
// contrast.mjs (run with: node contrast.mjs)
function channel(value) {
const c = value / 255;
return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
}
function luminance(hex) {
const n = parseInt(hex.replace("#", ""), 16);
const r = (n >> 16) & 255;
const g = (n >> 8) & 255;
const b = n & 255;
return 0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b);
}
export function contrast(fg, bg) {
const a = luminance(fg);
const b = luminance(bg);
const [hi, lo] = a > b ? [a, b] : [b, a];
return (hi + 0.05) / (lo + 0.05);
}
const pairs = [
["gray-500 on white", "#6b7280", "#ffffff"],
["gray-400 on gray-900", "#9ca3af", "#111827"],
["gray-500 on gray-900", "#6b7280", "#111827"],
["gray-600 on gray-900", "#4b5563", "#111827"],
];
for (const [label, fg, bg] of pairs) {
const ratio = contrast(fg, bg);
const verdict = ratio >= 4.5 ? "pass" : "fail";
console.log(`${label.padEnd(24)} ${ratio.toFixed(2)}:1 ${verdict} (text, AA)`);
}The results are roughly 4.8:1, 7.0:1, 3.7:1 and 2.3:1. The first two pass for body text. The third fails for body text but would pass for large text. The fourth fails everywhere.
The same gray, gray-500, passes on white and fails on gray-900. A class pair that looks consistent in the markup can still be wrong in one theme.
Fixing a failing pair
Start from the failing class and move one step along the scale in the direction that increases the gap.
<!-- Fails in dark mode: about 3.7:1 -->
<p class="text-gray-500 dark:text-gray-500">Last edited 3 days ago</p>
<!-- Passes in both themes: about 4.8:1 and 7.0:1 -->
<p class="text-gray-500 dark:text-gray-400">Last edited 3 days ago</p>Secondary text tends to be the main offender. Designers lower the contrast on purpose for hierarchy, then forget the floor of 4.5:1. If you theme with CSS variables, as in the earlier chapter, you can check every variable pair once instead of auditing every component.
Dark-specific problems that numbers don't catch
A passing ratio does not make a theme comfortable. Several issues are specific to dark interfaces.
- Maximum contrast can hurt. Pure
#fffffftext on pure#000000gives 21:1, but many readers see text glow or smear against it, an effect called halation. People with astigmatism report it most. A near-black surface such asgray-900withgray-100text (about 15:1) is usually more comfortable and still far above the threshold. - Saturated colors vibrate. A vivid
blue-600that works on white is dim on a dark surface, and a fully saturated color can look harsh. Use a lighter, less saturated step in dark mode, such asblue-400for links, and measure it. - Depth reverses. In light themes shadows signal elevation. On dark surfaces shadows are barely visible, so raised surfaces are usually a lighter gray (
gray-800on agray-900page). Make sure the difference between layers is visible, but remember it does not need to meet 3:1 unless it is the only way to identify a control. - Borders vanish.
gray-700ongray-900is about 1.7:1. That is fine for a decorative divider. It fails 1.4.11 when it is the only boundary of a text input or checkbox. - Images and icons. Dark-on-transparent PNGs and SVG icons with hard-coded black strokes disappear on dark backgrounds. Prefer
currentColorfor icon strokes so they followtext-*utilities.
Focus indicators and state
Keyboard focus must be visible in both themes. A ring that is clearly visible on white may disappear on dark. Set the ring color per theme and use focus-visible: so it appears for keyboard users.
<button
class="rounded bg-blue-600 px-4 py-2 text-white
hover:bg-blue-700
focus-visible:outline-none focus-visible:ring-2
focus-visible:ring-blue-600 focus-visible:ring-offset-2
focus-visible:ring-offset-white
dark:bg-blue-500 dark:text-gray-950 dark:hover:bg-blue-400
dark:focus-visible:ring-blue-300 dark:focus-visible:ring-offset-gray-900"
>
Save changes
</button>The ring offset color should match the surface behind it. If you leave the default white offset in dark mode, you get a bright halo that looks like a bug. Check hover and active states too. A dark:hover: color that is lighter than its resting state can drop below the text contrast limit.
Do not rely on color alone for state. Errors need an icon or text in addition to a red border, and selected items need more than a slightly different background.
Respecting user contrast and forced-colors settings
The dark: variant answers one preference: color scheme. Users can also request more contrast or override all colors. Tailwind exposes both as variants.
<input
class="rounded border border-gray-300 bg-white px-3 py-2 text-gray-900
dark:border-gray-600 dark:bg-gray-900 dark:text-gray-100
contrast-more:border-gray-900
dark:contrast-more:border-gray-100
forced-colors:border-[ButtonText]"
/>contrast-more:matchesprefers-contrast: more. Use it to strengthen borders and text for users who asked for it.forced-colors:matches Windows High Contrast and similar modes. The browser replaces your colors with a system palette, so custom backgrounds andbox-shadowborders may disappear. Use realborderandoutlineproperties on interactive elements, because the browser keeps those.
Both variants stack with dark:, so you can tune each theme separately.
Native controls and color-scheme
Controls you did not style, such as scrollbars, <select> popups, date pickers and default form fields, follow the color-scheme property. Without color-scheme: dark, a dark page can show a white scrollbar or black text on a dark checkbox. You set this in the CSS variables chapter, and it belongs in the audit too: open a form and a long scrolling page in each theme and look at the unstyled parts.
A repeatable audit
Run the same checks for every theme, in the same order each time. Skipping one theme is the usual cause of regressions.
Practical steps for each pass:
- Switch the theme with your toggle, not just the OS setting, so the
darkclass (or attribute) is applied. Reload to confirm the saved choice survives. - Run an automated tool such as the axe DevTools extension or Lighthouse's accessibility audit. They flag text contrast failures but miss many non-text cases.
- In Chrome or Edge DevTools, select an element and open the color picker in the Styles pane. It shows the computed contrast ratio and a line marking the AA and AAA boundaries.
- In the DevTools Rendering panel, emulate
prefers-color-scheme,prefers-contrastandforced-colors. Check each combination. - Put the keyboard through the whole page with Tab and Shift+Tab. Every control must show a visible focus indicator.
- Check text over images or gradients at the lightest and darkest points behind it. A single sampled pixel proves nothing.
What the tools do not tell you
Automated checks cannot judge whether a gray text is "secondary" enough to be skipped by a reader, or whether a chart relies on hue alone. They also skip states you must trigger manually: hover, focus, disabled, error, open menus and tooltips. Test these by hand in both themes.
WCAG 2.x contrast math has known weaknesses. It overrates some light-text-on-dark pairs and underrates some dark-on-light pairs. A newer method called APCA (Accessible Perceptual Contrast Algorithm) models this better, but it is not yet part of the WCAG 2 requirements. Use WCAG ratios for compliance, and treat APCA as an extra check when a passing pair still looks hard to read.
Exercise
A card uses bg-white text-gray-600 dark:bg-gray-900 dark:text-gray-600. In light mode the text passes (about 7.6:1). Predict whether the dark mode text passes AA for 16px body text, name the ratio range, and give a one-class change that fixes it.
Show answer
It fails. gray-600 (#4b5563) on gray-900 (#111827) is about 2.3:1, well under the 4.5:1 minimum for normal text. Changing dark:text-gray-600 to dark:text-gray-400 gives about 7.0:1 and passes. dark:text-gray-300 would also pass and would give stronger contrast.
Exercise
A text input uses border border-gray-700 on a bg-gray-900 page in dark mode. Which WCAG criterion applies to the border, what is the required ratio, and what change would you make?
Show answer
Criterion 1.4.11 (non-text contrast) applies, because the border is what identifies the input boundary. It needs at least 3:1 against the adjacent background. gray-700 on gray-900 is about 1.7:1, so it fails. Use a lighter border such as dark:border-gray-500 (about 3.7:1) or dark:border-gray-400 and re-measure.