0Pricing
Web Accessibility Academy · Lesson

Fragments, Portals, and the DOM Order Trap

Keep rendered order matching the accessibility tree.

Fragments, Portals, and the DOM Order Trap is a free Web Accessibility Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Web Accessibility Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Source Order Is the Default Order

Screen readers and the keyboard follow the DOM order, the order elements appear in the document. CSS may move things visually, but reading order does not change.

Fragments Add No Extra Box

A React Fragment groups children without adding a wrapper div. That keeps your DOM clean and avoids breaking lists or tables with stray elements.

<>
  <th>Name</th>
  <th>Email</th>
</>

Why a Wrapper div Can Hurt

An extra div inside a list or table can break the parent-child structure assistive tech expects. A Fragment avoids that invalid nesting entirely.

Portals Render Elsewhere

A Portal renders children into a different DOM node, often the document body. It is the standard way to mount modals above everything else.

createPortal(<Modal />, document.body);

Portals Keep React Context

Even though the markup lands elsewhere, a portal stays in your component tree. State and context flow normally, so logic is unaffected.

The DOM Order Trap

A portal at the end of body can appear after unrelated content. Tabbing may jump to a far-off spot, so focus management becomes essential for portals.

Move Focus Into the Portal

When a portal opens, the keyboard does not follow automatically. Explicitly move focus into the modal so the user is where the new content is.

Trap and Return Focus

While a portal modal is open, keep Tab inside it. On close, return focus to the element that opened it, since DOM proximity will not do this for you.

Visual Order Versus Reading Order

CSS flexbox order or grid placement can rearrange the screen. The tab order still follows the source, so avoid reordering interactive content with CSS.

Keep Source Order Meaningful

Write your JSX so the source order matches the logical reading flow. Then both sighted and screen reader users encounter content in the same sensible sequence.

Test by Tabbing

Press Tab through your app and watch where focus goes. If it jumps somewhere unexpected, your DOM order or a portal needs attention.

Quick Check

You mount a modal with a portal into document.body. What new responsibility does that create?

Recap: Order and Portals

Use Fragments to avoid invalid wrappers, keep source order logical, and manage focus carefully whenever a portal renders content elsewhere. 🧩

Frequently asked questions

Is the “Fragments, Portals, and the DOM Order Trap” lesson free?

Yes — the full text of “Fragments, Portals, and the DOM Order Trap” is free to read here on the web, and the Web Accessibility Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Web Accessibility Academy course, upgrade to CoddyKit PRO.

What will I learn in “Fragments, Portals, and the DOM Order Trap”?

Keep rendered order matching the accessibility tree. You practise Web Accessibility Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Web Accessibility Academy?

No prior experience is required. Web Accessibility Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Fragments, Portals, and the DOM Order Trap” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Web Accessibility Academy lesson?

Yes. Every Web Accessibility Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Route Changes Without Page Reloads
  2. Managing Focus With Refs and useEffect
  3. Fragments, Portals, and the DOM Order Trap
  4. jsx-a11y and Component-Level Guardrails
← Back to Web Accessibility Academy