Sub topic 12
Keeping clickable styles consistent across a site
This chapter assumes you can write the button and link CSS from earlier chapters, including hover, focus-visible and disabled states. It also assumes you know CSS custom properties (variables) and the cascade, and that you can run a snippet in the browser console. Here you stop styling single elements and make every button and link on a site follow the same rules.
Why consistency matters
Users learn what "clickable" looks like from the first few pages of a site. Later they apply that lesson without thinking. If the site breaks the pattern, they hesitate or miss controls entirely.
Inconsistency usually has three causes:
- Different developers solve the same problem at different times, so one page has a 6px radius and another has 8px.
- Quick fixes add one-off overrides such as
.pricing-page .btn { background: green; }. - Content from a CMS or a third-party widget arrives with its own styles.
Inconsistency also has user-facing costs:
- A link that is underlined in articles but plain in a sidebar teaches users that underlines are unreliable.
- A primary button that is blue in the header and green in a form makes users doubt which action matters most.
- Focus rings that exist on some pages and not others break keyboard navigation unpredictably.
Consistency does not mean every control looks identical. Buttons are ranked, so primary and secondary differ on purpose. The rule is that the same role always gets the same look, and the same state always gets the same feedback.
Define the decisions once as tokens
A design token is a named value for a design decision, such as "the action color" or "the minimum target height". In plain CSS you store tokens as custom properties on :root. Components read the tokens and never contain raw values.
:root {
--color-action: #1d4ed8;
--color-action-hover: #1e40af;
--color-action-visited: #6d28d9;
--color-action-subtle: #eff6ff;
--color-on-action: #ffffff;
--color-danger: #b91c1c;
--color-danger-hover: #991b1b;
--radius-control: 8px;
--target-min: 44px;
--focus-ring: 3px solid var(--color-action);
--focus-offset: 3px;
}
@media (prefers-color-scheme: dark) {
:root {
--color-action: #93c5fd;
--color-action-hover: #bfdbfe;
--color-action-visited: #c4b5fd;
--color-action-subtle: #1e293b;
--color-on-action: #0f172a;
}
}The dark-mode block shows the main benefit. You change the palette in one place, and every button, link and focus ring follows. You still have to recheck contrast for the new values. White text on the light blue would fail, which is why --color-on-action changes to a dark color.
Name tokens by role (--color-action) and not by appearance (--color-blue). If the brand color changes to green, a role name stays correct and an appearance name becomes a lie.
One base class and small modifiers
The button chapters gave you .btn and .btn--secondary. To keep variants consistent, build them on the same base and change only what differs. A good method is to give the component its own local custom properties, and let each variant reassign them.
.btn {
--btn-bg: var(--color-action);
--btn-bg-hover: var(--color-action-hover);
--btn-fg: var(--color-on-action);
--btn-border: var(--btn-bg);
display: inline-flex;
align-items: center;
justify-content: center;
min-height: var(--target-min);
padding: 0 1.25rem;
border: 2px solid var(--btn-border);
border-radius: var(--radius-control);
background: var(--btn-bg);
color: var(--btn-fg);
font: inherit;
font-weight: 600;
text-decoration: none;
cursor: pointer;
}
.btn:hover {
--btn-bg: var(--btn-bg-hover);
--btn-border: var(--btn-bg-hover);
}
.btn:active {
transform: translateY(1px);
}
.btn:focus-visible {
outline: var(--focus-ring);
outline-offset: var(--focus-offset);
}
.btn--secondary {
--btn-bg: transparent;
--btn-bg-hover: var(--color-action-subtle);
--btn-fg: var(--color-action);
--btn-border: var(--color-action);
}
.btn--danger {
--btn-bg: var(--color-danger);
--btn-bg-hover: var(--color-danger-hover);
}Look at what this buys you:
- Hover, active, focus and size rules are written once. A new variant cannot forget the focus ring, because it inherits it.
- A variant changes only color values. It cannot accidentally change the target size or radius.
- The hover rule reassigns
--btn-bgand--btn-borderon the element itself. Thebackgroundandborderdeclarations pick up the new values automatically.
One detail needs care. The .btn--secondary hover color is --color-action-subtle, a pale fill. The text stays --color-action, so contrast holds in both states. Check every variant's hover state, not only its resting state.
Links get the same treatment
Links need a small set of tokens too. Keep the link rules from the earlier chapter, and read the colors from the same tokens so links and buttons share one action color.
a {
color: var(--color-action);
text-decoration: underline;
text-decoration-thickness: 1px;
text-underline-offset: 0.15em;
}
a:visited {
color: var(--color-action-visited);
}
a:hover {
color: var(--color-action-hover);
text-decoration-thickness: 2px;
}
a:focus-visible {
outline: var(--focus-ring);
outline-offset: 2px;
}
.btn:visited {
color: var(--btn-fg);
}The last rule matters. The global a:visited selector has the same specificity as .btn, class and pseudo-class both counting as one class-level unit, and a link styled as a button would otherwise turn purple after a visit. .btn:visited is declared later and has the same specificity as a:visited, so it wins and the button keeps its text color. Buttons are actions, so a "visited" color on them is meaningless.
Navigation is the one allowed exception
Menus, tab bars and footers often drop underlines. This is acceptable only when position and grouping already signal clickability: a row of items inside a <nav> element. Make it a rule and scope it clearly:
nav a {
text-decoration: none;
font-weight: 600;
}
nav a[aria-current="page"] {
text-decoration: underline;
}Do not let the exception spread. A "Read more" link in a card sits in running content, so it keeps its underline.
Prevent override creep
Consistency usually erodes through specificity fights. Someone needs a one-off change, writes a longer selector, and the next person has to write a longer one still. Three habits slow this down.
- Change the token, not the component. If the pricing page needs a different accent, set
--color-actionon that page's container and let everything inside inherit it. - Add a modifier instead of an override. If you need a new variant, name it (
.btn--danger) and add it to the shared stylesheet. - Use cascade layers to fix the order of precedence, so selector length stops deciding who wins.
@layer reset, base, components, utilities;
@layer base {
a { color: var(--color-action); }
}
@layer components {
.btn { min-height: var(--target-min); }
}Styles in a later layer beat earlier layers regardless of specificity. A third-party stylesheet imported into the reset layer then cannot defeat your .btn rules, even if its selectors are more specific. Unlayered styles beat all layered styles, so keep your own code inside layers and avoid !important.
Handling content you do not control
CMS articles, markdown output and embedded widgets will contain plain <a> and <button> elements with no classes. Two practices help.
- Style bare elements (
a,button) with sensible defaults, as above, so unclassed content is still acceptable. - Scope a prose container so editors get the correct link style without adding classes, for example
.prose a { ... }.
For a widget that ships its own CSS, check it after integration and look for missing focus rings or a different action color. If the widget exposes custom properties, map them to your tokens.
How the pieces fit together
The diagram shows the flow of decisions, from tokens down to the elements users see.
Audit what you actually shipped
Rules in a stylesheet do not prove the site follows them. You can measure how many distinct looks exist. Paste this into the browser console on a page:
const looks = new Map();
document.querySelectorAll('a[href], button, [role="button"]').forEach((el) => {
const s = getComputedStyle(el);
const key = [
s.backgroundColor,
s.color,
s.borderRadius,
s.minHeight,
s.textDecorationLine,
].join(' | ');
looks.set(key, (looks.get(key) || 0) + 1);
});
console.table([...looks].map(([style, count]) => ({ style, count })));Read the table as a list of distinct looks. A healthy page shows a handful of rows that map to your known variants: primary, secondary, danger, inline link and navigation link. Many rows with a count of 1 point to one-off styles. Find those elements and move them onto a shared class.
Repeat the audit across several templates, such as the home page, a form, an article and a settings page. Look at states as well. Tab through each page and confirm the focus ring looks the same everywhere, then hover over a sample of controls.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Raw hex values inside components | A brand change needs many edits and some get missed | Read colors from tokens |
| New variant copies the whole `.btn` block | Variants drift apart over time | Reassign local custom properties only |
| Global `a:visited` hits link-styled buttons | Buttons turn purple after a visit | Add `.btn:visited` with the button text color |
| Underlines removed everywhere | Inline links lose their cue | Limit the exception to `nav` |
| Dark mode changes colors but not contrast checks | Text or focus rings become hard to see | Recheck contrast for each token set |
| `!important` used to win conflicts | Overrides escalate | Use tokens, modifiers and layers |
Summary of working rules
- Store decisions as role-named tokens and read them everywhere.
- Build one base button and add variants that change only color values.
- Share the action color between buttons and links, and keep focus rings identical.
- Allow underline removal only in navigation.
- Control precedence with layers and avoid
!important. - Audit computed styles across templates and fix outliers.
Exercise
A teammate adds .checkout .btn { background: #16a34a; border-radius: 4px; min-height: 36px; } so the checkout page "pops". List three problems this creates for consistency and accessibility, and describe a better way to give the checkout page a distinct primary action.
Show answer
Problems: (1) the radius differs from every other page, so the same role gets a different look; (2) min-height: 36px drops below the 44px target-size token, making the button harder to hit on touch screens; (3) the hover, active and focus rules still use the old blue values, so the hover color clashes with the green fill and the pattern is now inconsistent, and the new green was never contrast-checked against the white text. The selector also raises specificity, so future changes must beat it. A better approach is to leave the component alone. If the page truly needs another accent, set --color-action and --color-action-hover on the .checkout container to contrast-checked values, so the base .btn picks them up with its size, radius and states unchanged. If the green is a real new role, such as "confirm purchase", add a named modifier like .btn--confirm in the shared stylesheet that reassigns only the color properties.
Exercise
You run the audit script on an article page and the table shows nine rows. Five of them have a count of 1 and a border radius that is not 8px. What does this suggest, and what are your next two steps?
Show answer
It suggests one-off styles: several controls were styled outside the shared component, so the same roles look different. Step 1: use the console or the Elements panel to locate each element in the count-1 rows and identify which role it plays (primary action, secondary action, inline link, navigation link). Step 2: replace its custom styling with the matching shared class or variant, such as .btn, .btn--secondary or the base link style, and delete the one-off rule. Then run the audit again to confirm the row count falls to the number of real variants.