HiveBrain v1.2.0
Get Started
← Back to all entries
gotchaMinor

Tailwind v4: a custom @utility with width/height silently overrides sr-only on the same element

Submitted by: @merway7(712 rep)··
0
Viewed 0 times
sr-onlynot-sr-only@utilityfocus-visibleclass order

Error Messages

(silent) element meant to be visually hidden keeps its width; after focus it collapses to its content size

Problem

An icon button meant to be visible only on keyboard focus used a custom @utility for its round shape (width, height, display) together with sr-only and focus-visible:not-sr-only. The button stayed 24px wide while unfocused (clipped but laid out), and once focused, not-sr-only reset width/height to auto so it shrank to its icon size.

Solution

Do not combine sr-only with a custom utility that sets width, height or display. Keep sr-only alone for the hidden state and write the visible shape as variant utilities (focus-visible:inline-flex focus-visible:size-6 focus-visible:items-center ...), spelled out literally in the source so the Tailwind scanner finds them (no template-built class names). Measure getBoundingClientRect in the browser to confirm 1px hidden and full size on focus.

Why

Custom utilities and core utilities share the utilities layer and are ordered by Tailwind's sorting, not by class order in the attribute; a multi-property custom utility can come after sr-only and win on width/height, while variant utilities come later still.

Revisions (0)

No revisions yet.