Sub topic 14
Documenting patterns and style guides
This chapter assumes you know BEM naming, CSS custom properties for design tokens, and basic npm usage. Conventions only help if people can find them. A short, living record of your components and tokens keeps a large codebase consistent as the team changes.
Why document CSS
Naming rules and token names live in people's heads until you write them down. New developers then copy whatever they see first, and the codebase drifts. Good documentation answers three questions:
- What components and tokens exist?
- How do I use them?
- What am I not allowed to do?
Documentation that lives apart from the code goes stale. Prefer docs that sit next to the CSS or are generated from it.
What to document
Not everything needs a page. Focus on the parts other people depend on.
| Item | What to record |
|---|---|
| Design tokens | Name, value, and purpose |
| Components | Markup, modifiers, and states |
| Conventions | Naming, file layout, and layer order |
| Exceptions | Where a rule is deliberately broken, and why |
Document tokens from the source
Tokens are the easiest thing to document because their names already describe them. Add a short comment above each group so the intent is clear.
/* Colour tokens. Use these instead of raw hex values. */
:root {
--color-text: #1a1a1a; /* body copy */
--color-accent: #0b5fff; /* links, primary buttons */
}
/* Spacing scale. Pick the nearest step, do not invent new ones. */
:root {
--space-2: 0.5rem;
--space-4: 1rem;
}Comments on the tokens stay close to the values, so they are more likely to be updated when the values change.
Document components with examples
For each component, show the real markup. A reader should be able to copy it. Include the block, its elements, and each modifier.
<!-- Card: use card--featured to highlight one item per page. -->
<article class="card card--featured">
<h2 class="card__title">Release notes</h2>
<p class="card__body">What changed this week.</p>
</article>A style guide page is a set of such examples, rendered live. Because the examples use your actual stylesheet, they show the true appearance and cannot drift from the code.
Living style guides
A living style guide is a site built from your real components. It renders each one from the production CSS, so it updates whenever the code does. Tools such as Storybook, Fractal, and Pattern Lab build this kind of catalogue. Each one lets you list components, show their variants, and attach written notes.
A typical page for one component contains:
- A short description of when to use it.
- A rendered preview of each variant.
- The markup to copy.
- Notes on accessibility and known limits.
Keep a short conventions page
Alongside the catalogue, keep one plain page of rules. It should be short enough that people actually read it. Cover:
- The naming convention, with one example.
- The folder structure and the layer order from
@layer. - The Stylelint rules in force, and where the config lives.
- How to propose a new component or token.
Link the Stylelint config instead of copying it, so the page cannot disagree with the enforced rules.
Common mistakes
- Writing docs once and never updating them. Make doc updates part of the pull request checklist.
- Documenting every detail. Long pages go unread, so record decisions and usage, not every property.
- Showing screenshots instead of live examples. Screenshots go out of date silently.
- Leaving out the "why". A rule with no reason gets ignored or removed.
Exercise
Exercise
Why is a living style guide better than a static document describing your components?
Show answer
It renders the components from the real production CSS, so it updates when the code changes and cannot drift from what users see. A static document must be edited by hand and goes stale.
Exercise
You add a new modifier, .card--compact. Name two places where you should record it.
Show answer
In the component's style guide entry, with a rendered preview and markup. Also in the pull request's documentation checklist or the comments next to the component CSS, so the reason and usage are recorded where the code lives.