3 ms·
My understanding is that a button used like this is already accessible. However a button that looks like this: <button type="button"> <img src="/delete.
by d1sxeyes 3y ago
My understanding is that a button used like this is already accessible.
However a button that looks like this:
<button type="button">
<img src="/delete.png" alt="Delete post" />
</button>
Is not. Screen readers will read the simple text on the button, but any descendants of the button will be considered purely presentational (even if as in the example above the img tag includes an 'alt' attribute, it won't be read).
Modern screen readers will now often parse text content out of the button even if there are descendants, but this behaviour is not reliable or according to spec.
To fix:
<button type="button" aria-label="Delete post">
<img src="/delete.png" alt="Delete post" />
</button>
There are other techniques as well such as using CSS to hide text (not with 'display: none' though, which also hides from screen readers, meaning this often feels a bit hacky) or using a hidden element with text content and aria-labelledby to reference the element's ID.
This is a pretty good walk through: https://www.sarasoueidan.com/blog/accessible-icon-buttons/#accessible-button-names https://www.sarasoueidan.com/blog/accessible-icon-buttons/#a...
- extra88 3y agoNo, your first code snippet is totally fine. The button's name comes from its descendent elements by default. In this case, the `alt` attribute of the image provides the name. Sara is good source for accessibility information but I think you've misinterpreted that post. If an icon button uses an `img`, making the image's `alt` the name of the button works well. If the button's icon comes from an inline SVG, background image, or icon font, I recommend Sara's first technique, visually hidden text within the `button`; names from text nodes are more robust than from attributes (`alt` being an exception, in part because it has been around far longer).
- d1sxeyes 3y agoThank you for the correction! Always good to learn.