ARIA: button role
Thebutton role is for clickable elements that trigger a response when activated by the user. Addingrole="button" tells the screen reader the element is a button, but provides no button functionality. Use<button> or<input> withtype="button" instead.
In this article
Description
The button role identifies an element as a button to assistive technology such as screen readers. A button is a widget used to perform actions such as submitting a form, opening a dialog, canceling an action, or performing a command such as inserting a new record or displaying information. Addingrole="button" tells assistive technology that the element is a button but provides no button functionality. Use<button> or<input> withtype="button" instead.
Thisbutton role can be used in combination with thearia-pressed attribute tocreate toggle buttons.
<div tabindex="0" role="button" aria-pressed="false">Save</div>The above example creates a focusable button, but requires JavaScript and CSS to include button appearance and functionality. These are features provided by default when using the<button> and<input> withtype="button" elements:
<button type="button">Save</button>Note:If usingrole="button" instead of the semantic<button> or<input type="button"> elements, you will need to make the element focusable and define event handlers forclick andkeydown events. This includes handling theEnter andSpace key presses in order to process all forms of user input. Seethe official WAI-ARIA example code.
In addition to the ordinary button widget,role="button" should be included when creating a toggle button or menu button using a non-button element.
A toggle button is a two-state button that can be either off (not pressed) or on (pressed). Thearia-pressed attribute values oftrue orfalse identify a button as a toggle button.
A menu button is a button that controls a menu and has anaria-haspopup property attribute set to eithermenu ortrue.
All descendants are presentational
There are some types of user interface components that, when represented in a platform accessibility API, can only contain text. Accessibility APIs do not have a way of representing semantic elements contained in abutton. To deal with this limitation, browsers, automatically apply rolepresentation to all descendant elements of anybutton element as it is a role that does not support semantic children.
For example, consider the followingbutton element, which contains a heading.
<div role="button"><h3>Title of my button</h3></div>Because descendants ofbutton are presentational, the following code is equivalent:
<div role="button"><h3 role="presentation">Title of my button</h3></div>From the assistive technology user's perspective, the heading does not exist since the previous code snippets are equivalent to the following in theaccessibility tree:
<div role="button">Title of my button</div>Associated ARIA roles, states, and properties
aria-pressedThe
aria-pressedattribute defines the button as a toggle button. The value describes the state of the button. The values includearia-pressed="false"when a button is not currently pressed,aria-pressed="true"to indicate a button is currently pressed, andaria-pressed="mixed"if the button is considered to be partially pressed. If the attribute is omitted or set to its default value ofaria-pressed="undefined", the element does not support being pressed.aria-expandedIf the button controls a grouping of other elements, the
aria-expandedstate indicates whether the controlled grouping is currently expanded or collapsed. If the button hasaria-expanded="false"set, the grouping is not currently expanded. If the button hasaria-expanded="true"set, it is currently expanded; if the button hasaria-expanded="undefined"set or the attribute is omitted, it is not expandable.
Basic buttons
Buttons should always have an accessible name. For most buttons, this name will be the same as the text inside the button, between the opening and closing tags. In some cases, for example buttons represented by icons, the accessible name may be provided from thearia-label oraria-labelledby attributes.
Toggle buttons
A toggle button typically has two states: pressed and not pressed. A third mixed state is available for toggle buttons that control other elements, such as other toggle buttons or checkboxes, which do not all share the same value. Whether an element is a toggle button or not can be indicated with thearia-pressed attribute in addition to thebutton role (if the element is not already a native button element):
- If
aria-pressedis not used, or is set to the "undefined" state, the button is not a toggle button. - If
aria-pressed="false"is used the button is a toggle button that is currently not pressed. - If
aria-pressed="true"is used the button is a toggle button that is currently pressed. - if
aria-pressed="mixed"is used, the button is considered to be partially pressed.
As an example, the mute button on an audio player labeled "mute" could indicate that sound is muted by setting thearia-pressed state true. The label of a toggle button should not change when its state changes. In our example the label remains "Mute" with a screen reader reading "Mute toggle button pressed" or "Mute toggle button not pressed" depending on the value ofaria-pressed. If the design were to call for the button label to change from "Mute" to "Unmute," a toggle button would not be appropriate, so thearia-pressed attribute would be omitted.
Keyboard interactions
| Key | Function |
|---|---|
| Enter | Activates the button. |
| Space | Activates the button |
Following button activation, focus is set depending on the type of action the button performs. For example, if clicking the button opens a dialog, the focus should move to the dialog. If the button closes a dialog, focus should return to the button that opened the dialog unless the function performed in the dialog context logically leads to a different element. If the button alters the current context, such as muting and unmuting an audio file, then focus typically remains on the button.
Required JavaScript Features
Required event handlers
Buttons can be operated by mouse, touch, and keyboard users. For native HTML<button> elements, the button'sonclick event fires for mouse clicks and when the user pressesSpace orEnter while the button has focus. But if another tag is used to create a button, theonclick event only fires when clicked by the mouse cursor, even ifrole="button" is used. Because of this, separate key event handlers must be added to the element so that the button is be triggered when theSpace orEnter key is pressed.
onclickHandles the event raised when the button is activated using a mouse click or touch event.
onKeyDownHandles the event raised when the button is activated using the Enter or Space key on the keyboard. (Note not thedeprecated onKeyPress)
Examples
>Basic button example
In this example, a span element has been given thebutton role. Because a<span> element is used, thetabindex attribute is required to make the button focusable and part of the page's tab order. The included CSS style is provided to make the<span> element look like a button, and to provide visual cues when the button has focus.
ThehandleBtnClick andhandleBtnKeyDown event handlers perform the button's action when activated using a mouse click or theSpace orEnter key. In this case, the action is to add a new name to the list of names.
Try the example by adding a name to the text box. The button will cause the name to be added to a list.
HTML
<h1>ARIA Button Example</h1><ul></ul><label for="newName">Enter your Name: </label><input type="text" /><span role="button" tabindex="0">Add Name</span>CSS
[role="button"] { padding: 2px; background-color: navy; color: white; cursor: default;}[role="button"]:hover,[role="button"]:focus,[role="button"]:active { background-color: white; color: navy;}ul { list-style: none;}JavaScript
function handleCommand(event) { // Handles both mouse clicks and keyboard // activate with Enter or Space // Key presses other than Enter and Space should not trigger a command if ( event instanceof KeyboardEvent && event.key !== "Enter" && event.key !== " " ) { return; } // Get the new name value from the input element const newNameInput = document.getElementById("newName"); const name = newNameInput.value; newNameInput.value = ""; // clear the text field newNameInput.focus(); // give the text field focus to enable entering and additional name. // Don't add blank entries to the list. if (name.length > 0) { const listItem = document.createElement("li"); listItem.appendChild(document.createTextNode(name)); // Add the new name to the list. const list = document.getElementById("nameList"); list.appendChild(listItem); }}const btn = document.querySelector("span[role='button']");btn.addEventListener("click", handleCommand);btn.addEventListener("keydown", handleCommand);Toggle button example
In this snippet a<span> element is converted to a toggle button using thebutton role and thearia-pressed attribute. When the button is activated, thearia-pressed value switches states; changing fromtrue tofalse and back again.
HTML
<button type="button">Mute Audio</button><span role="button" tabindex="0" aria-pressed="false"> Mute Audio </span><audio src="https://soundbible.com/mp3/Tyrannosaurus%20Rex%20Roar-SoundBible.com-807702404.mp3"> Your browser does not support the `audio` element.</audio>CSS
button,[role="button"] { padding: 3px; border: 2px solid transparent;}button:active,button:focus,[role="button"][aria-pressed="true"] { border: 2px solid black;}JavaScript
function handleBtnClick(event) { toggleButton(event.target);}function handleBtnKeyDown(event) { // Check to see if space or enter were pressed // "Spacebar" for IE11 support if (event.key === " " || event.key === "Enter" || event.key === "Spacebar") { // Prevent the default action to stop scrolling when space is pressed event.preventDefault(); toggleButton(event.target); }}function toggleButton(element) { const audio = document.getElementById("audio"); // Check to see if the button is pressed const pressed = element.getAttribute("aria-pressed") === "true"; // Change aria-pressed to the opposite state element.setAttribute("aria-pressed", !pressed); // Toggle the play state of the audio file if (pressed) { audio.pause(); } else { audio.play(); }}const button = document.querySelector("button");const spanButton = document.querySelector("span[role='button']");button.addEventListener("click", handleBtnClick);button.addEventListener("keydown", handleBtnKeyDown);spanButton.addEventListener("click", handleBtnClick);spanButton.addEventListener("keydown", handleBtnKeyDown);Result
Accessibility concerns
Buttons are interactive controls and thus focusable. If thebutton role is added to an element that is not focusable by itself (such as<span>,<div> or<p>) then, thetabindex attribute has to be used to make the button focusable.
Warning:Be careful when marking up links with the button role. Buttons are expected to be triggered using theSpace orEnter key, while links are expected to be triggered using theEnter key. In other words, when links are used to behave like buttons, addingrole="button" alone is not sufficient. It will also be necessary to add a key event handler that listens for theSpace key in order to be consistent with native buttons.
When thebutton role is used, screen readers announce the element as a button, generally saying "click" followed by the button's accessible name. The accessible name is either the content of the element or the value of anaria-label or element referenced by anaria-labelledby attribute, or description, if included.
Best practices
If a link performs the action of a button, giving the elementrole="button" helps assistive technology users understand the function of the element. However, a better solution is to adjust the visual design so it matches the function and ARIA role. Where possible, it is recommended to use native HTML buttons (<button>,<input type="button">,<input type="submit">,<input type="reset"> and<input type="image">) rather than thebutton role, as native HTML buttons are supported by all user agents and assistive technology and provide keyboard and focus requirements by default, without need for additional customization.
Specifications
| Specification |
|---|
| Accessible Rich Internet Applications (WAI-ARIA)> # button> |
| Unknown specification> |