Sub topic 13
Source order, visual order, and accessibility
Grid areas let you place any element in any cell, regardless of where it sits in the HTML. That freedom has a cost. Keyboard focus, screen readers, and search-result snippets all follow the HTML source order, not the order you see on screen. This chapter explains where the two orders can diverge, how to spot problems, and how to design layouts that stay usable.
This chapter assumes you have read the earlier chapters on grid-template-areas, grid-area, and responsive area maps. You should also know the semantic elements header, main, aside, and footer, and have a basic idea of what a screen reader and keyboard tabbing do.
Two orders, one page
Every page has two sequences:
- Source order (also called DOM order) is the order of elements in the HTML.
- Visual order is the order in which elements appear on screen, which CSS can change.
Browsers use source order for several things:
- Tab and Shift+Tab move focus through links, buttons, and form fields in source order.
- Screen readers read content in source order, and their "next heading" or "next landmark" commands use it too.
- Text-only views, reader modes, and CSS-disabled rendering show source order.
Grid does not change any of that. It only moves the boxes. If the two orders disagree, a keyboard user watches focus jump around the screen, and a screen reader user hears content in a different sequence than a sighted user sees it.
WCAG, the Web Content Accessibility Guidelines, has two success criteria that apply here:
| Criterion | Requirement in plain terms |
|---|---|
| 1.3.2 Meaningful Sequence | If the order of content affects its meaning, the source order must give the same meaning. |
| 2.4.3 Focus Order | Focusable elements must receive focus in an order that preserves meaning and operability. |
Where grid can break the match
Three grid features can separate visual order from source order.
- Named areas and
grid-area. An element can be placed in any area, whatever its position in the HTML. - The
orderproperty. Grid items honororder, and a lower value paints earlier. It changes visual placement only. grid-auto-flow: dense. Dense packing back-fills gaps with later items, so they appear before earlier ones.
An auto-fit card grid with no explicit placement is safe. Items fill cells in source order, left to right and row by row, and wrapping is a natural reading order. Problems start when you place items explicitly or reorder them.
A layout that mismatches
Take the page structure from the earlier chapters. The HTML lists the sidebar before the main content:
<div class="page">
<header>Header</header>
<aside>Sidebar</aside>
<main>Main content</main>
<footer>Footer</footer>
</div>The responsive version stacks the areas on small screens like this:
.page {
display: grid;
grid-template-columns: 1fr;
grid-template-areas:
"header"
"main"
"sidebar"
"footer";
}On a phone, the screen shows header, main, sidebar, footer. The source order is header, sidebar, main, footer. A keyboard user tabbing through the page reaches the sidebar links first, then jumps back up the screen to reach the main content. A screen reader user hears the sidebar before the main content they can see first.
If the sidebar holds ten navigation links, the keyboard user tabs through all of them before reaching the content. Visually the content is first, so this is unexpected.
Fix the source, not the screen
The simplest rule is to write the HTML in the order you want users to read and tab, then use grid to move boxes only where the change does not affect meaning.
Here the fix is to put main before aside in the HTML. The mobile stack now matches the source order. On wide screens, the area map decides where the sidebar goes.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Source order demo</title>
<style>
.page {
display: grid;
gap: 1rem;
grid-template-columns: 1fr;
grid-template-areas:
"header"
"main"
"sidebar"
"footer";
}
.page > header { grid-area: header; }
.page > main { grid-area: main; }
.page > aside { grid-area: sidebar; }
.page > footer { grid-area: footer; }
@media (min-width: 48rem) {
.page {
grid-template-columns: 1fr 220px;
grid-template-areas:
"header header"
"main sidebar"
"footer footer";
}
}
a:focus-visible {
outline: 3px solid #1a5fb4;
outline-offset: 2px;
}
</style>
</head>
<body>
<div class="page">
<header><a href="/">Home</a></header>
<main>
<h1>Article</h1>
<p>Main content with a <a href="/more">link</a>.</p>
</main>
<aside>
<a href="/one">Sidebar link one</a>
<a href="/two">Sidebar link two</a>
</aside>
<footer><a href="/contact">Contact</a></footer>
</div>
</body>
</html>On wide screens the sidebar sits to the right of the main content, and on narrow screens it sits below. In both cases the visual order (header, main, sidebar, footer) equals the source order. Tab order is predictable at every width.
Note that the page only needed an area-map change. Nothing in the HTML moved between breakpoints, which is the safe pattern.
When the design needs a left sidebar
Many designs put the sidebar on the left. In a left-to-right language, the eye reads left before right, so the design reads sidebar first while the source reads main first. You then have three options.
- Put the sidebar first in the source too. This is fine when the sidebar is short or is the primary navigation users expect first. Provide a skip link so keyboard users can jump to the main content.
- Keep main first in the source and accept the mismatch on wide screens. This is acceptable only if the sidebar is secondary content, and the focus jump is small and not confusing. Test with a keyboard before deciding.
- Use the newer
reading-flowproperty. It tells the browser to follow the visual order of a grid for focus navigation. At the time of writing, support is limited to recent Chromium-based browsers, so check current compatibility tables before relying on it. Use it as an enhancement on top of a sound source order, not as a replacement for one.
A skip link is a link at the very start of the page that jumps past repeated blocks:
<a class="skip-link" href="#content">Skip to main content</a>
<!-- header, aside ... -->
<main id="content" tabindex="-1">...</main>.skip-link {
position: absolute;
left: -999px;
}
.skip-link:focus {
left: 1rem;
top: 1rem;
z-index: 10;
background: #fff;
padding: 0.5rem 1rem;
}The link is moved off-screen but stays in the tab order, and it moves on-screen when it receives focus. Do not use display: none for it, because that removes it from the tab order and from the accessibility tree.
Reordering inside card grids
Auto-fit grids usually need no reordering. Two situations deserve care.
orderon cards. If you sort cards visually withorder, a screen reader still reads them in HTML order. Sort the HTML itself, or sort on the server or with JavaScript before rendering.grid-auto-flow: dense. The browser fills holes with later items. This is acceptable for decorative tiles such as an image gallery. It is a problem for ordered content such as search results, steps, or a ranked list.
Checking your work
You do not need special tools to find most order bugs.
- Press Tab from the top of the page and watch the focus ring. It should move in a path that matches how you would read the page.
- Resize the window through each breakpoint and repeat. Area maps change at breakpoints, so a layout that passes at one width can fail at another.
- Disable author styles in the browser's developer tools, or use a reader mode. The result is the source order, and it should still make sense.
- Open the accessibility tree in browser developer tools and check landmark order.
The diagram below compares a mismatched and a matching layout.
Summary of decision rules
| Situation | Recommended approach |
|---|---|
| Stacked layout on small screens | Match the source order to the stack order. |
| Sidebar on the right on wide screens | Put `main` first in the source. Source and visual order agree. |
| Sidebar on the left, and it is primary navigation | Put it first in the source and add a skip link. |
| Sidebar on the left, and it is secondary content | Source `main` first, test the focus path, consider `reading-flow` as an enhancement. |
| Sorted or ranked cards | Sort in the HTML, not with `order`. |
| Decorative tiles | `dense` packing is acceptable. |
Exercise
A page has this HTML order: header, nav, main, footer. At 48rem and wider, the area map is "header header" / "nav main" / "footer footer". Below 48rem, the stacked map is "header" / "main" / "nav" / "footer". On a phone, what mismatch do keyboard users experience, and how do you fix it without changing the desktop appearance?
Show answer
On a phone, the screen shows main above nav, but the source places nav before main. Tab focus reaches the nav links first, then jumps back up to main. To fix it without changing the desktop look, change the stacked map to "header" / "nav" / "main" / "footer" so it matches the source. If nav must appear below main on phones, move main before nav in the HTML and keep the desktop map, accepting that on desktop the nav appears to the left while focus reaches main first. In that case, test the focus path and add a skip link if the nav is long.
Exercise
A product list uses order to show the cheapest items first, but the HTML lists items by date added. Why is this an accessibility problem, and what is the fix?
Show answer
Screen readers and keyboard focus follow the HTML order, so they present the items by date while sighted users see them by price. This breaks the meaningful-sequence requirement (WCAG 1.3.2). The fix is to sort the items in the HTML itself, on the server or with JavaScript before rendering, and drop the order property.