Sub topic 15
Reordering Items with order, and Its Accessibility Cost
This chapter assumes you can already create a flex container, set flex-direction, and read the main axis and cross axis. It covers the order property, how flex items are sorted by it, and why changing the visual order can harm keyboard and screen reader users.
What order does
Every flex item has an order value. The default is 0. The browser lays out items in ascending order, and items with equal values keep their source order.
order takes an integer. Negative values are allowed. Fractions and keywords are not valid.
.row {
display: flex;
gap: 1rem;
}
.row .first-in-html {
order: 2;
}
.row .second-in-html {
order: 1;
}
.row .third-in-html {
order: 0;
}With this CSS, the third item is drawn first, the second item next, and the first item last. The HTML itself is unchanged.
The property only applies to children of a flex container (and of a grid container). On other elements it has no effect.
How the sorting works
The browser uses a stable sort, which means ties keep their original order. Take these values:
| Source position | order value | Visual position |
|---|---|---|
| 1 | 0 | 2 |
| 2 | 0 | 3 |
| 3 | -1 | 1 |
| 4 | 1 | 4 |
Item 3 comes first because -1 is lower than 0. Items 1 and 2 tie at 0, so item 1 stays ahead of item 2. Item 4 comes last because 1 is higher than 0.
A common idiom is to move a single item to the start with order: -1 or to the end with order: 1. Both work without touching any other item, because all the others stay at 0.
.toolbar {
display: flex;
gap: 0.5rem;
}
.toolbar .help {
order: 1;
}
.toolbar .search {
order: -1;
}Here .search moves to the start and .help moves to the end. Everything else stays in source order between them.
Interaction with flex-direction
order works along the main axis. With flex-direction: row, items are placed left to right (in a left-to-right language) in order sequence. With flex-direction: column, they are placed top to bottom.
flex-direction: row-reverse and column-reverse do not change order values. They reverse the final sequence. If you combine them, the items are sorted by order first, and the whole sequence is then flipped. This is easy to get wrong, so avoid mixing the two on the same container unless you have a clear reason.
Interaction with flex-wrap
With flex-wrap: wrap, the browser sorts all items by order first. It then breaks the sorted sequence into lines. An item with a high order therefore lands on the last line, not in a particular column.
Source order versus visual order
The key idea is that order changes only the painted layout. It does not change the DOM, the accessibility tree, or the default tab order. Two orders now exist:
- Source order: the sequence of elements in the HTML.
- Visual order: the sequence in which the browser draws the items.
Many things follow the source order, not the visual order:
- Keyboard focus moves through links, buttons, and form fields in source order.
- Screen readers read content in source order.
- Search engines and other tools that parse the document read it in source order.
- Text selection and reading modes may follow the source order.
When the two orders differ, a keyboard user pressing Tab sees the focus ring jump around the page. A screen reader user hears the content in a sequence that does not match what a sighted user sees. This breaks WCAG success criteria 1.3.2 (Meaningful Sequence) and 2.4.3 (Focus Order) when the order carries meaning.
The diagram shows A, B, and C in the HTML. Because C has the lowest order, it is drawn first. Tab still visits A, then B, then C.
When reordering is a problem
Ask whether the order carries meaning. If it does, a mismatch hurts users.
| Situation | Risk |
|---|---|
| Form fields reordered visually | Focus jumps between fields, and users fill them in the wrong sequence |
| Steps in a list or checkout flow | Screen readers announce steps out of sequence |
| A heading moved below its content | Screen reader users hear the content before its heading |
| Decorative items such as icons or badges | Usually harmless |
| Layout-only wrappers with no sequence | Usually harmless |
A button placed first visually but last in the DOM is a typical case. Sighted keyboard users see focus jump from the second control to the last one and then back to the start.
Better alternatives
Prefer to fix the order in the HTML. If the visual order is the correct reading order, the markup should match it.
For responsive layouts, a different tool often fits better:
- Write the HTML in the order you want read. Then adjust only the visuals, such as spacing and alignment.
- Use
flex-direction: row-reverseonly when the content is truly symmetric and the order does not carry meaning. - Use CSS Grid with named areas when the layout changes a lot across breakpoints. Grid reordering has the same accessibility caveat, but the layout intent is clearer.
- Where you need two different orders for two screen sizes, consider two separate elements and hide one with
display: noneat each breakpoint. Hidden content withdisplay: noneis removed from the accessibility tree, so users do not hear it twice.
A safe use of order
Reordering is acceptable when the items are interchangeable or purely visual. Here is a media object where the image is on the right on wide screens but sits above the text on narrow ones. The text stays first in the HTML, and the image is moved visually.
.media {
display: flex;
flex-direction: column;
gap: 1rem;
}
.media .picture {
order: -1;
}
@media (min-width: 40rem) {
.media {
flex-direction: row;
}
.media .picture {
order: 1;
}
}An image that is decorative does not change what a screen reader user needs to understand. If the image carries essential content, give it an alt attribute, and keep it near the text it illustrates in the HTML.
Testing for the problem
You can find order mismatches in a few minutes:
- Load the page and press Tab repeatedly. Watch the focus ring. It should move in a predictable direction, such as left to right and top to bottom.
- Turn on a screen reader and move through the page, or open the browser's accessibility tree view and compare its sequence with the screen.
- Temporarily remove the stylesheet. The page then shows the source order. If it reads sensibly, the order is probably safe.
- Search your CSS for
order:and forrow-reverseandcolumn-reverse. Review each hit.
Summary of rules
- The default
orderis0. Lower values come first. Ties keep source order. orderchanges painting, not the DOM, the tab order, or the reading order.- Use it for decorative or interchangeable items, not for content with a sequence.
- Fix the HTML first. Reach for
orderlast.
Exercise
A page has three links in this HTML order: Home, Products, Contact. A developer sets order: 1 on Home and leaves the other two at 0. In what visual order do the links appear, and in what order does the Tab key visit them? Is this a problem for the navigation?
Show answer
Visually the links appear as Products, Contact, Home, because Home has the highest order value and the other two tie at 0, keeping source order. The Tab key visits them in source order: Home, Products, Contact. The focus ring therefore jumps from the left end to the right end and back, which is a problem because the navigation order carries meaning. The fix is to put Home last in the HTML, or to leave it first visually.
Exercise
A container has flex-direction: row-reverse and three items with source positions 1, 2, 3 and all order values at 0. What is the visual sequence from left to right in a left-to-right language? What would change if item 1 had order: 1?
Show answer
With all values at 0, the sorted sequence is 1, 2, 3, and row-reverse flips it, so from left to right you see 3, 2, 1. If item 1 has order: 1, the sorted sequence becomes 2, 3, 1, and flipping it gives 1, 3, 2 from left to right.