Accessible Buttons
A Button is an interactive element that cause an action on a page or submit a form.
For a long time now, we have had the <button>
tag. Developers should use these
for this purpose.
This may seem obvious to many developers.
Unfortunately there is a lot of code out there that uses <a>
tags
to do the work of a <button>
.
Marking them up this way is an accessibility issue, since screen reader users will assume that pressing it
will go to another page. When I review code like this, it makes me sad. If you do this in a new project,
think about how sad I will be, and change your ways.
This page covers how to make buttons in three ways. Review all scenarios and decide which is best for your use-case (but read the disclaimers made before you make that decision).
An HTML Button.
The most bulletproof way to make a button. It "just works" for everyone.
The following is a
<button>
tag with a
<label>
tag describing what it is for.
Code Walkthrough of the Above Example
Below is the HTML of the above example. Use the dropdown to highlight each of the individual steps that makes the example accessible.
A link with the role of button
I can't tell you how many times I have seen buttons marked up as links on a project.
When the project is an older one, and it would take a long time to refactor the existing
functionality to change the "links that are actually buttons" to <button>
tags, it makes sense to add the ARIA role="button"
to the existing
<a>
. If you decide to do this, you should first review all the steps below
and see what would be more work: refactoring the code to use real HTML buttons, or to add the extra
JS to the codebase to make the "faux buttons" accessible.
Code Walkthrough of the Above Example
Below is the HTML of the above example. Use the dropdown to highlight each of the individual steps that makes the example accessible.
A DIV with a role of button
There have been a few projects (usually, in my experience, in ones done in React and Angular for some
reason) where I have seen
developers use <div>
tags to make buttons.
This hurts my brain. It goes against the ideas of semantic HTML and it makes Tim Berners-Lee cry. Do you really want to make the Father of the Web cry? What kind of monster are you?
If you have an existing project that has code built this way, read the steps below and
determine if it would be less work to do this instead of using real HTML <button>
tags.
Code Walkthrough of the Above Example
Below is the HTML of the above example. Use the dropdown to highlight each of the individual steps that makes the example accessible.
Disabled HTML Button.
There are two ways of making a button disabled.
- Use the
disabled
attribute. This removes the button from the keyboard tabbing order. It also doesn't prevents click events from being fired. -
Use
aria-disabled="true"
attribute. This doesn't remove the button from the keyboard tabbing order. It also doesn't prevents click events from being fired except for Chrome on Google Android with Talkback on (Thanks to Noel Tibbles for pointing this out).
The following is disabled with the disabled
attribute
THe following is disabled with aria-disabled="true"