Sub topic 10
Keeping Visual Order and DOM Order Aligned
This chapter assumes you know basic HTML (headings, links, buttons), basic CSS (selectors, the box model, custom properties), and the layout ideas from earlier chapters: size, contrast, and position as hierarchy cues. Here you will learn why the order of elements in your HTML must match the order in which they appear on screen, and how to find and fix mismatches.
Two orders, one page
A web page has two orders at the same time.
- Visual order is the sequence in which a sighted reader sees elements on the screen. Size, contrast, and position create it.
- DOM order (also called source order) is the sequence of elements in the HTML document. The DOM, or Document Object Model, is the tree the browser builds from your HTML.
Many people never see the DOM order. Keyboard users move through it with the Tab key. Screen readers read it aloud from top to bottom. Search engines and reader modes parse it. If the two orders disagree, these people get a page whose sequence differs from the one you designed.
Earlier chapters showed how position places important content first. CSS gives you powerful ways to place elements anywhere on the screen regardless of their source position. That power is the source of the problem.
What goes wrong when the orders differ
Consider a keyboard user who presses Tab. The browser moves focus to the next focusable element in DOM order (links, buttons, form fields, and anything with a positive or zero tabindex). It does not follow the visual layout. If a button appears first on screen but sits last in the HTML, focus jumps to it only after every other control. The focus indicator seems to leap around the page.
A screen reader user has a similar experience. The reader announces content in DOM order. If a visually prominent call to action is placed last in the HTML, the user hears the secondary details first and may not understand the page's purpose until the end.
Sighted keyboard users are affected most. They see the layout and expect focus to move through it logically. A jump from the top-left of the page to the bottom-right, then back to the top, is disorienting.
WCAG covers this in two success criteria:
- 1.3.2 Meaningful Sequence (Level A): when the order of content affects its meaning, a correct reading order must be available programmatically.
- 2.4.3 Focus Order (Level A): when a page can be navigated sequentially, focusable components receive focus in an order that preserves meaning and operability.
Related guidance, 2.4.11 Focus Not Obscured, also matters when sticky headers cover a focused element, but this chapter focuses on order.
The usual causes of mismatch
Mismatches almost always come from a handful of CSS features. Each one changes where an element is drawn without changing where it sits in the HTML.
| Feature | What it changes | Does it change DOM or tab order? |
|---|---|---|
| `order` in flexbox or grid | Visual position of items | No |
| `flex-direction: row-reverse` or `column-reverse` | Visual direction of items | No |
| `grid-template-areas` or explicit `grid-row`/`grid-column` | Which cell an item occupies | No |
| `position: absolute` or `fixed` | Where the item is drawn | No |
| Negative margins and transforms | Drawn location | No |
| `tabindex` greater than 0 | Tab order | Yes, and it usually makes things worse |
None of these change the DOM order. All of them can make the screen disagree with it.
A worked example: the reordered card
Suppose you design a product card. Visually you want an image on top, then the product name, then the price, then a buy button. You also want a short badge such as "Sale" to appear first on the screen. A common shortcut is to write the badge last in the HTML and use order to pull it forward.
<article class="card">
<h2>Trail Shoes</h2>
<p class="price">$89</p>
<a class="button" href="/buy/trail-shoes">Buy now</a>
<span class="badge">Sale</span>
</article>.card {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.badge { order: -1; }On screen the badge appears first. In the DOM it is last. A screen reader says "Trail Shoes, $89, Buy now, Sale". The sale information arrives after the user has already been offered the purchase. That is a small problem in one card. Across a grid of thirty cards it becomes a pattern that is hard to follow.
The fix is to write the HTML in the order you want it read and drop the order property.
<article class="card">
<span class="badge">Sale</span>
<h2>Trail Shoes</h2>
<p class="price">$89</p>
<a class="button" href="/buy/trail-shoes">Buy now</a>
</article>.card {
display: flex;
flex-direction: column;
gap: 0.5rem;
}Now the visual and DOM orders match, and the CSS is shorter.
When the design needs a different visual order
Sometimes the visual order really does differ from the best reading order. A common case is a page where a large heading sits above an image, but the markup is easier to maintain with the image first. Another is a two-column layout where a sidebar appears on the left on wide screens but its content is secondary.
The first question to ask is whether you can change the HTML instead. In most cases you can. Treat CSS reordering as a last resort and apply it only when the reordering does not change meaning or the order of interactive elements.
If you must reorder, follow these checks:
- Tab through the page with the keyboard. Does the focus ring move in a sensible path?
- Read the page with a screen reader or inspect the accessibility tree. Is the announced sequence sensible?
- Turn off CSS (many browser extensions can do this) and read the raw order. Does it still make sense?
A purely decorative reorder, such as swapping two non-interactive images, is lower risk than reordering links, buttons, and form fields. Focus order is where reordering hurts most.
Flexbox and grid reordering
The CSS specifications warn about this directly. The order property and grid placement affect only the visual rendering, not speech order or navigation order. The specifications tell authors to use these properties for visual adjustments and to change the document source when the order should change for everyone.
Grid is a good example of doing it well. A layout can place a sidebar on the left while keeping the main content first in the source.
<body>
<header>Site header</header>
<main>
<h1>Article title</h1>
<p>Main content...</p>
</main>
<aside>
<h2>Related links</h2>
<a href="/one">One</a>
</aside>
</body>body {
display: grid;
grid-template-columns: 1fr 3fr;
grid-template-areas:
"header header"
"side main";
gap: 1rem;
}
header { grid-area: header; }
main { grid-area: main; }
aside { grid-area: side; }Here the sidebar appears left of the main content, yet it comes after main in the source. Keyboard users reach the article first, then the sidebar. The visual order is left to right, the DOM order is main then aside. For a reader who looks at the main content first (it is larger and holds the primary heading), this mismatch is minor. But the sidebar's links are interactive, so test the focus path. If focus jumps from the right column back to the left column, that is expected and acceptable here, because the sidebar is a distinct region that screen reader users can skip to by landmark.
Reading direction and reversed layouts
row-reverse is useful for right-to-left languages and for alternating image-and-text rows. The danger is using it only to flip the look of a layout.
.feature {
display: flex;
flex-direction: row-reverse;
}If the HTML lists text and then image, row-reverse draws the image on the left and the text on the right. For a feature row with a single paragraph and a picture, this is harmless. If the row holds three links, the Tab order runs right to left while the eyes read left to right. Avoid row-reverse on groups of focusable items.
Absolute positioning and hidden jumps
Absolute and fixed positioning take an element out of flow. A "Skip to content" link, a floating chat button, or a cookie banner may appear at one end of the screen but sit at the other end of the DOM.
- A skip link should be the first focusable element in the DOM, then shown on focus. That keeps the visual and DOM order the same at the moment it matters.
- A cookie banner placed at the bottom of the HTML but drawn over the page top traps users who Tab for a long time before reaching it. Place it early in the DOM, or move focus to it deliberately when it opens.
- A modal dialog needs focus managed deliberately. Moving focus into it on open and back to the trigger on close is part of keeping the order meaningful.
.skip-link {
position: absolute;
left: -999px;
}
.skip-link:focus {
left: 1rem;
top: 1rem;
background: #ffffff;
color: #0b1f4d;
padding: 0.5rem 1rem;
}Notice that hiding the link off screen with left: -999px keeps it focusable. Using display: none or visibility: hidden would remove it from the tab order and the accessibility tree entirely.
Do not fix order with `tabindex`
When the tab order looks wrong, it is tempting to add tabindex="1", tabindex="2", and so on. Any positive tabindex moves an element ahead of all elements with tabindex="0" or none. Positive values are fragile: adding one new control forces you to renumber the rest, and they conflict with browser defaults. They also fix only keyboard order, not screen reader reading order.
Use these values instead:
tabindex="0"makes a non-interactive element focusable in DOM order. Use it rarely, and only for custom widgets that need it.tabindex="-1"makes an element focusable by script but not by Tab. It is useful for moving focus to a heading after navigation.- Positive values: avoid them.
How visual hierarchy fits in
This chapter belongs in a series on hierarchy because the cues you have learned work on a page only if the structure underneath agrees with them. A large, high-contrast heading at the top-left tells sighted users what matters most. The same heading must be the first heading in the DOM, and an h1, so that assistive technology users receive the same message.
The same applies to headings generally. Heading levels (h1 to h6) are the structural counterpart of your type scale. Choose a heading level for its place in the outline, then style it with CSS to get the size you want. Do not pick h4 because it is small, and do not use a styled div to look like a heading.
.section-title {
font-size: 1.25rem;
}<h2 class="section-title">Shipping details</h2>Here the element is an h2 for structure and sized to 1.25rem for the design. The visual hierarchy and the document outline both stay correct.
A diagram of the check
The following flow shows a practical review of order.
Common mistakes
- Using
orderto move a banner or badge forward when the HTML could simply be rewritten. - Applying
row-reverseorcolumn-reverseto a group of links or buttons. - Using absolute positioning to place a control far from its place in the DOM.
- Adding positive
tabindexvalues to patch a bad order. - Choosing heading levels for their default size rather than for their place in the outline.
- Testing only with a mouse.
Quick audit with a script
You can list the tab order and compare it with the position on screen. This script, run in the browser console, prints each focusable element in DOM order with its top and left coordinates. If the coordinates jump back and forth in a way you did not plan, investigate.
const selector = [
"a[href]",
"button:not([disabled])",
"input:not([disabled])",
"select:not([disabled])",
"textarea:not([disabled])",
"[tabindex]:not([tabindex='-1'])"
].join(",");
const items = Array.from(document.querySelectorAll(selector));
console.table(
items.map((el, index) => {
const box = el.getBoundingClientRect();
return {
domIndex: index,
element: el.tagName.toLowerCase(),
label: (el.textContent || el.getAttribute("aria-label") || "").trim().slice(0, 30),
top: Math.round(box.top + window.scrollY),
left: Math.round(box.left + window.scrollX)
};
})
);The script does not account for positive tabindex values, which change the true tab order. That is one more reason to avoid them. It also reports every element in the DOM, including hidden ones with zero size, so read the coordinates alongside what you see.
Summary
- Visual order is what sighted users see. DOM order is what keyboards, screen readers, and parsers follow.
- CSS features such as
order, reversed flex directions, grid placement, and absolute positioning change only the visual order. - Write the HTML in the reading order you want, then style it. Reorder with CSS only for low-risk, non-interactive content.
- Do not patch order with positive
tabindex. - Check with the keyboard, an accessibility tree view, and the console script above.
Exercise
A page uses display: flex; flex-direction: row-reverse; on a row containing three links in the HTML order "Home", "Products", "Contact". On screen they read "Contact, Products, Home" from left to right. What happens when a keyboard user presses Tab, and what is the better fix?
Show answer
Focus moves in DOM order, so it goes Home, then Products, then Contact. On screen that is right to left, which does not match the left-to-right reading direction. The better fix is to write the links in the HTML in the order you want them to appear ("Contact", "Products", "Home") and remove row-reverse, so the visual order and the DOM order are the same.
Exercise
Why is tabindex="3" on a button a poor way to move it earlier in the keyboard order, and which two things should you check or change instead?
Show answer
A positive tabindex pulls the element ahead of all normally ordered elements and forces you to renumber other controls whenever the page changes. It also does not change the order a screen reader reads content. Instead, move the element earlier in the HTML so DOM order matches the design, and verify the result by tabbing through the page and inspecting the accessibility tree.