Skip to content

VIS-002 Use each colour token only for the role and the pairs it was made for

Level
Rule
Verdict
Unanimous (3 of 3)
Status
proposed, version 1.1.0

Rule

Use a *-foreground token only on its own fill, border-input for the edges of controls, and a state colour such as ring, destructive or accent only for its state. Never use raw palette classes such as text-gray-500 for something that carries meaning.

Examples

Live examples built with Justified's components. Try them with the mouse, the keyboard or a screen reader.

Do: Each text colour on its own fill

Light theme

Order 2291

Payment failed

3 plants, delivery on Friday

  • text-destructive on the card: measuring …
  • text-muted-foreground on bg-muted: measuring …
  • text-primary-foreground on bg-primary: measuring …

Dark theme

Order 2291

Payment failed

3 plants, delivery on Friday

  • text-destructive on the card: measuring …
  • text-muted-foreground on bg-muted: measuring …
  • text-primary-foreground on bg-primary: measuring …
text-destructive on the card, text-muted-foreground on bg-muted and text-primary-foreground on bg-primary are the pairs Justified tests. Switch the page theme and the figures follow, because every colour is named by its role.

Don't: A foreground on someone else's fill

Light theme

  • text-primary-foreground on bg-muted: measuring …
  • text-muted-foreground on bg-primary: measuring …
  • text-accent-foreground on bg-destructive: measuring …

Dark theme

  • text-primary-foreground on bg-muted: measuring …
  • text-muted-foreground on bg-primary: measuring …
  • text-accent-foreground on bg-destructive: measuring …
Three drawings: a foreground token laid on a fill it was never paired with. Each is measured only against its own partner, so these combinations are nobody's responsibility and the figures show it.

Do: The accent fill for the chosen card

Choose a plan
Choose a plan with the pointer, or press Tab and then the arrow keys. The chosen card has the bg-accent fill, a check and the word Selected. The focus ring is a separate thing: it is only on the card you are on.

Don't: Selection drawn as focus

Choose a plan
The chosen card has border-ring ring-2 ring-ring. Before you touch anything it already looks as if it has focus. Press Tab into the group and the real focus ring is hard to tell from it; press Tab again and the chosen card still looks focused.

Why

A token is a promise about where a colour goes. primary-foreground was chosen to be read on primary, input to mark the edge of a field, ring to show focus. Justified's contrast test checks those pairs in both themes, and a theme change reaches every place that uses them by name. A colour borrowed for another job keeps none of that. It was never measured where it now sits, it does not follow the theme, and when the palette changes it breaks without anyone noticing. A colour that has one meaning also keeps that meaning: once red marks a sale price, it no longer warns of an error. No WCAG criterion asks for tokens. What the law asks is the measured result, in VIS-001 for text and VIS-004 for borders and graphics. Using colours by role is how the judges keep that result true across themes and later changes, and it keeps colour cues readable in the sense of WCAG 2.2, 1.4.1 Use of Color. Following the pairs does not prove contrast on its own; only measuring does.

Do

  • Pair each *-foreground with its own fill: text-card-foreground on bg-card, text-popover-foreground on bg-popover, text-primary-foreground on bg-primary. foreground and muted-foreground go on background, and muted-foreground also on muted.
  • Use the status colours as Justified tests them: text-destructive, text-success, text-warning and text-info as text on the page, and each status fill under its own *-foreground. An order in a plant nursery's shop that could not be charged reads "Payment failed" in text-destructive.
  • Edge every field with border-input: an Input, a custom select, a combobox trigger, a date picker button. Their edge is the only sign of where they are. Keep border-border for dividers and the edge of a Card, where nothing depends on seeing it.
  • Keep ring for the focus indicator that KBD-001 describes. Show a chosen plan or an active tab some other way, as VIS-003a says.
  • Keep bg-accent for items that are hovered, highlighted, pressed or selected, which is how shadcn's menus and Toggle use it. Give a static panel bg-muted or bg-card, so an item that turns bg-accent inside it still shows. Whether hover and press looks exist at all is VIS-008; when bg-accent draws both hover and selected, VIS-008b asks the selected item for a cue the hover does not use.
  • When the product needs a colour it does not have, such as a soft badge of bg-primary/10 with text-primary for Ships in 2 days, add it to the tokens with a name for its role and add the pair to the contrast test, so it is measured in both themes. This step is our own choice.
  • Give brand colours the same treatment: a token with a role and a tested pair, not a one-off class.
  • Treat an inverse surface, such as a dark tooltip in the light theme, as a pair like any other.

Don't

  • Use raw palette classes such as text-gray-500, bg-slate-100, text-blue-600 or border-gray-200, or a hex value in style. Such a colour has no role, so nothing says where it may go. It does not follow the theme, and nobody has tested it.
  • Put a foreground on someone else's fill: text-primary-foreground on bg-muted, text-muted-foreground on bg-primary, text-accent-foreground on bg-destructive. Each is tested only against its own partner.
  • Use text-destructive for a sale price or a New label, bg-success as a brand accent, or text-warning for a heading. DLG-005 keeps the destructive button for actions that destroy something.
  • Mark a selected card with border-ring or ring-2 ring-ring. Selection then looks like focus.
  • Draw an Input override, a custom select or a combobox trigger with border-border. It measures about 1.4:1 against the light page and 1.5:1 against the dark one, made for a divider, not for the only edge of a control; VIS-004 gives the law's side.
  • Mix systems in one pair, as in bg-card dark:bg-zinc-900. The pair now depends on two palettes, and the test sees only one.
  • Reuse chart colours for status, or status colours for chart series in a chart that is not about status. shadcn's --chart-1 to --chart-5 are a role of their own; Justified does not define or test them.

Exceptions

  • Colours that carry nothing. An illustration or a photo is outside the rule. A colour that people read, or that marks a control, a state or a boundary, is inside it.
  • Text over a photo or video. Text in text-white or text-black on a photo or a video, laid on a scrim such as bg-black/60, may keep that colour. No token is made to pair with a picture, and the picture looks the same in both themes. VIS-001 asks for the text to be measured against the lightest spot behind it. This is our own choice.
  • Loading signs. A spinner, such as shadcn's Spinner, counts as an icon. Beside a word that says what is happening it only decorates (STR-005), so its colour carries nothing; as the only sign of a wait, it takes a token like any icon that carries meaning. This is our own choice.
  • Neighbouring rules. The contrast a pair must reach is VIS-001 for text and VIS-004 for borders and graphics. The second cue next to a colour is VIS-003a. The focus ring itself is KBD-001. Hover and press looks are VIS-008, and a lasting state kept apart from them is VIS-008b.

Verdict

Unanimous (3 of 3). Only the judges decide; the expert witnesses are shown but do not vote. How verdicts work

Stances on VIS-002
SourceRoleStancePage
WCAG 2.2LawSupports1.4.3 Contrast (Minimum)
WCAG 2.2LawSupports1.4.11 Non-text Contrast
WCAG 2.2LawSupports1.4.1 Use of Color
Material DesignJudge SupportsColor roles
Badges: accessibility
Apple Human Interface GuidelinesJudge SupportsColor
Carbon Design SystemJudge SupportsColor
GOV.UK Design SystemExpert witness SupportsColour
Nielsen Norman GroupExpert witness No position

Each stance links to the page it rests on, read on the date in the source register. This page never repeats what a source says; read the source itself.

History

  • Version 1.1.0, : Two new exceptions, both our own choice, so the rule agrees with VIS-001 and the lint check. White or black text on a scrim over a photo or video may keep that colour, and a spinner counts as an icon. Points to VIS-008 and VIS-008b for hover, press and selected looks.
  • Version 1.0.0, : First version.

Last checked against the other rules for conflicts on .

More in Colour and motion: 11 rules and 0 guidelines · All rules