Full answer
CSS is hard to test because the output is a picture, and small changes can have large side effects. A good strategy combines several kinds of test, each with its own job.
- Linting. A tool such as Stylelint reads the source and catches syntax errors, unknown properties, duplicate selectors,
!important, ids in selectors and style rules that break your conventions. It is fast and runs in the editor and in the CI pipeline. It cannot see the result. - Computed style tests. In a real browser, load a component and assert values: the colour of a button, the padding, the display type, the position. They are quick and precise, and they work well for tokens and states, such as focus or hover, and for media queries by changing the viewport. They do not catch a layout that looks wrong when all numbers are right.
- Visual regression tests. Take a screenshot of each component or page, compare it with an approved baseline and fail when the difference is above a small threshold. They catch unplanned changes, such as a header that moved 10 pixels. Cost: baselines need review when a change is intended, fonts and rendering must be the same in every run (use one browser version in a container), and animation and dynamic data must be frozen.
- Accessibility checks. Automated tools find missing names, low contrast and invalid ARIA in the rendered page. They catch many but not all problems, so also test with the keyboard and a screen reader by hand.
- Responsive and state coverage. Run the tests at several widths, in dark mode, with reduced motion, with forced colours and with larger text.
- Performance budgets. Track the size of the stylesheet and the time of style and layout work for key pages.
A good order: lint on every change, computed and accessibility tests on every pull request, and visual tests on components with a review step. Keep tests few and meaningful, because slow or flaky tests are ignored.
Three assertions pass. The fourth fails on purpose, because the button has the default border of 2px, and the failure message shows the real value. This is the kind of unplanned default that computed tests find.
Choose tests by the risk, and keep each one stable enough to trust.
<style>
.btn { background: #0a58ca; color: #fff; padding: 8px 16px; }
.btn:focus-visible { outline: 3px solid #111; }
</style>
<button class="btn" id="b" type="button">Save</button>
<script>
const results = { passed: 0, failed: 0, failures: [] };
function assertStyle(el, prop, expected) {
const actual = getComputedStyle(el)[prop];
if (actual === expected) results.passed++;
else { results.failed++; results.failures.push(prop + ": " + actual); }
}
assertStyle(b, "backgroundColor", "rgb(10, 88, 202)");
assertStyle(b, "paddingLeft", "16px");
assertStyle(b, "color", "rgb(255, 255, 255)");
assertStyle(b, "borderTopWidth", "0px");
window.results = results;
</script>The test helper is a small stand-in written for the example. No Stylelint, screenshot or accessibility tool was run. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Linting checks the source, not the result.
- Computed style tests check exact values in a browser.
- Visual regression catches unplanned visual changes.
- Automated accessibility checks find many but not all issues.
- Test several widths and modes; keep tests stable.
Common mistakes
- Using only screenshot tests, with unstable fonts and dynamic data.
- Testing numbers only, and missing layouts that look wrong.
- Writing so many tests that the team ignores failures.
Follow-ups
- What makes visual regression tests flaky?
- Why are automated accessibility checks not enough?
Tips
- Name the test types and what each catches and misses.
- Give a sensible order for a pipeline.