Focus Order: CSS reading-flow to the Rescue
Frontend work is all about trade-offs sometimes: maintainability vs delivery speed, accessibility vs design requirements, etc. Some solutions look perfectly fine on screen and still introduce problems that only show up once you navigate with a keyboard or use a screen reader.
Some people might think that I shouldn’t talk about compromises in a blog post dedicated to accessibility. But I don’t support extremes in any way. I think it’s more useful to show the trade-offs, and how to make them work for your users.
Let’s look at a very popular case for many e-commerce websites: a responsive grid where a few items (promotions or advertisements) are placed at specific visual positions, and what that does to focus order.
The Challenge: Position Depends on the Layout
Imagine a responsive product grid with 4, 3, 2 or 1 column depending on the width of its container or screen size. Most cards simply flow in order. But a few promotional cards have to appear at particular spots, and it would be super nice if they are always placed at the outer edge of the page, alternating right and left. Those spots change with the number of columns. The promo that occupies the 8th cell in a 4-column layout needs to be the 6th cell in a 2-column one.
A promo may only take its cell if there are enough products to fill the rest of that row. Otherwise it becomes a trailing promo: it goes after the last product. With zero products there are no promos at all.
Basically, we have to keep a few highlighted items evenly distributed without leaving holes. But how do we implement it?
Option 1: Reorder the DOM with JavaScript
Calculate each promo’s index from the current column count and insert it into the list at that position. The DOM order then matches the visual order, which is exactly what keyboard and screen reader users need.
The cost is everything around that calculation. You have to know the column count, recalculate on every resize, and keep the JavaScript logic in sync with your CSS breakpoints forever.
Server-side rendering may make it worse. The server has no idea how wide the client’s grid container is, so it renders a 1-column default and the client rearranges after hydration. On a wide screen that is a very visible layout shift, and it hurts both the user and your Core Web Vitals.
Option 2: Let CSS Grid Do the Positioning
Keep the DOM order stable and use CSS Grid to place the promos. Grid was built for positioning independent of source order. The placement rules change per breakpoint with container queries, no JavaScript watches the viewport, and SSR just works because the browser places the grid correctly on first paint.
The markup becomes one plain list: every product, then the promos.
<ul class="grid">
<li class="product">…</li>
<li class="product">…</li>
<!-- … -->
<li class="promo">…</li>
<li class="promo">…</li>
</ul>
The column count comes from a container query on the grid’s parent:
.stage { container: stage / inline-size; }
.grid { display: grid; }
@container stage (width >= 990px) {
.grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}
The promo positions are plain rules, one set per column count. Products are auto-placed and flow around the occupied cells in source order, so they stay ascending in every layout.
Now the “only if enough products” requirement. It can also be met with simple CSS! It only needs some counting.
Take promo #1 in the 4-column layout. Its cell is row 2, column 4: the last cell of the second row. To leave no holes, the 7 cells before it must be filled with products: 4 in row 1 and 3 in row 2. So promo #1 may take its cell only if the list has at least 7 products.
CSS selectors can’t count products (or can they already? It evolves so quickly!), but they can check whether a 7th one exists. Products come first in the list, so if the 7th child of the grid is a product, there are at least 7 of them. That is a question the :has() selector can answer:
/* 4 columns: promo #1 sits in row 2 on the right edge
once 7 products exist to fill rows 1 and 2 around it. */
.grid:has(> .product:nth-child(7)) > .promo:nth-child(1 of .promo) {
grid-row: 2;
grid-column: 4;
}
Read the selector in 2 parts:
.grid:has(> .product:nth-child(7))matches the grid only when its 7th child is a product, so only when there are enough products.> .promo:nth-child(1 of .promo)picks the first promo inside that grid.
Every other promo and layout works the same way, only with a different number. The number is row × columns − 1, minus the promos placed before this one, because each of them already fills a cell. For promo #2 in row 4 that is 4 × 4 − 1 − 1 = 14 products.
With fewer products the rule doesn’t match, the promo keeps its default auto placement, and the browser drops it into the next free cell after the last product. That is the trailing behaviour for free.
And the business rule that no products means no promos:
.grid:not(:has(> .product)) > .promo { display: none; }
A few lines of CSS replace the placement algorithm, the resize handling and the hydration struggles. For a handful of repositioned items this is clearly the simpler option. The rules do multiply, though: every promo needs one per column count, so 2 promos across 4 layouts is already 8 rules. And it has one real problem: the DOM order is now different from the visual order.
When Visual Order and DOM Order Don’t Match
Moving an item with CSS doesn’t move it in the DOM, and sequential focus navigation follows the DOM. A keyboard user tabs through a few cards, expects to land on the promo they can see next, and instead keeps going through products. The promos turn up at the very end, after everything else.
This isn’t automatically a WCAG failure. WCAG 1.3.2 Meaningful Sequence and WCAG 2.4.3 Focus Order only require that the order preserve meaning and operability.
It’s subjective. If you want, you can find it meaningful: first you go through the products, then you reach the promos. Makes sense!
But it still might not be the best experience for keyboard users. A focus order that visibly contradicts the layout is confusing, and in a long grid it’s exactly the kind of thing an audit flags. The standard advice, let the DOM define a logical sequence, sends us straight back to Option 1 and its SSR problems (or to negotiating the initial design requirements).
What if we want to keep the DOM stable and still have navigation follow the visual placement?
CSS reading-flow to the Rescue
The reading-flow property lets a flex, grid or block container define the order in which its children are exposed to assistive technology and reached by sequential focus navigation. For a grid, one declaration is enough:
.grid {
display: grid;
reading-flow: grid-rows;
}
With grid-rows, the browser derives reading and focus order from where items actually land, row by row. When CSS moves a promo to the right edge of row 2, Tab reaches it right after product 7.
The two grids below share the same DOM and the same layout; only the declaration differs. The green badge in each cell is its position in the Tab sequence.
reading-flow, Tab follows DOM order: the promos are reached 15th and 16th, after every product. With reading-flow: grid-rows they are reached 8th and 13th, where they appear.Try it yourself in the live reading-flow demo on CodePen: press Tab through the cards with reading-flow switched on, then off, at 4, 3, 2 and 1 columns. Open it in Chrome or Edge to see the difference. The demo is a simplified version of the code in this post: it uses smaller breakpoints so every layout fits a small screen.
The other values cover the remaining cases: grid-columns reads column by column, grid-order follows the order property, flex-visual and flex-flow are the flexbox equivalents, and source-order combined with a per-item reading-order lets you hand-pick a sequence without touching markup.
What I like about this:
- No DOM reordering just to match the layout
- No positive
tabindexvalues and no manual focus management - No JavaScript listeners tied to breakpoints
- One rule that holds across every grid configuration
One line of CSS, and visual order and navigation order agree again.
A caution from Rachel Andrew, one of the property’s authors, in her article Making features generic in CSS: for most grid layouts the source order is very likely to be the best focus order.
Is It Ready for Production?
Not everywhere. As of November 2026, reading-flow ships in Chromium browsers (Chrome and Edge since version 137, May 2025) and in neither Firefox nor Safari. reading-flow on Can I Use has the current picture.
That may change, but don’t count on it soon. reading-flow is one of the features developers are voting for in Interop 2027, the yearly effort where browser vendors agree on what to make work the same everywhere, and the official focus areas are announced in early 2027. Mozilla’s position on reading-flow for Firefox is still missing, and WebKit’s position on the proposal, from the engine behind Safari, is that it is not ready to support it as it stands, so the property could still change before other browsers ship it.
Until then, treat it as a progressive enhancement. In browsers without support, focus follows DOM order exactly as before. That makes the pre-shipping question not “is the enhanced order nice?” but “is the fallback order acceptable on its own?”
For this grid the fallback is: every product in order, then the promos. Products never move relative to each other, so a keyboard user still gets a sensible catalogue; the promos just come last. I’d ship that. If your repositioned items were, say, the primary actions on a page, the same fallback would be confusing enough to fail your own audit in Safari, and reading-flow hasn’t solved your problem yet.
Either way, test with a keyboard and test with at least one screen reader. CSS that changes focus order is precisely the kind of thing that works in the demo and surprises you on the real page.
Final Thoughts
In this case, placing a few cards in a grid ended up touching accessibility, SSR, performance, responsive design and maintainability topics at once.
We were trying to satisfy 3 requirements at the same time:
- Responsive positioning. Some items must sit at positions that depend on the container width.
- Stable server-rendered markup. No visible rearrangement after hydration, no layout shift.
- Accessible navigation. The visual arrangement and the navigation order should agree, across browsers and assistive technologies.
Every approach has a price:
| Approach | Responsive | Stable SSR | Focus matches layout |
|---|---|---|---|
| JavaScript reorders the DOM | Yes | No, shifts after hydration | Yes |
| CSS Grid placement only | Yes | Yes | No, follows DOM order |
CSS Grid + reading-flow | Yes | Yes | Yes in Chromium, DOM order elsewhere |
| Precomputed variants + container queries | Yes | Yes | Yes, at the cost of duplicated markup |
The last row means rendering one copy of the grid per column count, each already in the right order, and showing only the matching copy with a container query.
reading-flow won’t replace thoughtful markup, and it doesn’t remove every trade-off. But it gives us one more way to keep layout and navigation aligned, and sometimes one line of CSS saves a surprising amount of complexity.
The most useful part, though, was understanding why that one line wasn’t enough on its own.