Sub topic 8
Block-level and inline content, and valid nesting
This chapter assumes you can write a basic HTML document, and that you know what elements, tags and attributes are. It also assumes you have used headings, paragraphs and inline text elements such as em and strong. Here you will learn how browsers group elements into block-level and inline content, and which nesting combinations are valid.
Two default display behaviors
Most elements have a default display behavior that affects how the browser lays them out.
- Block-level elements start on a new line and stretch across the available width. Examples are
p,h1toh6,ul,ol,li,div,tableandform. - Inline elements sit inside a line of text and take only the width their content needs. Examples are
a,em,strong,span,codeandimg.
These names describe default rendering, which CSS can change. The HTML specification does not use "block" and "inline" to decide what may go inside what. It uses content models instead, described below.
Content models
Every element has a content model, which states what it may contain. Two broad categories are useful in practice.
- Flow content covers most things you put in the body, including text, paragraphs, lists and inline elements.
- Phrasing content is the text itself and the elements that mark up text within a paragraph, such as
em,aandspan.
A p element may contain only phrasing content. An element such as div or li may contain flow content, so it can hold paragraphs, lists and other blocks.
Valid nesting rules
Three habits prevent most nesting errors.
- Close elements in the reverse order you opened them. The inner element must end before the outer one.
- Put blocks inside blocks, and inline elements inside either. Do not put a paragraph or heading inside a
p. - Respect the parent-child rules of specific elements. A
ulorolmay have onlylichildren, and atrbelongs inside a table section.
<!-- Valid -->
<p>Use <strong>clear <em>headings</em></strong> in every page.</p>
<!-- Invalid: the end tags overlap -->
<p>Use <strong>clear <em>headings</strong></em> in every page.</p>The overlapping version is an error. Browsers will repair it, but the result may not match what you intended.
Elements that cannot be nested
Some elements may not contain themselves or certain others, even though they look harmless.
- A
pcannot contain anotherp, a heading, a list or adiv. - An
acannot contain anothera, or interactive content such as abutton. - A
buttoncannot contain a link or another control.
When a browser meets a start tag that is not allowed inside an open p, it closes the p automatically. This is why a block inside a paragraph produces surprising results.
<!-- Invalid: a list inside a paragraph -->
<p>Ingredients:
<ul>
<li>Flour</li>
</ul>
</p>
<!-- Valid -->
<p>Ingredients:</p>
<ul>
<li>Flour</li>
</ul>In the invalid version, the browser ends the paragraph at the ul. The stray </p> then creates an extra empty paragraph.
Wrapping blocks in links
An a element is allowed to wrap block content when its parent permits flow content. This is useful for making a whole card clickable.
<a href="/articles/tomatoes">
<h2>Growing tomatoes</h2>
<p>Tomatoes need sun and regular watering.</p>
</a>This is valid in current HTML. Older HTML 4 rules forbade it.
Generic containers: div and span
div is a generic block container and span is a generic inline container. Neither carries meaning. Use them for styling or scripting hooks only when no element with the right meaning exists. Prefer semantic elements such as article or em first.
| Need | Better choice | Fallback |
|---|---|---|
| Group blocks for styling | `section`, `article` | `div` |
| Mark emphasized words | `em`, `strong` | `span` |
Common errors
- Putting a heading or list inside a
p. - Overlapping end tags.
- Placing text or a
divdirectly inside aul. - Nesting one link inside another.
Run your page through a validator to catch these. It reports nesting problems that browsers silently repair.
Exercise
This markup has a nesting error: <p>See <a href="/a">this <strong>page</a></strong>.</p>. What is wrong, and how do you fix it?
Show answer
The end tags overlap, because strong opens inside a but </a> closes first. Close the inner element first: <p>See <a href="/a">this <strong>page</strong></a>.</p>.
Exercise
Why does <p>Steps: <ol><li>Open</li></ol></p> render with an extra empty paragraph?
Show answer
An ol is not allowed inside a p, so the browser closes the paragraph when it reaches the ol. The later </p> then produces an empty paragraph. Put the ol after a closed p.