Tab Order in Flex and Grid Layouts
You'll learn how CSS layout reordering can break keyboard navigation and how to maintain accessibility when visual and DOM order don't match.
The Visual vs Logical Order Problem
Flexbox and Grid give you powerful tools to rearrange elements visually—properties like order, flex-direction: row-reverse, or grid placement can make item #3 appear first on screen. Here's the catch: keyboard tab order follows the DOM (HTML) order, not the visual order.
Think of it like reading a book where the chapters are numbered 1, 2, 3, but someone printed them in order 2, 3, 1. Your eyes see the wrong sequence, but the table of contents still lists them in the original order. Screen reader users and keyboard navigators experience the "table of contents" version—they move through elements as written in HTML, not as displayed on screen.
The Accessibility Impact
When visual order and tab order conflict:
- Keyboard users jump unexpectedly around the page
- Screen reader users hear content announced in a confusing sequence
- Focus moves illogically, breaking mental models
Maintaining Accessibility
Best practice: Match your HTML source order to the intended reading and navigation order. Use CSS only for minor visual adjustments, not complete reordering.
If you must reorder visually:
- Test thoroughly with keyboard-only navigation (Tab key)
- Consider if you should restructure your HTML instead
- Use
tabindex="-1"and programmatic focus management only when absolutely necessary (rarely the right solution) - Document the discrepancy for future maintainers
Responsive Considerations
Be especially careful when changing flex-direction or grid layouts at breakpoints—you might inadvertently create tab order problems on mobile that don't exist on desktop.
Key Takeaway: Always prioritize logical HTML order over visual CSS tricks; keyboard tab order follows DOM structure, so visual reordering creates accessibility barriers that CSS alone cannot fix.