Block & Inline Elements
Why does a heading always start on a new line, while a link sits happily in the middle of a sentence? Every element has a default way of flowing on the page. Most are either block or inline, and understanding the difference explains a lot of the layout behaviour you'll see before you write any CSS.
Block elements
A block element:
- starts on a new line,
- stretches to fill the full width of its container, even if its content is short,
- stacks vertically, one below the other.
Common block elements: <h1>–<h6>, <p>, <div>, <ul>, <ol>, <li>, <table>, <form>, <blockquote>, <pre>, <hr>, and the layout elements <header>, <nav>, <main>, <section>, <article>, <aside> and <footer>.
Inline elements
An inline element:
- does not start a new line; it flows inside the text around it,
- is only as wide as its content,
- wraps onto the next line like ordinary words when it reaches the edge.
Common inline elements: <a>, <span>, <strong>, <em>, <code>, <abbr>, <br>, and also <img>, <input> and <button>.
See it in action
The example below adds a temporary border to every element so you can see the boxes. The <style> section is CSS, which you'll learn in the CSS series; for now, just notice the shapes.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Block and inline</title>
<style>
h1, p, div { border: 2px solid steelblue; }
a, strong, span { border: 2px dashed orange; }
</style>
</head>
<body>
<h1>Short heading</h1>
<p>A paragraph with a <a href="#">link</a>, some <strong>strong text</strong>
and a <span>span</span> flowing in the same line.</p>
<div>A div is a block too.</div>
<a href="#">Link one</a> <a href="#">Link two</a>
</body>
</html>
The solid blue boxes around the heading, paragraph and div each stretch right across the preview, even though "Short heading" is short. The dashed orange boxes hug their words and sit within the line. The last two links appear side by side on the same line, because inline elements don't force line breaks.
Rules for putting elements inside each other
The HTML standard doesn't actually use the words "block" and "inline" for elements; those are CSS display values. Instead, it sorts elements into content categories. The two you need most are:
- Flow content: almost everything that can go in
<body>, including paragraphs, headings, lists and divs. - Phrasing content: the text and text-level elements inside a paragraph, such as
<strong>,<a>,<span>and<img>.
Some elements, including <p>, <h1>–<h6> and <span>, may only contain phrasing content. So:
<!-- Fine: inline elements inside a paragraph --> <p>Read the <a href="rules.html">club rules</a> <strong>today</strong>.</p> <!-- Invalid: a div inside a paragraph --> <p>Intro text <div>A box</div> more text</p>
In the invalid example, the browser does something surprising: when it meets <div>, it automatically closes the open paragraph. "A box" ends up outside the paragraph, " more text" is left as loose text, and the final </p> creates a new, empty paragraph. Your CSS for p then misses half the content. Check your nesting with the browser's Inspect tool or the W3C validator.
One useful exception: the link element <a> takes on the rules of its parent, so it may wrap block content such as a whole card containing a heading and paragraph, as long as there are no other links or buttons inside it.
CSS can change the display
Block and inline are only defaults. CSS's display property can change them: a list of links can be shown in a row, or an inline link can be made into a large block-shaped button. Three common values are block, inline and inline-block (flows in a line, but accepts a width and height like a block). See Display & Visibility in the CSS series.
Changing the display with CSS changes how something looks, not what it means. A <span> styled as a block is still a span, so choose elements for their meaning and leave the appearance to CSS.
Key points
- Block elements start on a new line and fill the available width; examples are
<p>,<h1>,<div>and<ul>. - Inline elements flow within text and are only as wide as their content; examples are
<a>,<span>and<strong>. - Paragraphs and headings may contain only phrasing (inline-level) content; a
<div>inside a<p>breaks the paragraph. - CSS's
displayproperty can change how an element flows without changing its meaning.
Tip: use the ← and → keys to move between pages.
