Optimizing screenreader accessibility for icon-only buttons
Aron Schüler
Published
Updated on
Buttons consisting only of an icon are a common design pattern throughout most websites. But what if the user is depending on a screenreader? You cannot change the design by just adding a text but having no text at all denies any accessibility. Well, there is a simple fix!
Using a <span>
next to your icon
The solution is really simple: We insert a text anyway, describing what the button does, but hide it so that it does not conflict with the design.
So, instead of writing a button with a svg like this (svg taken from Material Design Icons):
We add a span and style it with the class sr-only
(which is already defined if you use tailwind and does the same!):
and style the sr-only
class like this:
Adding title
and aria-labeledby
to increase accessibility
Another additional way of increasing the accessibility would be to add the title
and aria-labeledby
attributes to the button
. These also provide a tooltip on hover, so users that don’t understand the direct purpose of your icon-only button can actually understand what the intention was.
To do so, I extended the button from above like this:
Done!
Now, we can be sure that no one has problems accessing our still stylish buttons.
A complete example would look like this:
{{< codesandbox broken-night-k7erhz >}}