Tailwind CSS

tailwind/sr-only-label

The only text of a button or link is never hidden with hidden; use sr-only (with md:not-sr-only to show it on wider screens).

seriousstaticWCAG 4.1.2 · Afe-tailwind

Why

hidden is display: none, which removes the text from the accessibility tree as well as from the screen. In <button><svg/><span class="hidden md:inline">Menu</span></button> the button has no name at all on phones: a screen reader announces only "button", and voice control users cannot say its name.

Fix

Use sr-only for text that should be read but not seen, and md:not-sr-only where it should also be seen on wider screens: <span class="sr-only md:not-sr-only">Menu</span>. Alternatively give the control aria-label. Mark the icon itself aria-hidden="true".

Examples

<!-- bad -->
<button type="button"><svg aria-hidden="true">…</svg><span class="hidden md:inline">Menu</span></button>
<!-- good -->
<button type="button"><svg aria-hidden="true">…</svg><span class="sr-only md:not-sr-only">Menu</span></button>

Sources

Facts

Id
tailwind/sr-only-label
Severity
serious
Check
static checked in the source by audit
Applies to
html angular vue svelte astro jsx
Only in projects with
tailwind
WCAG 2.2
4.1.2, level A
Same problem in
axe-core button-name, axe-core link-name
Taught by the skill
fe-tailwind Tailwind CSS
Since version
0.1.0

Rule file on GitHub

Read it in the terminal

npx github:dawidolko/frontend-skills explain tailwind/sr-only-label