Block vs inline elements
4 min lesson
- Every HTML element has a default display type: block or inline, which decides how it takes space on the page.
- A block element starts on a new line and takes the full width available, like a paragraph or a heading.
- An inline element flows inside text on the same line and only takes the width of its content, like a link or bold word.
- Think of block elements as full shelves and inline elements as small labels on a shelf.
- This builds on Elements & structure: you now know what an element is, and here you learn how it behaves in layout.
- The example shows a heading and two paragraphs (block) with a bold word and a link inside (inline).
- Next, Attributes & global attributes adds extra information to these elements.
- Block elements stack vertically; inline elements sit inside a line of text.
- Use block elements for structure and inline elements for parts of a sentence.
- Do not put a block element inside a small inline element like <span>.
- Try the example, then add a second paragraph to see the stacking clearly.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Block vs inline</title>
</head>
<body>
<!-- A block element: starts on its own line -->
<h1>Block vs inline</h1>
<!-- Another block element: the paragraph takes the full width -->
<p>This whole sentence is a block element.</p>
<!-- Inline elements stay inside the text flow -->
<p>This sentence has <strong>bold text</strong> and a <a href="https://example.com">link</a> inside it.</p>
</body>
</html>What happens: A page with the heading "Block vs inline", a paragraph "This whole sentence is a block element.", and a paragraph reading "This sentence has bold text and a link inside it." with "bold text" in bold and "link" underlined.
Breaking it down
Block heading
<h1>Block vs inline</h1>Headings are block elements, so they start on a new line and take the full width. The next element always begins below.
Block paragraph
<p>This whole sentence is a block element.</p>Paragraphs are block too. Two paragraphs always appear on separate lines, never side by side by default.
Inline strong
<strong>bold text</strong>Strong is inline, so it flows inside the sentence without breaking the line. It only takes the width of its words.
Inline link
<a href="...">link</a>Links are inline. They sit inside text and do not push the next content to a new line.
Block vs inline
| Type | Starts on | Width | Examples |
|---|---|---|---|
| Block | A new line | Full available width | h1, p, div, ul |
| Inline | Same line | Only its content | a, strong, em, span |
A helpful analogy
Block elements are like full shelves in a bookcase: each one takes a whole row. Inline elements are like sticky labels placed on a shelf: they sit within the row and only take the space they need.
Nesting rules
- 1.Never put a block element like <p> or <div> inside an inline element like <span> or <a>.
- 2.Inline elements may contain other inline elements, for example a link inside bold text.
Common mistakes
Expecting two inline elements to stack
Avoid
<span>A</span><span>B</span> hoping for two linesUse instead
<div>A</div><div>B</div>Inline elements stay on the same line. Use block elements when you want each item on its own line.
Putting a paragraph inside a span
Avoid
<span><p>Hi</p></span>Use instead
<div><p>Hi</p></div>A paragraph is block and does not belong inside a small inline element. The browser may break the layout.
Quick tips
- •Add a border to elements in DevTools to see their box and how much width they take.
- •If two items sit side by side unexpectedly, check whether they are inline.