← Back to HTML & CSS, page 8

html-css-0156 · Hard senior

A div inside a table jumps out above it, and a p with a div leaves an empty paragraph. What rules cause this?

Inside a table, content that is not a table part is moved before the table (foster parenting). A p is closed when a block element starts, and the later end tag creates an empty paragraph. A link cannot contain another link, and interactive elements should not be nested.

Full answer

When markup is nested in a way that the content model does not allow, the parser applies recovery rules. Three of them often puzzle people.

Foster parenting: inside a table, only table parts are allowed. If other content, such as a div, text or a span, appears where the parser expects a row or a cell, the parser moves that content to just before the table. Text between rows ends up above the table, and a stray div sits above it. A common cause is a template or a component that wraps rows in a div.

Paragraph closing: a p cannot contain block-level content such as div, ul, table or h2. When one starts, the parser closes the open p first. A later </p> then has no open paragraph, so the parser creates an empty one. This is why a stray empty paragraph with a margin sometimes appears in the layout.

Nested interactive content: an a cannot contain another a. The second start tag closes the first. A button inside an a is not repaired by the parser, but it is invalid, because it creates two controls for one click and confuses keyboard and screen reader users. The same goes for a form inside a form, where the inner form tag is ignored.

How to fix these:

  • Keep table content to rows and cells. Put wrappers outside the table, or use CSS grid with proper roles in place of a real table when the structure must be flexible.
  • Use div or section as a wrapper for block content and keep the p for text.
  • Run the page through an HTML checker, and compare the DOM with the source when the layout is strange.

The div is moved before the table, so it is the table's previous sibling. The paragraph is closed by the div, so the block is a sibling of the paragraph, and the final </p> makes one more empty paragraph after it. The two anchors become two separate links.

These are rules from the specification, so all current browsers agree.

html
<table id="t"><div id="d">Misplaced</div><tr><td id="c">cell</td></tr></table>
<p id="p">Text<div id="dv">Block</div>more</p>
<a href="#one" id="a1">One <a href="#two" id="a2">Two</a></a>

The page was parsed by Chrome 156 and the resulting DOM was read. Other parsers follow the same HTML standard, but only Chrome was run. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Foster parenting moves stray table content above the table.
  • Block elements close an open p.
  • A stray </p> creates an empty paragraph.
  • Nested links are split by the parser.
  • Compare the DOM with the source when layout looks wrong.

Common mistakes

  • Wrapping table rows in a div in a component and finding the wrapper above the table.
  • Putting a div or ul inside a p.
  • Placing a button or a link inside another link.

Follow-ups

  • What happens to text placed between table rows?
  • Why does a stray empty paragraph appear after a div inside a p?

Tips

  • Name foster parenting and paragraph closing, and say what each moves or creates.
  • Add that nested interactive content is a separate accessibility problem.