KodingKaro
CSS · Advanced · Accessibility and CSS

Focus, contrast, and non-color cues

CSS directly affects whether people can locate text and controls. Visual design should make interaction and status perceivable in more than one way.

✓ What you will learn in this topic

  • Explain the key idea: Preserve visible focus and communicate important states with more than color.
  • Apply the concept: Keyboard focus is outlined clearly, while an error state has both color and a visible structural cue.
  • Avoid this common mistake:

Concept explanation

Maintain adequate text and control contrast. Provide a clear :focus-visible style and do not rely on color alone to indicate success, error, or selection. Use patterns, text, or icons with accessible names when an additional cue is needed.

Test hover and focus independently. A custom outline should not be clipped by an ancestor’s overflow, and its color should remain visible on every surface.

Syntax

A CSS rule has a selector followed by declarations. Each declaration is a property and a value; the braces group the declarations that apply to matching elements.

.demo a:focus-visible {
  outline: 3px solid #d97706;
  outline-offset: 3px;
}

Example 1: a small page element

Use this HTML as the target and place the CSS in a stylesheet linked to the page, or inside a <style> element while experimenting.

<main class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></main>
.demo a:focus-visible {
  outline: 3px solid #d97706;
  outline-offset: 3px;
}

Expected result: Keyboard focus is outlined clearly, while an error state has both color and a visible structural cue.

Example 2: a practical interface card

The same idea can style a reusable card on a course page. The class selector keeps the rule attached to this component rather than changing every element of that type.

<section class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></section>
.card button:focus-visible {
  outline: 3px solid #f59e0b;
  outline-offset: 3px;
}
.card .error { border-inline-start: 0.25rem solid #b91c1c; }

Expected result: The course update reads as a distinct interface card, and its content follows the spacing, color, and layout choices in the rule.

Common mistake

Using display:none to hide information that a user still needs, or removing focus indicators for visual neatness.

Best practice

Test keyboard interaction, contrast, zoom, forced colors, and reduced motion; keep meaning in semantic HTML.

Practice exercise

Audit a card interface for visible focus, text contrast, zoom behavior, and states that are not conveyed by color alone. Apply it to the example above and inspect the result at a narrow and a wide viewport.

Section summary

  • Preserve visible focus and communicate important states with more than color.
  • Test keyboard interaction, contrast, zoom, forced colors, and reduced motion; keep meaning in semantic HTML.

Knowledge check

Why should focus not be removed without a replacement?

Reveal the explanation

A clear indicator communicates where keyboard input will go next.

Finish the module lessons, then take the server-scored module quiz to check your understanding.

Practical example

css
.demo a:focus-visible {
  outline: 3px solid #d97706;
  outline-offset: 3px;
}

Expected result:

Keyboard focus is outlined clearly, while an error state has both color and a visible structural cue.

Hands-on practice task

Audit a card interface for visible focus, text contrast, zoom behavior, and states that are not conveyed by color alone.

In this section

Other lessons in Accessibility and CSS

Broaden your knowledge

Related learning topics