# Understanding Focus Order

## In brief

Goal
  Keyboard users navigate content in a correct order.
What to do
  Elements receive focus in an order that preserves meaning.
Why it's important
  Navigating a site or application with only the keyboard will make sense.

## Intent of Focus Order

The intent of this Success Criterion is to ensure that when users navigate sequentially through content, they encounter information in an order that is consistent with the meaning of the content and can be operated from the keyboard. This reduces confusion by letting users form a consistent mental model of the content. There may be different orders that reflect logical relationships in the content. For example, moving through components in a table one row at a time or one column at a time both reflect the logical relationships in the content. Either order may satisfy this Success Criterion.

The way that sequential navigation order is determined in Web content is defined by the technology of the content. For example, simple HTML defines sequential navigation via the notion of tabbing order. Dynamic HTML may modify the navigation sequence using scripting along with the addition of a `tabindex` attribute to allow focus to additional elements. If no scripting or `tabindex` attributes are used, the navigation order is the order that components appear in the content stream. (See Focus in the HTML Living Standard).

An example of keyboard navigation that is not the sequential navigation addressed by this Success Criterion is using arrow key navigation to traverse a tree component. The user can use the up and down arrow keys to move from tree node to tree node. Pressing the right arrow key may expand a node, then using the down arrow key, will move into the newly expanded nodes. This navigation sequence follows the expected sequence for a tree control - as additional items get expanded or collapsed, they are added or removed from the navigation sequence.

The focus order may not be identical to the programmatically determined reading order (see Success Criterion 1.3.2) as long as the user can still understand and operate the Web page. Since there may be several possible logical reading orders for the content, the focus order may match any of them. However, when the order of a particular presentation differs from the programmatically determined reading order, users of one of these presentations may find it difficult to understand or operate the Web page. Authors should carefully consider all these users as they design their Web pages.

For example, a screen reader user interacts with the programmatically determined reading order, while a sighted keyboard user interacts with the visual presentation of the Web page. Care should be taken so that the focus order makes sense to both of these sets of users and does not appear to either of them to jump around randomly.

For clarity:

- Focusable components need to receive focus in an order that preserves meaning and operability only when navigation sequences affect meaning and operability.
- In those cases where it is required, there may be more than one order that will preserve meaning and operability.
- If there is more than one order that preserves meaning and operability, only one of them needs to be provided.

## Benefits of Focus Order

These techniques benefit keyboard users who navigate documents sequentially and expect the focus order to be consistent with the sequential reading order.

- People with mobility impairments who must rely on keyboard access for operating a page benefit from a logical, usable focus order.
- People with disabilities that make reading difficult can become disoriented when tabbing takes focus someplace unexpected. They benefit from a logical focus order.
- People with visual impairments can become disoriented when tabbing takes focus someplace unexpected or when they cannot easily find the content surrounding an interactive element.
- Only a small portion of the page may be visible to an individual using a screen magnifier at a high level of magnification. Such a user may interpret a field in the wrong context if the focus order is not logical.

## Examples of Focus Order

- On a web page that contains a tree of interactive controls, the user can use the up and down arrow keys to move from tree node to tree node. Pressing the right arrow key expands a node, then using the down arrow key moves into the newly expanded nodes.
- A Web page implements modeless dialogs via scripting. When the trigger button is activated, a dialog opens. The interactive elements in the dialog are inserted in the focus order immediately after the button. When the dialog is open, the focus order goes from the button to the elements of the dialog, then to the interactive element following the button. When the dialog is closed, the focus order goes from the button to the following element.
- A Web page implements modal dialogs via scripting. When the trigger button is activated, a dialog opens and focus is set within the dialog. As long as the dialog is open, focus is limited to the elements of the dialog. When the dialog is dismissed, focus returns to the button or the element following the button.

- An HTML Web page is created with the left hand navigation occurring in the HTML after the main body content, and styled with CSS to appear on the left hand side of the page. This is done to allow focus to move to the main body content first without requiring `tabindex` attributes or JavaScript.

  While this example passes the Success Criterion, it is not necessarily true that all CSS positioning would. More complex positioning examples may or may not preserve meaning and operability

- The following example fails to meet the Success Criterion:

  A company's Web site includes a form that collects marketing data and allows users to subscribe to several newsletters published by the company. The section of the form for collecting marketing data includes fields such as name, street address, city, state or province, and postal code. Another section of the form includes several checkboxes so that users can indicate newsletters they want to receive. However, the tab order for the form skips between fields in different sections of the form, so that focus moves from the name field to a checkbox, then to the street address, then to another checkbox.

## Resources for Focus Order

## Techniques for Focus Order

### Sufficient Techniques for Focus Order

- Placing the interactive elements in an order that follows sequences and relationships within the content

- Giving focus to elements in an order that follows sequences and relationships within the content using one of the following techniques:

  - Making the DOM order match the visual order

- Changing a Web page dynamically using one of the following techniques:

  - Inserting dynamic content into the Document Object Model immediately following its trigger element
  - Creating modal dialogs with the HTML dialog element
  - Reordering page sections using the Document Object Model

### Additional Techniques (Advisory) for Focus Order

### Failures for Focus Order

- Failure of 2.4.3 due to using `tabindex` to create a tab order that does not preserve meaning and operability
- Failure of Success Criterion 2.4.3 due to using dialogs or menus that are not adjacent to their trigger control in the sequential navigation order