Sub topic 13
Keeping source order and visual order aligned
This chapter assumes you can write a 12-column grid with display: grid and place items with grid-column, as in the previous chapters. You should also know basic HTML structure.
Two orders for the same content
A page has two orders. The source order is the sequence of elements in your HTML. The visual order is where those elements appear on screen.
CSS Grid lets you place any item in any cell, so the two orders can disagree. The browser doesn't mind. People who don't read the page visually may.
What follows the source order
Several things ignore your grid placement and follow the HTML:
- Keyboard focus moves through links and buttons in source order.
- Screen readers announce content in source order.
- Search-engine crawlers and reader modes use source order.
- A one-column layout on small screens stacks items in source order, unless you changed it.
A mismatch in practice
Suppose you want a navigation sidebar on the left and the article on the right. A tempting solution keeps the article first in the HTML and moves the sidebar with order:
<div class="grid">
<main class="main">Article</main>
<nav class="sidebar">Navigation</nav>
</div>.grid {
display: grid;
grid-template-columns: repeat(12, 1fr);
column-gap: 1.5rem;
}
.main {
grid-column: span 8;
}
.sidebar {
grid-column: span 4;
order: -1;
}On a wide screen the sidebar appears first. A keyboard user, though, tabs through the article's links before reaching the sidebar. The focus jumps from the first link on the left to somewhere on the right, then back. A screen reader reads the article first, which contradicts what sighted users see.
Fix the HTML, not the CSS
Write the HTML in the order you want people to experience it. Then use grid-column only to assign widths and positions.
<div class="grid">
<nav class="sidebar">Navigation</nav>
<main class="main">Article</main>
</div>.grid {
display: grid;
grid-template-columns: repeat(12, 1fr);
column-gap: 1.5rem;
}
.sidebar {
grid-column: span 4;
}
.main {
grid-column: span 8;
}Items flow into the grid in source order, so the sidebar fills columns 1 to 4 and the article fills columns 5 to 12. Visual order and source order now agree without any reordering property.
Properties that can break alignment
| Property | Effect | Risk |
|---|---|---|
| `order` | Changes the visual order of grid or flex items | Tab and screen reader order stay the same |
| `grid-row` and `grid-column` with explicit lines | Places items anywhere | Items can land far from their source position |
| `grid-auto-flow: dense` | Back-fills gaps with later items | Later items can appear earlier |
| `flex-direction: row-reverse` | Reverses a flex row | Same mismatch as `order` |
Small mismatches are sometimes unavoidable. A decorative element can move without harm. Content that users read, follow, or tab through should not.
Deciding what to do
- Decide the reading order that works with no CSS at all. This is also your mobile order.
- Write the HTML in that order.
- Use
grid-columnto size items, and place them in a different spot only when the order still makes sense. - Check keyboard focus and reading order at each breakpoint.
Exercise
A card grid uses grid-auto-flow: dense so that a wide card does not leave a gap. A user who tabs through the cards finds the focus jumping backward in places. Why does this happen, and what are two ways to address it?
Show answer
Dense packing back-fills earlier gaps with later items, so those items appear visually before cards that come earlier in the HTML, while tab order still follows the HTML. You can remove dense and accept or design around the gaps, for example by making card widths regular. You can also reorder the HTML, or the data that generates it, so the source order matches the packed result.