Block, Inline, and the Flow of the Document: Fixing Broken Layouts at the HTML Level
Block and inline elements in HTML5: content models, parser rewrites of invalid nesting, the p and div trap, links around blocks, and whitespace gaps.

A developer wraps a promo box in a paragraph: <p class="promo"><div class="badge">New</div> Try the beta</p>. The styles for .promo .badge never apply. DevTools shows an empty <p class="promo"></p>, then the div, then the text, then a second empty <p></p> that appears nowhere in the source. Nobody wrote that markup. The parser did.
Block and inline elements were the HTML 4 way of describing which elements could contain which: block elements like div and p started new lines, inline elements like span and a sat inside text, and inline elements could not contain blocks. HTML5 replaced that pair with content models, and separated them cleanly from CSS. Whether an element looks block or inline is now purely a matter of its CSS display value. Whether it may contain another element is a matter of the content model, and breaking that rule changes the DOM itself.
Which content categories matter in HTML5?
The HTML specification sorts elements into overlapping categories. Two of them cover most everyday nesting questions.
| Category | Roughly means | Examples |
|---|---|---|
| Flow content | Almost anything that can appear in the body | div, p, ul, section, table, form, plus all phrasing content |
| Phrasing content | Text and the elements that mark up text within a paragraph | span, a, em, strong, img, button, input, code |
| Interactive content | Elements the user can activate | a with href, button, input, select, details |
| Heading content | Headings | h1 to h6 |
| Sectioning content | Elements that define a section | article, section, nav, aside |
Each element then has a content model saying what it accepts. A <div> accepts flow content. A <p> accepts only phrasing content. A <ul> accepts only <li> (plus script-supporting elements). MDN lists the “Permitted content” for every element when you need to check.
When does the parser rewrite your markup?
HTML parsing never fails. Instead of rejecting invalid markup, the parser follows precise error-recovery rules that every browser implements the same way. That consistency is good, but the result is a DOM that differs from your source, and CSS and JavaScript only ever see the DOM.
Block content inside a paragraph
A <p> end tag is optional, so when the parser sees a <div>, <ul>, <table>, heading or other element that cannot live in a paragraph, it closes the open paragraph first. The stray </p> later in the source has no open paragraph to close, so the parser creates an empty one.
<!-- You wrote -->
<p class="promo"><div class="badge">New</div> Try the beta</p>
<!-- The DOM contains -->
<p class="promo"></p>
<div class="badge">New</div>
" Try the beta"
<p></p>
The fix is in the HTML: make the outer element a div, or make the badge a span and style it with display: inline-block. Content management systems produce this bug constantly, because rich text editors wrap everything in paragraphs and authors paste blocks into them.
Paragraphs inside paragraphs
<p>One <p>Two</p></p> becomes two sibling paragraphs and one extra empty paragraph. The same auto-close rule applies.
Content inside tables but outside cells
Text or elements placed directly inside <table> or <tr>, outside any <td> or <th>, are “foster parented”: the parser moves them to just before the table. A loading message placed inside a table body ends up above the table, and a <form> wrapped around table rows ends up empty. Put forms around the whole table, or inside a single cell.
Interactive elements inside interactive elements
<!-- Invalid: a button inside a link -->
<a href="/product/42">
Wireless Headphones
<button type="button">Add to wishlist</button>
</a>
Links and buttons may not contain other interactive content. A nested <a> inside another <a> is actually split apart by the parser. A button inside a link usually stays in the DOM, but clicking it activates both, keyboard users get two tab stops with confusing names, and screen readers announce the button text as part of the link name. Make the link and the button siblings, and if the whole card must be clickable, use a pseudo-element to stretch the link over the card rather than wrapping everything in it.
Can a link wrap block elements?
One HTML5 change goes the other way. The <a> element has a transparent content model: it may contain whatever its parent may contain, as long as there is no interactive content inside. So this is valid:
<a href="/blog/semantic-html" class="post-card">
<h3>Semantic HTML Explained</h3>
<p>Why a div is not a layout strategy.</p>
</a>
Valid is not the same as ideal. Screen readers read the entire contents as the link’s name, so a large card becomes a very long link. For cards with a lot of text, link the heading and stretch its clickable area with CSS instead.
Is block or inline decided by HTML or CSS?
A common source of confusion is mixing up the two meanings of “inline”. The browser’s default stylesheet gives span and a a display of inline, and gives div and p display: block. You can change those freely. A span with display: block is perfectly valid, and so is a li with display: inline. What you cannot do with CSS is change the content model: a span styled as a block still may not contain a div, and changing its display does not stop the parser rules above.
So when you need a block-level box inside a paragraph, use a phrasing element and style it. When you need a container for blocks, use a flow container. Keep the two decisions separate.
Why does whitespace create gaps in inline flow?
Inside inline formatting, line breaks and spaces in your source become a single space on screen. That is why inline-block navigation items have a mysterious gap of about four pixels between them:
<nav>
<a class="tab" href="/a">Overview</a>
<a class="tab" href="/b">Pricing</a>
</nav>
.tab { display: inline-block; } /* a space appears between the tabs */
The gap is the newline between the two links, rendered as a space in the parent’s font. Old fixes included removing whitespace from the HTML or setting font-size: 0 on the parent. The clean fix is to make the parent a flex or grid container. Flex and grid items are blockified and whitespace-only text between them is ignored, so you control spacing explicitly with gap.
The same inline behavior causes the small gap under images: an <img> is inline, sits on the text baseline, and leaves room below for descenders. Set display: block on standalone images. The CSS side of this is covered in the box model, display, and positioning guide.
Line breaks and empty elements
<br>is for line breaks that are part of the content, such as lines of an address or a poem. Stacking<br><br>for spacing is a layout decision hiding in the markup; use margins.<wbr>marks an optional break point in long words or URLs, which helps on narrow screens without inserting a visible hyphen.- Empty
<p></p>and<div> </div>spacers create blank lines for screen reader users, who may hear “blank” as they navigate. - The
hiddenattribute removes an element from rendering and the accessibility tree, likedisplay: none, but any CSS that setsdisplayon the element overrides it. Add[hidden] { display: none !important; }to your base styles to make it reliable.
Debugging at the HTML level
- Compare source with DOM. View Source shows what the server sent; the Elements panel shows what the parser built. If they differ, nesting is the cause.
- Validate. The W3C Nu HTML Checker flags every content-model violation with a line number. Run it on the rendered output of templates, not just the template files.
- Look for empty elements you did not write, especially
<p></p>. They are fingerprints of auto-closing. - Check CMS output. Rich text editors and shortcode plugins frequently wrap block content in paragraphs.
- Only then touch the CSS. A selector that does not match because the DOM changed will never be fixed by adding specificity.
Frequently Asked Questions
What is the difference between block and inline elements in HTML5?
HTML5 no longer defines block and inline elements. It defines content models, such as flow content and phrasing content, that control what may be nested where. Whether an element displays as a block or inline box is controlled by its CSS display value, which can be changed freely.
Can I put a div inside a p tag?
No. A paragraph may contain only phrasing content. When the parser meets a div inside an open paragraph, it closes the paragraph early, so the div ends up outside it and an extra empty paragraph appears where the closing tag was.
Can an a tag contain block elements like div or h3?
Yes, in HTML5. The a element has a transparent content model, so it can contain whatever its parent allows, including headings and paragraphs, as long as nothing inside it is interactive, such as another link or a button.
Why is there a gap between my inline-block elements?
The whitespace between the elements in your HTML is rendered as a space in inline formatting. Make the parent a flex or grid container and use gap for spacing, which ignores whitespace-only text between items.
Fix the tree before the styles
When a selector inexplicably stops matching, or a block of content jumps outside its container, look at the DOM before the stylesheet. The parser is following rules, and once you know a handful of them, these bugs take minutes instead of hours. This builds on the element choices from semantic HTML explained: pick the right element first, then make sure it is nested where the specification allows it. For the full reference, see the content models section of the HTML Standard and MDN on block-level content. Related reading on this site: the HTML5 document skeleton.
Originally posted on 21 February 2023 and post last modified on 21 February 2023