Sub topic 13
Keeping Source Order and Visual Order Aligned
A grid lets you put any element in any cell, so the order of elements on screen can differ from the order in the HTML. This chapter explains why that difference matters and how to keep the two orders in agreement.
This chapter assumes you can build a 12-column grid with repeat(12, 1fr) and place items with grid-column, as covered in the earlier chapters. You should also know what the tab key does on a web page.
Two orders, one page
Every page has two orders.
- Source order is the order of elements in the HTML document.
- Visual order is the order in which the elements appear on screen, left to right and top to bottom.
Without CSS layout tricks, the two are the same. Grid, flexbox, and order can pull them apart. Some people and tools follow the visual order, and others follow the source order.
| Who or what | Follows |
|---|---|
| Sighted mouse user | Visual order |
| Keyboard user pressing Tab | Source order |
| Screen reader reading the page | Source order |
| Browser reader mode, or a page with CSS failing to load | Source order |
| Search engine crawler | Source order |
| Selecting and copying text | Source order, so the selection can jump around on screen |
The CSS Grid specification says that grid placement and order change only the visual presentation. They do not change speech order or the order in which focus moves. A mismatch is therefore not only cosmetic. A keyboard user can see focus jump from the right side of the screen to the left, then back, with no clear reason.
How grid lets the orders diverge
Grid has four main ways to change visual order without touching the HTML.
- Explicit
grid-columnandgrid-rowvalues, which put an item in any cell. - The
orderproperty, which changes where items go during auto-placement. grid-auto-flow: dense, which backfills earlier gaps with later items.- Named areas with
grid-template-areas, which place items by name rather than by position.
All four leave the DOM unchanged. Here is a common case. The main content comes first in the HTML, but the design wants the sidebar on the left.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Source order demo</title>
<style>
.page {
max-width: 72rem;
margin-inline: auto;
padding-inline: 1rem;
}
.grid {
display: grid;
grid-template-columns: repeat(12, 1fr);
column-gap: 1.5rem;
}
/* Risky: the sidebar is visually first but second in the HTML. */
.main {
grid-column: 5 / span 8;
grid-row: 1;
}
.sidebar {
grid-column: 1 / span 4;
grid-row: 1;
}
</style>
</head>
<body>
<div class="page">
<div class="grid">
<main class="main">
<h1>Article</h1>
<p>Read the <a href="#notes">notes</a>.</p>
</main>
<aside class="sidebar">
<a href="#one">Related one</a>
<a href="#two">Related two</a>
</aside>
</div>
</div>
</body>
</html>Open this page and press Tab. Focus lands on the "notes" link in the right column first, then jumps to the left column. A sighted keyboard user expects the opposite.
Why `grid-row: 1` is there
With auto-placement, an item that has a definite column but no definite row is placed after the previous item. The algorithm moves to the next row when the item's start column is earlier than the current cursor column. Here .main is placed at column 5, and then .sidebar asks for column 1, which is earlier. Without grid-row: 1, the sidebar would drop to a second row. Setting the row on both items keeps them side by side.
Fixing it: change the HTML, not the CSS
The most reliable fix is to make the source order match the visual order.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Aligned source order</title>
<style>
.page {
max-width: 72rem;
margin-inline: auto;
padding-inline: 1rem;
}
.grid {
display: grid;
grid-template-columns: repeat(12, 1fr);
column-gap: 1.5rem;
}
.sidebar {
grid-column: span 4;
}
.main {
grid-column: span 8;
}
</style>
</head>
<body>
<div class="page">
<div class="grid">
<aside class="sidebar">
<a href="#one">Related one</a>
<a href="#two">Related two</a>
</aside>
<main class="main">
<h1>Article</h1>
<p>Read the <a href="#notes">notes</a>.</p>
</main>
</div>
</div>
</body>
</html>Now the CSS has no placement beyond span. Auto-placement puts the first item in columns 1 to 4 and the second in columns 5 to 12. Tab order, reading order, and visual order all agree.
This choice has a cost. A left sidebar full of links now sits before the article in the source, so a screen reader user must pass it every time. Two common answers:
- Put the sidebar on the right, after the article, so the natural order is also the preferred one.
- Keep the sidebar first and add a "Skip to main content" link at the top of the page.
Pick the layout whose natural order serves the reader. Do not pick the order that is easiest to style and then repair it with CSS.
Source order and the stacked mobile layout
On a narrow screen the grid collapses to one column and each item spans the whole width. In that layout, the visual order is the source order, because auto-placement lays items out in sequence. This is why source order should be written for the mobile reading experience first.
.grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 1rem;
}
.sidebar,
.main {
grid-column: 1 / -1;
}
@media (min-width: 48em) {
.grid {
grid-template-columns: repeat(12, 1fr);
gap: 1.5rem;
}
.sidebar {
grid-column: span 4;
}
.main {
grid-column: span 8;
}
}Problems appear when a breakpoint rearranges items. Suppose a call-to-action box is third in the HTML and a rule at 48em moves it to the top of the page with order: -1. Sighted users see it first, but keyboard focus reaches it only after the two blocks above it in the source. Each breakpoint that moves content makes the mismatch harder to track.
Auto-placement features that reorder content
Two features reorder without any explicit coordinates.
ordersorts items by its value before auto-placement. The default is0. A negative value moves an item earlier visually and leaves its place in the DOM unchanged.grid-auto-flow: denselets the algorithm go back and fill holes with later items. This can fill the gaps left by wide items in a card grid, but it also moves cards out of source order.
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
gap: 1.5rem;
grid-auto-flow: dense; /* later cards may appear before earlier ones */
}
.card--wide {
grid-column: span 2;
}If the cards are ordered, such as steps in a process, search results, or a ranked list, do not use dense. If the cards are an unordered collection, such as a gallery, a small mismatch is usually acceptable.
Rules of thumb
- Write the HTML in the order a person should read and tab through the page.
- Use
grid-columnandgrid-rowto set sizes and positions that stay consistent with that order. - Treat any item that appears visually before an item that precedes it in the HTML as a defect to justify.
- Keep reorderings small and non-interactive. Moving a decorative image is low risk. Moving a form is high risk.
- Retest after every breakpoint change, because a layout that is aligned at one width can be misaligned at another.
A newer CSS property, reading-flow, lets the browser follow visual order for focus navigation in grid and flex containers. Browser support is still incomplete, so check current compatibility tables before depending on it. Treat it as a safety net, not a replacement for a sensible source order.
Checking your own pages
You can test alignment in a few minutes.
- Reload the page and press Tab repeatedly. Watch whether the focus ring moves in the pattern your eye expects.
- Add a temporary rule so the focus ring is easy to see:
:focus-visible {
outline: 3px solid #d00;
outline-offset: 2px;
}- Disable your stylesheet in the browser's developer tools and read the raw page. The order you see is what a screen reader encounters.
- Resize the window through each breakpoint and repeat the Tab test at each width.
Common mistakes
- Reordering with
order: -1to put a banner or sidebar first, and forgetting that the DOM is unchanged. - Omitting
grid-rowwhen placing items with explicit columns, which pushes items onto unexpected rows. - Using
denseon a list where sequence has meaning. - Testing with a mouse only.
Exercise
A page has <main> followed by <aside> in the HTML. The CSS gives the aside grid-column: 1 / span 4; grid-row: 1; and main grid-column: 5 / span 8; grid-row: 1;. What does a keyboard user experience, and what are two ways to fix it?
Show answer
Focus goes into the main content in the right-hand columns first, then jumps to the left-hand sidebar, which is the reverse of the visual left-to-right order. Fix it by moving the aside before the main in the HTML and using plain spans, or by moving the sidebar to the right (after main in both source and visual order). A skip link helps if the sidebar must stay first.
Exercise
Why does grid-auto-flow: dense on a list of numbered steps cause a problem, even though nothing changes in the HTML?
Show answer
Dense packing can place a later step into an earlier gap, so step 4 may appear above step 3 on screen. The HTML, tab order, and screen reader order still list the steps in sequence, so sighted users and assistive technology users get different orders. Remove dense for ordered content.