Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

React: div cannot appear as a descendant of p

validateDOMNesting reports invalid HTML the browser repairs, which becomes a hydration error. Fix p, a, button, table, and list nesting at the source.

~5 min readReact
#react#validatedomnesting#hydration#html
Bold white headline reading NESTING BROKEN with a React document crate showing an amber block escaping a paragraph.

Introduction & Quick Summary

React 18 logs a warning naming two elements and their relationship:

code
Warning: validateDOMNesting(...): <div> cannot appear as a descendant of <p>. at div at Card (src/components/Card.tsx:14:5) at section at App (src/App.tsx:7:3)

React 19 promotes the same mistake to a hydration error:

code
In HTML, <div> cannot be a descendant of <p>. This will cause a hydration error.

The markup is invalid HTML. The parser inside the browser has already fixed it — it closes the <p> before the <div> — so the DOM React expects and the DOM the browser built differ. Warning in 18, hydration mismatch and a client re-render in 19.

Where it appears:

  • A component that renders <div>, <ul>, <pre>, or <table> inside a <p> written by a parent or by a rich-text renderer.
  • <p> returned from a Markdown component that wraps block output in a paragraph.
  • Nested anchors: a <Link> inside another <Link> or an <a> inside <a>.
  • <tr> or <td> rendered outside a <table> body, often through a component that returns a fragment.
  • Custom components whose name hides what they render, so the stack trace points at the wrapper instead of the culprit.

Quick answer: replace the invalid wrapper. A <p> holds phrasing content only, so a block child becomes a <div> — or the <p> becomes a <div>; anchors never nest, so the inner one becomes a <span> with a click handler or the two links become siblings.

Core Mechanics, Signatures & Reproducible Setup

validateDOMNesting is React DOM's development-only check of the HTML content model. It walks the tree after render, compares each child's tag with the parent's permitted content, and logs the first violation per parent type. In production the check is compiled out, which is why the warning never appears in a deployed build and why hydration bugs surface instead.

Reproduce it without a bundler:

html
<!-- index.html --> <div id="root"></div> <script src="https://unpkg.com/react@19/umd/react.development.js"></script> <script src="https://unpkg.com/react-dom@19/umd/react-dom.development.js"></script> <script> const h = React.createElement; ReactDOM.createRoot(document.getElementById("root")).render( h("p", null, "Read more ", h("div", null, h("a", { href: "/docs" }, "in the docs"))) ); </script>

Open the console and the violation is printed with the component stack. The browser has already produced this DOM:

html
<p>Read more </p><div><a href="/docs">in the docs</a></div>

The <div> escaped the paragraph. Any CSS that selected p > div, or any server-rendered HTML that did contain the nested block, now disagrees with the client DOM.

The rules that produce almost every report:

ParentAcceptsRejectsTypical message
<p>phrasing content: <span>, <a>, <em>, <code>, <br><div>, <p>, <ul>, <table>, <section><div> cannot appear as a descendant of <p>
<a>phrasing content, no interactive descendants<a>, <button>, <input><a> cannot appear as a descendant of <a>
<button>phrasing content<div>, <button>, <a><div> cannot appear as a descendant of <button>
<table><caption>, <colgroup>, <thead>, <tbody>, <tfoot><tr>, <td>, <div><tr> cannot appear as a child of <table>
<ul>, <ol><li>, <script>, <template><div>, <a><div> cannot appear as a child of <ul>
<select><option>, <optgroup><div>, <input><div> cannot appear as a child of <select>

Key Comparison Table

Three ways to resolve the same violation, and when each one is correct:

FixChangeKeeps semanticsUse when
Change the parent<p> → <div>partly: loses paragraph semanticsthe block truly belongs in the container
Change the childblock → <span> with display: blockyesthe child is inline content styled as a block
Split the parentclose <p> and open a <div> after ityesa paragraph followed by a list or code block

What each element change costs:

ChangeScreen reader effectCSS riskHydration
<p> to <div>no paragraph role; margin comes from CSSinherits div selectorsnone
<div> to <span> + display: blocksame text, no block roleneeds explicit displaynone
<a> to <span role="link">loses keyboard activation unless addedpointer cursor requirednone
<button> wrapping <div> to <div role="button">loses native keyboard handlingmust re-add tabIndex and keydownnone

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

1. A component that hides its tag

The warning names div and the stack shows Card, section, App. The fix usually lives in the leaf component:

tsx
// before: Card renders a div, used inside a paragraph of text function Card({ children }: { children: React.ReactNode }) { return <div className="card">{children}</div>; } // caller <p>Latest release: <Card>v2.4.0</Card></p> // after: the wrapper is inline by default, block only where intended function Card({ children, block = false }: { children: React.ReactNode; block?: boolean }) { return <span className={block ? "card card--block" : "card"}>{children}</span>; }

Search the tree rather than guessing: grep -rn "children" --include=*.tsx src/components | grep -n "<p" finds paragraphs that accept arbitrary children. In a design system, a Text component with as="p" is the usual source, because callers pass blocks without knowing the tag.

2. Markdown renderers wrapping block output in a paragraph

A renderer that maps paragraph to <p> produces <p><pre>...</pre></p> for a fenced code block when the AST node type is misclassified. Configure the renderer so block nodes are not wrapped:

tsx
const components = { p: ({ children }: { children: React.ReactNode }) => { // a paragraph containing a block child is invalid; render a div instead const isBlock = Array.isArray(children) ? children.some((c) => typeof c !== "string" && ["pre", "ul", "ol", "table", "div"].includes((c as any)?.type)) : false; return isBlock ? <div className="paragraph">{children}</div> : <p>{children}</p>; }, };

The some check is cheap and keeps normal paragraphs semantic. It also fixes the hydration error that appears only on posts with code blocks.

A whole-card <Link> plus an inner "read more" <Link> produces <a> cannot appear as a descendant of <a> and, in the browser, an anchor that swallows the click. Make the card a positioned container and one link the only interactive element:

tsx
<article className="card"> <h3><Link href={`/posts/${slug}`}>{title}</Link></h3> <p>{excerpt}</p> {/* decorative overlay: not focusable, not announced */} <Link href={`/posts/${slug}`} className="card-overlay" aria-hidden="true" tabIndex={-1} /> </article>

With position: relative on the card and position: absolute; inset: 0 on the overlay, the whole card is clickable and the document keeps a single link per card. Keyboard users tab to the heading link once, which is the intended behaviour.

4. Tables rendered through fragments

<tr> outside <table> fails when a component returns rows as a fragment and the parent renders a <div>:

tsx
// invalid: table > div > tr <table><div>{rows}</div></table> // valid: tbody owns the rows <table> <tbody>{rows.map((r) => <Row key={r.id} row={r} />)}</tbody> </table>

If the layout needs a wrapper for scrolling, wrap the <table> itself, never its children. The same rule applies to <li>: it must be a direct child of <ul> or <ol>, so a wrapper <div> around list items produces <li> cannot appear as a child of <div> and the browser hoists the items out of the list.

5. Detecting the violation in CI instead of the console

Because the check is development-only, a violation can ship silently and appear as a hydration error. Run the component tests with NODE_ENV=development and make console.error fail the test with a spy that throws on the first argument; that catches validateDOMNesting and the React 19 hydration messages in one pass. Compare rendered markup with the expected HTML using Diff when a hydration error names an element that looks fine in the source.

Common Compilation/Runtime Mistakes & Version Gotchas

Fixing the warning and keeping the layout. Replacing <div> with <span> removes the block context, so margins, flex sizing, and width stop applying. Add display: block (or display: flex) in the same change, or the page shifts in production while the console is clean.

Suppressing the warning with dangerouslySetInnerHTML. Hand-written HTML bypasses the check and produces the same invalid DOM, now invisible to React and your tests.

Assuming production hides the bug. React 18 strips the warning in production builds, but the browser still repaired the DOM, so hydration mismatches, lost event handlers, and wrong :nth-child styling remain. Treat any nesting warning as a real defect.

Version differences worth knowing. React 18 prints Warning: validateDOMNesting(...) and continues; React 19 reports In HTML, <div> cannot be a descendant of <p>. This will cause a hydration error. and re-renders the subtree on the client. The React 19 message includes the offending source element when the component has a key or is traceable in the stack, so upgrading React turns a noisy warning into a failing test — plan for that when migrating. Server-rendered HTML from a framework that sanitizes differently (Next.js use client boundaries, for example) can also change which side reports the error.

Checking the emitted HTML. Server-rendered pages can be verified directly: fetch the page and look for a block element inside a paragraph, or format the markup with the HTML-aware JSON tools and the regular expression tester to find <p[^>]*>(?:(?!</p>).)*<(div|ul|table) across a template corpus.

Reference material: React DOM common components and HTML specification, the p element.

Official references

primary documentation

The React documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit React work — they run entirely in this browser tab, so nothing you paste leaves your device.

All React guides

More in React

All React guides

The guides filed next to this one in React, in library order.