Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

React warns that a div is nested inside a p tag

Error<div> cannot appear as a descendant of <p>

Short answer

A p element may contain only phrasing content, and the HTML parser closes a paragraph as soon as a div, list or other block element starts. React warns because the browser then builds a different tree than your JSX describes. Replace the p with a div, or the inner div with a span.

  • Applies to React 19, Next.js 15, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the browser console in development. It names the tags involved, so you can see which p holds the offending element.

    # find paragraphs in your source that contain a block child
    grep -rn --include=*.jsx --include=*.tsx -A3 "<p[ >]" src | grep -E "<(div|ul|ol|table|section|h[1-6])"
  2. 2Match what you see, then jump to its fix

    1. 1validateDOMNesting names the tag, and the JSX shows it between <p> and </p>A div, list, heading or table is written directly inside a pFix
    2. 2The JSX looks fine, <p><Badge /></p>, but the warning names a div under the pA child component renders a block element inside a pFix
    3. 3A hydration mismatch error on a server-rendered page, on a route that has a p holding a divThe same mistake breaks server-rendered pages as a hydration errorFix

What does the error look like?

  • <div> cannot appear as a descendant of <p>
  • Text content does not match server-rendered HTML

Cause 1 of 3

A div, list, heading or table is written directly inside a p

1Check

The content model of p is phrasing content. A p end tag may also be left out when the next tag is a div, ul, ol, table, section, heading or another p, which means the parser closes the paragraph for you.

grep -rn --include=*.tsx -B1 -A4 "<p" src/components/Card.tsx

2Fix

Make the outer element a div, or make the inner element a span if the content really is inline text.

// before
<p className="note">
  <div className="icon" /> Saved
</p>

// after
<div className="note">
  <span className="icon" /> Saved
</div>

3Verify

Reload the page. The warning is gone from the console.

npm run dev

Confirmed by html.spec.whatwg.org

Did this fix it?

Cause 2 of 3

A child component renders a block element inside a p

1Check

The warning is about rendered output, not about your JSX. Open the child component and look at its outermost element.

grep -n "return" -A3 src/components/Badge.tsx

2Fix

Change the child's root to a span, or move the child out of the paragraph.

// Badge.tsx
export function Badge({ children }) {
  return <span className="badge">{children}</span>;
}

3Verify

The console no longer names a div under a p.

npm run dev

Confirmed by nextjs.org

Did this fix it?

Cause 3 of 3

The same mistake breaks server-rendered pages as a hydration error

1Check

Next.js lists p inside p, div inside p, ul or ol inside p, and interactive content such as a inside a or button inside button as causes of hydration mismatch.

curl -s http://localhost:3000/ | grep -n -A2 "<p"

2Fix

Fix the nesting itself. Do not hide it with suppressHydrationWarning, which is meant for unavoidable differences such as timestamps and works only one level deep.

// a link inside a link, a button inside a button and p inside p
// are fixed the same way: one valid element per level
<div>
  <p>Intro text</p>
  <p>Second paragraph</p>
</div>

3Verify

Reload with the console open. The hydration error is gone.

npm run build && npm run start

Confirmed by nextjs.org

Did this fix it?

How do I stop it coming back?

  • Default to div for wrappers and keep p for text that is only text and inline elements.
  • Read the tags named in the warning to find the p and the element inside it.
  • Run the app in development and click through the main pages before a release, because the nesting warnings only print there.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.