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.

Introduction & Quick Summary
React 18 logs a warning naming two elements and their relationship:
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:
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:
<!-- 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:
<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:
| Parent | Accepts | Rejects | Typical 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:
| Fix | Change | Keeps semantics | Use when |
|---|---|---|---|
| Change the parent | <p> → <div> | partly: loses paragraph semantics | the block truly belongs in the container |
| Change the child | block → <span> with display: block | yes | the child is inline content styled as a block |
| Split the parent | close <p> and open a <div> after it | yes | a paragraph followed by a list or code block |
What each element change costs:
| Change | Screen reader effect | CSS risk | Hydration |
|---|---|---|---|
<p> to <div> | no paragraph role; margin comes from CSS | inherits div selectors | none |
<div> to <span> + display: block | same text, no block role | needs explicit display | none |
<a> to <span role="link"> | loses keyboard activation unless added | pointer cursor required | none |
<button> wrapping <div> to <div role="button"> | loses native keyboard handling | must re-add tabIndex and keydown | none |
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:
// 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:
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.
3. Nested links in cards and navigation
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:
<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>:
// 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 documentationThe React documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit React work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related React guides
All React guidesReact 19: Support for defaultProps will be removed
React 19 ignores defaultProps on function components, so a default becomes undefined. Move defaults into the parameter list and handle null honestly.
React: Each child in a list should have a unique key prop
Keys tell React which item is which between renders. Use stable ids, avoid index keys and put the key on the mapped element, not on a wrapper.
react-hook-form with zod 4: resolver type errors after the upgrade
Zod 4 split input from output and resolvers v5 re-typed Resolver. Drop the generic, or pass all three, and the errors disappear.
React Profiler API: Measuring Render Timings and Identifying Wasted Re-Renders
actualDuration versus baseDuration, phase names that tell you who triggered a commit, and how to ship the numbers without flooding a backend.
More in React
All React guidesThe guides filed next to this one in React, in library order.