Sub topic 15
Reordering items with order, and its accessibility cost
This chapter assumes you can create a flex container, you know the main axis and cross axis, and you are comfortable with flex-direction and flex-wrap. The order property changes where an item is drawn. It does not change where the item sits in the HTML.
How order works
Every flex item has an order value. The default is 0. The browser sorts items by order from lowest to highest and lays them out in that sequence. Items with the same value keep their source order. Negative values are allowed, so order: -1 moves an item before all the default items.
.toolbar {
display: flex;
gap: 1rem;
}
.toolbar .search {
order: -1;
}
.toolbar .help {
order: 1;
}With HTML in the order .logo, .search, .help, .profile, the screen shows .search, .logo, .profile, .help. The two items left at 0 keep their source order. .search moves first because -1 is lowest. .help moves last because 1 is highest.
The accessibility cost
order changes only the visual layout. Several things still follow the source order:
- Keyboard focus. Pressing Tab moves through links and buttons in HTML order.
- Screen readers. They read the content in HTML order.
- Search and reader modes. These also use the source.
If you move a button visually to the start of a row, the focus ring jumps to a different place than the reader expects. Someone using a keyboard sees focus leap around the page. Someone using a screen reader hears content in a sequence that does not match what sighted users see. The WCAG success criteria on meaningful sequence (1.3.2) and focus order (2.4.3) cover this problem.
The same applies to flex-direction: row-reverse and column-reverse. They reverse the visual order but not the tab order.
When order is acceptable
Reordering is reasonable when the items are not interactive and their sequence carries no meaning. A grid of decorative tiles is one example. Another is a responsive layout where a sidebar moves below the main content on narrow screens, as long as the content is not a series of steps and the sidebar has no controls whose position matters.
When you do use it, test with the Tab key. If focus moves in a way that looks wrong, change the HTML instead.
Exercise
A flex container holds three links in the HTML order Home, About, Contact. You set order: -1 on Contact. What is the visual order, and what order does the Tab key follow?
Show answer
The visual order is Contact, Home, About. The Tab key still follows the source order: Home, About, Contact. This mismatch between visual and focus order is the accessibility cost of order. The fix is to place Contact first in the HTML.
Exercise
Two items both have order: 2, and an earlier item in the HTML has order: 0. How does the browser arrange the three?
Show answer
The item with order: 0 comes first. The two items with order: 2 follow it in their HTML order, because equal values keep their source order.