Max Design

Published:

A quick explanation of aria-pressed - including an example, when it can be used, and when it should not be used.

An example to set the scene

Imagine a table listing a set of servers. At the right, each server has a button that lets you switch the server "on", or back to the default state of "off".

Above these server buttons, there is an overall button that allows you to switch all servers "on" or "off".

The image below shows all buttons in the default state of "off".

All four buttons stacked on top of each other in the off state so they are grey

The image below shows all buttons in the new state of "on".

All four buttons stacked on top of each other in the on state so they are green

But what happens if you switch Server 1 from "off" to "on"?

The overall button would change to a new state between "all off" and "all on", called a mixed state.

For this example, the overall button has been coloured orange to help flag this mixed state.

Overall button at top is orange to indicate mixed state, second button is green to indicate on, other buttons are grey to indicate off

If this overall button is activated again, all child buttons would be immediately turned off, so they all return to the same state.

This is a deliberately detailed example to show the "mixed" state in action. The full working example also explores some of the design and UX issues involved with this type of control.

But how can we inform assistive technologies users of these different states?

The aria-pressed attribute

aria-pressed tells assistive technologies whether a toggle button is currently pressed. It accepts three values: true, false, or mixed.

These values can be changed dynamically using JavaScript based on user input.

In our example, the overall button would initially be:

<button type="button" aria-pressed="false">
  Turn all servers on
</button>

When activated, this overall button would change to:

<button type="button" aria-pressed="true">
  Turn all servers off
</button>

If a child button were to be activated, the overall button would change to:

<button type="button" aria-pressed="mixed">
  Turn all servers off
</button>

Which controls can use aria-pressed?

  1. Native buttons: HTML alone doesn’t provide pressed or mixed states.
  2. Custom controls: a <div> can be used as a toggle button.

Be aware that custom controls require additional accessibility features. They need to:

When to use aria-pressed

When not to use aria-pressed

Takeaway

aria-pressed provides a clear way to communicate the current state of toggle buttons, including when that state is mixed.

Test page

Testing aria-pressed