Tailwind CSS

Lesson 09

Accessibility: contrast and focus

Review Tailwind styling for visible focus, readable contrast, and accessible labels on icon-only controls.

Good Code

CopyLinkButton.tsx
export function CopyLinkButton() {
  return (
    <button
      type="button"
      className="inline-flex h-9 w-9 items-center justify-center rounded-md border border-slate-300 bg-white text-slate-700 hover:bg-slate-50 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-sky-500 focus-visible:ring-offset-2"
    >
      <LinkIcon className="h-4 w-4" aria-hidden="true" />
      <span className="sr-only">Copy lesson link</span>
    </button>
  );
}

Bad Code

CopyLinkButton.tsx
export function CopyLinkButton() {
  return (
    <button className="h-9 w-9 rounded-md bg-slate-200 text-slate-300 outline-none hover:bg-slate-300">
      <LinkIcon className="mx-auto h-4 w-4" />
    </button>
  );
}

Review Notes

What to review

Good Code

The good version gives the icon-only button an accessible text label, adequate contrast, and a strong keyboard focus ring.

Bad Code

The bad version removes the outline, uses weak foreground contrast, and leaves the icon as the only name for the control.

Takeaways

  • Utility classes can make accessibility explicit when contrast, focus, and labels are part of the class list.