bootstrap icon indicating copy to clipboard operation
bootstrap copied to clipboard

Accessibility Improvement: Close buttons to use visually-hidden text instead of aria-labels

Open coliff opened this issue 5 months ago • 3 comments

Description

One thing I always have in my mind when building accessible components is 'the better this works without any CSS the better', (and 'the better this works without JavaScript the better') so from time to time I disable one or the other and see if any improvements can be made.

With that in mind, when working on a standard Bootstrap alert (with dismiss button) from the example code I noticed it appears like this with CSS disabled:

Image

Because the .btn-close is like this:

<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>

I think this could be improved with the following:

<button type="button" class="btn-close" data-bs-dismiss="alert">
  <span class="visually-hidden">Close</span>
</button>

So it appears like this:

Image

Motivation & Context

It does mean a few more characters/bytes of markup added but I believe it's worth it. it makes the content more resilient and usable even if CSS for any reason fails to load.

Also, aria-labels are not always auto-translated for users reading the content in a different language, but using a visually hidden text label would be translated - another potential accessibility benefit. REF: https://adrianroselli.com/2019/11/aria-label-does-not-translate.html

Type of changes

  • [x] Bug fix (non-breaking change which fixes an issue)
  • [ ] New feature (non-breaking change which adds functionality)
  • [ ] Refactoring (non-breaking change)
  • [ ] Breaking change (fix or feature that would change existing functionality)

Checklist

  • [x] I have read the contributing guidelines
  • [ ] My code follows the code style of the project (using npm run lint)
  • [ ] My change introduces changes to the documentation
  • [ ] I have updated the documentation accordingly
  • [ ] I have added tests to cover my changes
  • [ ] All new and existing tests passed

Live previews

Related issues

Fixes: #41580

coliff avatar Jul 04 '25 06:07 coliff

The Translate scenario isn't necessarily translating from English though, the source of the site could be anything, like Ukrainian or Vietnamese (for example)... and it's not just Google Translate, all modern browsers auto-translate content now... and Safari doesn't auto-translate aria-labels for those two example languages.

I know this is a very minor thing overall, but having the choice between those two options I think using visually hidden text makes the content more accessible and don't see an advantage in NOT making the change.

coliff avatar Jul 04 '25 13:07 coliff

was just reading @ericwbailey post 'aria-label is a code smell' and reminded me to revisit this pull request.

There's plenty of reasons why a text label is preferable. https://ericwbailey.website/published/aria-label-is-a-code-smell/

coliff avatar Nov 20 '25 06:11 coliff

I remain unconvinced that our use of aria-label in cases of graphical controls should be replaced by visually hidden text, I'm afraid (with all due respect for @ericwbailey)

patrickhlauke avatar Nov 26 '25 11:11 patrickhlauke