Is the 'id' HTML attribute required for accessible buttons?

Hi all,

I was having troubles finding the answer to this online! I understand that the ā€˜idā€™ attribute uniquely identifies a component on a website/web app. In examples of ā€˜accessibleā€™ buttons, they always have the ā€˜idā€™ attribute, but Iā€™m currently struggling to explain to my dev team why it is required.

My assumption is that the ā€˜idā€™ may be used by assistive tech like ā€˜speech inputā€™ devices to identify what component the user is trying to operate, but without access to a device like this I canā€™t confirm! Our buttons donā€™t currently use the ā€˜idā€™ attribute but are announced as I would expect by screen-readers and donā€™t show any issues in tools like AXE.

Iā€™m familiar with the WCAG criteria ā€˜label in nameā€™ but it does not explain where the accessible/programmatic name actually comes from!

Many thanks,
Claudia

3 Likes

Specificly ā€˜idā€™ is not required. You can use any tag/attribute you want in hope they are unique.
We even introduced a specific element ā€˜testidā€™ which is unique and only available on our test environment. In order to do some easy UI checks.

Thatā€™s okay and sometimes normal, I want to say itā€™s not a standard use in Angular but Iā€™m not sure anymore, itā€™s been to long to tell :stuck_out_tongue:

This is also not always true. Iā€™ve had many instances where an ID was found multiple times. Which was expected behavior.

2 Likes

The HTML specification states that ā€œidā€ attributes must be unique. See HTML Standard

Depending on what an ā€œidā€ attribute is used for, the page may still work ok if it is not unique. For instance, if you are using it as a CSS selector (although you ought to use a ā€œclassā€ if you want to target multiple elements).

We usually tolerate duplicate ā€œidā€ attributes if only one instance is displayed at any time and all the others are hidden using ā€œdisplay:noneā€. This is quite common with navigation menus if there is separate code for the desktop and mobile menus. Obviously, the two menus would never be displayed at the same time.

To answer the original question, buttons do not need ā€œidā€ attributes for accessibility, but they may well have ā€œidā€ attributes for other reasons. For instance, if you want to target the button using JavaScript to disable it under certain circumstances. Also, ā€œidā€ attributes will be needed for test automation.

Label in Name
The easiest way to find the accessible name is to look in the Accessibility tab for the element in the DOM Inspector. If you are familiar with the Accessible Name Computation - see https://w3c.github.io/accname/ - you can work out the accessible name by reading the code. There is a hierarchy, for instance an ā€œaria-labelā€ attribute overrides the elementā€™s contents, which overrides the ā€œtitleā€ attribute, but itā€™s way more complicated than that. The DOM Inspector does that calculation for you.

1 Like

Iā€™m no pro at this stuff, Kris and Steve seem to have it covered. But I suspect that many other folk may have some further hints on how to be more confident. If you have a MOT Pro subscription I could point you to some of the articles and recorded talks over in the members area for inspiration ways to test this Claudia .

Iā€™ve stolen some references from these talks for you here

Iā€™m not trying to sell it, but this is the best content the web has even if it is behind a paywall, get your boss to sign you up. Iā€™m not a salesperson, I just love how the club is where the gold lies for me.

1 Like

Screen readers rely on labels to describe things so any button which is unlabelled may announce what is on the button, e.g. submit, but not that it is a button or what will happen when it is activated.
This falls under * 2.4.6 Headings and Labels: Headings and labels describe topic or purpose. (Level AA).
Good info here with an example of how to inspect in dev tools.
https://www.sarasoueidan.com/blog/accessible-icon-buttons/#accessible-button-names
You should also check your buttons have distinctive shapes, high colour contrast in all states, hover, keyboard focus, activated, and clear labels.

1 Like

Hi @claudia.davey Great question! Firstly, I believe WCAG refers to test inside the button and, especially if button is an icon, aria-label attribute. This is because the screen readers would use that exact data to read the screen. Therefore, your tests should also rely on the same exact information to refer to the button (including relative locations/indexes if the button is not unique). It is most easily done with the tools like testRigor where you can write instructions like click "Button text" or click "whatever is in aria-label". Dislaimer: Iā€™m associated with testRigor.

1 Like

Screen readers announce the name that results from the Accessible Name Computation I mentioned above. The best way to label a button is to provide text contents, in which case the visual label is the same as the accessible name.

Although you can use the ā€œaria-labelā€ attribute to label buttons, it should not be your first choice. The ARIA specification itself states that ā€œThe first rule of ARIA is donā€™t use ARIAā€. If you can achieve the required result without ARIA, then do so.

In the case of a button with an icon, use text that is hidden using the ā€œclipā€ technique. One of the benefits of this approach is that the text label is displayed instead of the icon if the CSS fails to load for any reason. If you used an ā€œaria-labelā€ attribute, your button would have no visible label if that happened.

One of the risks of using an ā€œaria-labelā€ attirbute is that it is given a value that does not match or contain the visible text label, in which case you fail WCAG SC 2.5.2 (Label In Name).

You are mixing up the role and the label. If a button is unlabelled, it will still be announced as a button because that is its role.

If a button has the visible label ā€œSubmitā€, then that will also be its accessible name unless it is an image of text (which would be crazy) or if it is overridden by something with higher priority in the Accessible Name Computation, such as an ā€œaria-labelā€ attribute. For the button to be unlabelled, it would have to have an empty ā€œaria-labelā€ attribute or an ā€œaria-labelledbyā€ attribute that points to an empty string.

1 Like