Sub topic 11
Checking contrast and accessibility
This chapter assumes you have Tailwind installed, know the dark: variant, and have a working class or selector strategy from earlier chapters. You will learn how to check that a dark theme is readable, and how to fix the usual problems.
Why dark themes need their own check
A color pair that passes in light mode can fail in dark mode. Swapping text-gray-600 on white for the same gray on gray-900 often gives text that is too dim. Every dark: override creates a new foreground and background pair, so each pair needs its own check.
The Web Content Accessibility Guidelines (WCAG) set minimum contrast ratios. A contrast ratio compares the brightness of two colors, from 1:1 (identical) to 21:1 (black on white).
| Content | Minimum ratio (AA) |
|---|---|
| Normal body text | 4.5:1 |
| Large text (about 24px, or 18.66px bold) | 3:1 |
| Icons, borders of inputs, focus rings | 3:1 |
A dark pair that fails, and one that passes
In the first card, the muted text is too dark against the dark background. In the second, the lighter shade passes.
<!-- Fails: gray-600 on gray-900 is roughly 2.6:1 -->
<p class="text-gray-600 dark:text-gray-600 dark:bg-gray-900">Muted text</p>
<!-- Passes: gray-300 on gray-900 is well above 4.5:1 -->
<p class="text-gray-600 dark:text-gray-300 dark:bg-gray-900">Muted text</p>The ratios above are approximate. Measure your own colors instead of trusting a table, because a custom palette will differ from the defaults.
Avoid pure black and pure white
Pure white (#ffffff) text on pure black (#000000) has the maximum ratio, but it can look harsh and cause glare for some readers. Many designs use a very dark gray background and an off-white text color.
<body class="bg-white text-gray-900 dark:bg-gray-900 dark:text-gray-100">This pair stays well above 4.5:1 and is softer to read. If you use CSS variables, keep this choice in one place:
:root {
--surface: #ffffff;
--text: #111827;
}
.dark {
--surface: #111827;
--text: #f3f4f6;
}Check more than body text
Contrast problems often hide in secondary elements. Check each of these in dark mode:
- Placeholder text in inputs, which browsers often render dimmer than you expect.
- Disabled controls, which are exempt from the rule but should still be distinguishable.
- Borders and dividers, such as
border-gray-200, which can vanish or become glaring on dark surfaces. - Focus rings. A
ring-blue-500ring that is visible on white may be weak on a dark surface. - Links and visited links, which need to be distinguishable from surrounding text by more than color alone where possible.
Here is a focus style that adapts to the theme:
<button class="rounded bg-blue-600 px-3 py-1 text-white
focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-600 focus-visible:ring-offset-2
dark:bg-blue-500 dark:focus-visible:ring-blue-300 dark:focus-visible:ring-offset-gray-900">
Save
</button>The ring-offset-gray-900 value matters. The offset area is painted with the offset color, so it should match the dark background, or you get a white halo.
How to measure
Use more than one method:
- Open browser developer tools, inspect a text element, and read the contrast ratio in the color picker. Chrome and Firefox both show it and mark whether it passes AA.
- Run an automated audit such as Lighthouse or axe DevTools with the dark theme active. Toggle the theme first, then run the scan, because the audit only sees the current state.
- Check by eye on a real screen. Automated tools miss text over images, gradients, and hover states.
Test both paths in your workflow:
Respect other user preferences
Dark mode is one of several preference settings. Users may also ask for more contrast or less motion. Tailwind has variants for these:
<div class="border border-gray-300 contrast-more:border-gray-900 dark:border-gray-700 dark:contrast-more:border-gray-100">
<p class="transition-colors duration-300 motion-reduce:transition-none">Card</p>
</div>contrast-more: applies when the operating system requests higher contrast. motion-reduce: applies when the user asks for less animation, which matters if your theme toggle fades colors across the page.
Do not rely on color alone to convey state. An error shown only by a red border can be invisible to some users, so add an icon or text.
Common mistakes
- Reusing a light-mode shade in
dark:without checking it. - Testing only the system strategy and never the manual class strategy.
- Auditing the page once, in light mode only.
- Forgetting text over images, which needs an overlay or a solid background behind it.
Exercise
A paragraph uses text-gray-500 dark:text-gray-500 on a dark:bg-gray-900 card, and an audit reports it fails AA. Name one way to fix it and one way to confirm the fix.
Show answer
Change the dark text to a lighter shade, for example dark:text-gray-300, so the ratio reaches at least 4.5:1. Confirm it by activating dark mode, inspecting the element in browser developer tools to read the contrast ratio, and re-running an automated audit such as Lighthouse or axe.
Exercise
Why can a focus ring with ring-offset-2 look wrong on a dark page, and what class fixes it?
Show answer
The offset area is painted with the ring offset color, which defaults to white, so it shows as a white halo on a dark background. Add a dark offset color such as dark:ring-offset-gray-900 so it matches the page background.