Sub topic 13
Source order, visual order, and accessibility
This chapter assumes you can write a grid-template-areas layout and place items with grid-area, as covered in earlier chapters. It explains one side effect of those tools: they change what users see, but they do not change the order of the HTML.
Two orders, one page
A page has two orders:
- Source order is the order of elements in the HTML.
- Visual order is where the browser paints them.
Without Grid, the two match. With grid-area, you can place the last element in the first cell. Nothing in the HTML moves.
Keyboard focus, screen readers, and reading modes all follow source order. A sighted mouse user follows visual order. If the two disagree, a keyboard user pressing Tab can jump around the page in a way that looks random. A screen reader user hears content in an order that doesn't match what is on screen.
The mismatch in our layout
Our earlier HTML put aside before main. The mobile layout from the responsive-areas chapter then stacks them like this:
.page {
display: grid;
gap: 1rem;
grid-template-columns: 1fr;
grid-template-areas:
"header"
"main"
"sidebar"
"footer";
}Visually, main comes before the sidebar. In the HTML, the sidebar comes first. A screen reader reads the sidebar before the main content, and Tab reaches sidebar links first. This breaks WCAG success criteria 1.3.2 (Meaningful Sequence) and 2.4.3 (Focus Order) whenever the sequence affects meaning or focus moves illogically.
Fix it in the HTML first
Write the HTML in the order you want it read. Then use areas to move things visually only where it is safe.
<div class="page">
<header>Header</header>
<main>Main content</main>
<aside>Sidebar</aside>
<footer>Footer</footer>
</div>The mobile CSS above now matches the source. On wide screens, the same HTML can still show the sidebar on the left:
@media (min-width: 48rem) {
.page {
grid-template-columns: 220px 1fr;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}
}This leaves a small mismatch on desktop. The sidebar appears left of main, but Tab reaches main first. Ask whether that matters for your content:
- If the sidebar is supplementary, such as related links, reading it after
mainis correct. Consider placing it on the right with"main sidebar", so both orders match. - If the sidebar is primary navigation, put it before
mainin the HTML and accept that it stacks abovemainon mobile. - If you must keep the mismatch, add a skip link at the top of the page that jumps to
main.
Auto-fit grids have the same issue
Auto-fit card grids place items in source order, so they are usually safe. The risk appears when you add order or explicit placement to individual cards. A card that looks first but sits last in the HTML will be focused last.
Testing the order
- Put the cursor at the top of the page and press Tab repeatedly. Focus should move in a direction that matches the visual flow.
- Turn off CSS, for example in your browser's developer tools. The page should still read sensibly.
- Try a screen reader on one narrow and one wide layout, since areas can change between breakpoints.
Browsers are starting to ship reading-flow, a CSS property that lets focus order follow the grid's visual order. Support is not yet universal, so treat it as an enhancement, not a replacement for correct HTML order.
| Technique | Changes visual order | Changes focus and reading order |
|---|---|---|
| `grid-area` / template areas | Yes | No |
| `order` | Yes | No |
| Moving elements in the HTML | Yes | Yes |
Exercise
A page's HTML lists header, aside (a "related articles" list), main, footer. On mobile, the grid areas stack header, main, sidebar, footer. What accessibility problem does this cause, and what is the best fix?
Show answer
Visually the sidebar comes after main, but in the HTML it comes before main, so keyboard focus and screen readers reach the related-articles list first. Move the aside after main in the HTML. The mobile area order then matches the source. On wide screens, place the sidebar with "main sidebar" so the visual order also matches.