Sub topic 8
Block-level and inline content, and valid nesting
This chapter assumes you can write a complete HTML document and recognise an element's start tag, content, end tag and attributes. You should also know the common text elements: headings, paragraphs and inline text such as strong, em and a. Here you will learn how elements are grouped by what they may contain, how the browser's parser reacts when you break those rules, and how to find nesting errors.
Two ideas that get mixed up
Most tutorials split elements into "block" and "inline". The split is useful, but it combines two separate things.
- Display behaviour is how the browser draws an element by default. This is a CSS matter. A block-level box starts on a new line and stretches across the available width. An inline box flows within a line of text and is only as wide as its content.
- Content model is which elements the HTML specification allows inside which other elements. This is an HTML matter, and it decides whether your markup is valid.
The HTML Standard no longer uses "block-level" and "inline-level" as formal categories. It defines content categories instead: flow content, phrasing content, heading content, sectioning content, embedded content and interactive content. The older words still describe what you see on screen, so this chapter uses both. When it matters, it says which one it means.
| Informal term | Standard's category | Typical elements |
|---|---|---|
| Block-level | Flow content (the block-like members) | `p`, `div`, `h1`–`h6`, `ul`, `ol`, `table`, `section`, `form` |
| Inline | Phrasing content | `a`, `em`, `strong`, `span`, `code`, `img`, `br` |
Phrasing content is the text of the document plus the elements that mark up that text inside a paragraph. Flow content is almost everything that can appear in body, so phrasing content is a subset of flow content.
How block and inline differ on screen
Here is a small page with one of each kind of element.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Block and inline</title>
</head>
<body>
<p>First paragraph with <em>emphasis</em> and <code>code</code>.</p>
<p>Second paragraph.</p>
<div>A generic block container.</div>
<span>One span.</span> <span>Another span.</span>
</body>
</html>The two p elements and the div each begin on a new line. The browser adds vertical space around the paragraphs by default. The em and code elements stay inside the first line of their paragraph. The two span elements sit side by side on one line, because span is inline.
A few differences follow from this default behaviour.
- Block boxes respect
width,heightand vertical margins. Inline boxes ignorewidthandheight, and their vertical margins do not push other lines away. - The line break between two inline elements in your source becomes a single space on screen. That is why
<span>One</span> <span>Two</span>shows a gap. Whitespace between block elements has no visible effect. - Several consecutive spaces and newlines in your source collapse into one space in the rendered text.
Replaced inline elements such as img are a special case. They are inline by default but do accept width and height, as the image example in the topic's earlier code shows.
The two content models that matter most
Every element has a content model that says what may go inside it. Two rules cover most of what beginners need.
- Elements that hold flow content may contain block-level and inline elements. Examples are
body,div,section,article,li,td,blockquoteandmain. - Elements that hold only phrasing content may contain text and inline elements, but not block-level elements. Examples are
p,h1–h6,span,em,strong,code,buttonandlabel.
In practice, a paragraph is the leaf of the block structure. It holds text and inline markup. A heading works the same way.
This is valid:
<div>
<h2>Shopping</h2>
<p>Buy <strong>fresh</strong> bread.</p>
<ul>
<li>Bread</li>
<li>Milk, <em>semi-skimmed</em></li>
</ul>
</div>The div holds a heading, a paragraph and a list. The p holds text and a strong. The li elements hold text and an inline em. Every container is allowed to hold what it holds.
Rules that restrict nesting further
The flow and phrasing rule is only the start. Some elements have narrower models.
| Element | What it may contain |
|---|---|
| `ul`, `ol` | Only `li` elements as direct children (plus script-supporting elements such as `script` and `template`) |
| `table` | `caption`, `colgroup`, `thead`, `tbody`, `tfoot` and `tr` in a set order |
| `tr` | Only `td` and `th` |
| `a` | Transparent content, but no interactive content inside (no nested `a`, no `button`) |
| `button` | Phrasing content, but no interactive content inside |
| `p` | Phrasing content only |
| `head` | Metadata elements such as `title`, `meta` and `link` |
"Transparent" means the element takes the content model of its parent. An a inside a p may hold only phrasing content, but an a directly inside a div may wrap a whole block, such as a heading and a paragraph. This lets a card be one link, which HTML5 allows. A link must still never contain another link or a button.
What the parser does with invalid nesting
HTML parsers do not stop at an error. The specification defines exactly what to build for broken markup, so every modern browser produces the same tree. The result can differ a lot from what you meant, so it is worth knowing the common cases.
Block element inside a paragraph
The p end tag is optional, and a p is closed automatically when certain block elements start. So this markup:
<p>Intro text
<div>A box</div>
</p>is parsed as an open p that ends before the div, then the div, then a stray </p>. The stray end tag creates a new, empty p. The browser's DOM shows three nodes where you wrote one p. Nothing visibly breaks, but an empty paragraph can add unwanted space once CSS is applied.
Lists with the wrong children
<ul>
<li>One</li>
<p>Not a list item</p>
<li>Two</li>
</ul>Browsers keep the p inside the ul. It renders, but the markup is invalid, and assistive technology may announce the list incorrectly. If you need extra text between items, put it inside an li, or end the list and start a new one.
Mis-nested inline tags
<p><strong>Bold <em>and italic</strong> only italic?</em></p>The tags overlap. Elements must close in the reverse order they opened. The parser repairs the overlap with an "adoption agency" algorithm. It closes and reopens the formatting elements to make a valid tree, so the output usually looks as you hoped but the markup is still wrong. The correct form is:
<p><strong>Bold <em>and italic</em></strong><em> only italic.</em></p>Nested links and forms
A link inside a link is invalid, and the parser closes the first a when it meets the second. A form inside another form is also invalid, and the parser ignores the inner start tag. Both mistakes appear when pages are built by combining templates, so check them when a rendered page differs from the source.
Seeing the parse result
You can compare your source with what the browser built. Open the page, then open the developer tools and look at the Elements panel (or Inspector in Firefox). This panel shows the DOM after parsing, not the source file. If a p you did not write appears, or a tag has moved, you have nesting that the parser repaired.
You can also check this with a short script. Paste the following into the browser console on any page. It parses a string the same way the browser parses a page and prints the result.
const doc = new DOMParser().parseFromString(
"<p>Intro <div>Box</div></p>",
"text/html"
);
console.log(doc.body.innerHTML);The output is <p>Intro </p><div>Box</div><p></p>. It shows the auto-closed paragraph and the extra empty one.
Choosing between `div`, `span` and semantic elements
div and span have no meaning of their own. div is the generic flow container and span is the generic phrasing container. Use them when no more specific element fits, usually as a hook for CSS or scripts.
- Prefer
p,ul,section,article,navand similar elements when one describes the content. A later chapter covers semantic layout in detail. - Use
spanfor a short run of text that needs a class or attribute, such as<span class="price">4.99</span>. - Do not use
divin place of a list, heading or paragraph. A column ofdivelements gives assistive technology no structure to announce.
Void elements and line breaks
Void elements such as br, hr, img, input and meta have no content and no end tag. br and img are phrasing content. hr is flow content only, so it cannot sit inside a p.
Use br only when the line break is part of the content, as in an address or a poem. Do not use it to create vertical space or to separate paragraphs. Use separate p elements for paragraphs and CSS for spacing.
<p>
221B Baker Street<br>
London<br>
NW1 6XE
</p>A short checklist for valid nesting
- Close elements in the reverse order you opened them.
- Put block-level elements inside flow containers, never inside
p, a heading or an inline element. - Give
ulandolonlylichildren, andtronlytdorth. - Never put an
ainside ana, or abuttoninside anaor anotherbutton. - Use
divandspanonly when no meaningful element fits. - Compare the Elements panel with your source when the page looks wrong.
A validator, covered in the final chapter, catches all of these automatically. Until then, the checklist and the developer tools are enough to find most problems.
Exercise
This markup has three nesting problems. Name each one and write the corrected version. <ul> <li>Fruit</li> <p>Vegetables</p> </ul> <p><a href="/shop">Visit <a href="/sale">the sale</a></a></p> <p><em>Fresh <strong>bread</em></strong></p>
Show answer
Problem 1: a p is a direct child of ul, but ul may contain only li elements. Problem 2: an a is nested inside another a, which is not allowed. Problem 3: the em and strong tags overlap instead of closing in reverse order. Corrected: <ul> <li>Fruit</li> <li>Vegetables</li> </ul> <p><a href="/shop">Visit</a> <a href="/sale">the sale</a></p> <p><em>Fresh <strong>bread</strong></em></p>
Exercise
You write <p>Notes <div>Details</div></p> and open the page. In the Elements panel you see two p elements and one div. Why are there two p elements, and which one is empty?
Show answer
A p start tag is closed automatically when a div start tag appears, so the first p holds only "Notes". The div is then built as a sibling. The final </p> has no open paragraph to close, so the parser creates a new p for it. That second p, which follows the div, is empty.