Sub topic 10
Keeping visual order and DOM order aligned
This chapter assumes you know basic HTML structure, CSS flexbox and grid, and the size, contrast, and position cues from earlier chapters. Those cues shape what sighted users see first. This chapter covers a second reader: the person who navigates by keyboard or screen reader and gets the page in a different order.
Two orders, one page
Every page has two sequences:
- Visual order is the order in which the layout shows elements on screen.
- DOM order is the order of elements in the HTML source. Tab key navigation, screen readers, and reading modes all follow it.
CSS can change the visual order without touching the DOM order. When the two differ, a keyboard user's focus jumps around the screen. A screen reader user hears content in a sequence that doesn't match what a sighted colleague describes.
What breaks the alignment
These CSS features reorder content visually and leave the DOM unchanged:
orderon flex or grid itemsflex-direction: row-reverseandcolumn-reverse- Explicit grid placement with
grid-area,grid-row, orgrid-column - Absolute positioning that moves an element far from its source position
All of these are useful. The problem appears only when they move an interactive or meaningful element past its neighbours.
A worked example
A dialog needs a prominent primary button on the left and a quieter secondary button after it. A common shortcut keeps the "natural" HTML and reverses it with CSS:
<div class="actions">
<button class="button button--secondary">Cancel</button>
<button class="button button--primary">Save</button>
</div>.actions {
display: flex;
flex-direction: row-reverse;
justify-content: flex-end;
gap: 0.75rem;
}On screen, Save appears first and Cancel second. But pressing Tab focuses Cancel first, and a screen reader announces Cancel first. The primary action, which you made visually dominant, comes second for these users.
The fix is to write the DOM in the order you want users to meet the content, and drop the reversal:
<div class="actions">
<button class="button button--primary">Save</button>
<button class="button button--secondary">Cancel</button>
</div>.actions {
display: flex;
gap: 0.75rem;
}Now the visual order, focus order, and reading order are the same.
When visual reordering is acceptable
Reordering is fine when it doesn't change the meaning of the sequence. Examples:
- Placing a decorative image above or below text with
order, when the image has no alt text or is not focusable. - Rearranging cards in a grid when each card is independent and the DOM already lists them in a logical sequence.
- Moving a sidebar to the left on wide screens, if the sidebar contains no links that readers need to reach before the main content.
Ask this question for each change: if someone read the page as a plain list in source order, would it still make sense, and would focus still move in a sensible path?
Fixing mismatches in practice
- Start with the DOM in the order of importance and reading flow. This also helps on small screens, where layouts become a single column in source order.
- Use CSS to arrange the layout, not to rescue a wrong source order.
- If you must reorder, move the HTML instead of adding
order. - Test with the Tab key alone. Focus should move in the same direction your eyes do, usually top to bottom and left to right in left-to-right languages.
- Turn off CSS in the browser's developer tools or a reading-mode view, and check that the content is still understandable.
Browsers are adding a CSS property called reading-flow that lets focus follow the visual order inside flex and grid containers. Support is still limited, so check current browser support before depending on it, and keep the DOM aligned as the baseline.
Where this fits in the standards
WCAG success criterion 1.3.2 (Meaningful Sequence) requires that the reading order preserves meaning. Criterion 2.4.3 (Focus Order) requires that focus moves in a sequence that preserves meaning and operation. A mismatch between visual and DOM order is a common way to fail both.
Exercise
A product card uses display: flex; flex-direction: column-reverse so the price appears above the product name on screen, although the name comes first in the HTML. The name is a link. Why is this a problem, and how would you fix it?
Show answer
Keyboard focus and screen readers follow the DOM, so they reach the name link before the price, while sighted users see the price first. The two sequences disagree. Fix it by moving the price before the name in the HTML and removing column-reverse, so the visual order and DOM order match.