Lesson 28 of 51 · Module 5, lesson 1 of 7 · 4 min

Block and inline elements

See how elements sit on the page by default, and which elements can go inside which.

What you'll learn

  • What block and inline elements are
  • Which elements you know are block, and which are inline
  • What can go inside what

Why this matters

Every element has a default way of sitting on the page. Some stack on top of each other; others flow inside a line of text. Knowing which is which explains a lot of "why is it on a new line?" moments — and it tells you which elements may go inside which.

Two ways to sit on a page

Block elements stack like bricks, each taking the full width: an h1 "Tomato soup", a p paragraph and a ul list, one under the other. Inside the paragraph, inline elements such as strong and a sit inside the line, like words, taking only the space they need. Block elements include h1 to h6, p, ul, ol, table, div and section. Inline elements include strong, a, em, code and span.

  • A block element starts on a new line and takes the full width available. The next element starts on a new line after it.
  • An inline element sits inside a line of text, like a word. It takes only as much width as it needs.
html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Block and inline</title>
</head>
<body>
  <h1>Tomato soup</h1>
  <p>Ready in <strong>30 minutes</strong>. Read the <a href="#steps">steps</a> below.</p>
  <p>Serves <em>four</em> people.</p>
</body>
</html>

What you'll see: the heading and the two paragraphs are each on their own line. The bold words, the link and the italic word stay inside their sentences.

The page with the heading "Tomato soup" and two paragraphs on separate lines. Inside the first paragraph, "30 minutes" is bold and "steps" is a link, both within the same line of text.

See it for yourself

Open the example in your browser, open the developer tools, and point at elements in the Elements panel. A block element's highlight stretches across the whole width. An inline element's highlight wraps only its words.

Which is which

Block (new line, full width)Inline (inside a line)
<h1>–<h6>, <p><a>, <strong>, <em>
<ul>, <ol>, <li>, <dl><code>, <kbd>, <abbr>, <q>
<table>, <blockquote>, <pre><br>, <img> (images sit in the line like a big letter)
<figure>, <hr>, and the elements in the next lessons<span> (next lesson)

What can go inside what

Two rules cover almost every case:

  1. A paragraph holds only inline content. No headings, lists, tables or other paragraphs inside a <p>. If you put one there, the browser closes the paragraph early — and the page no longer matches your code.
  2. A link can wrap a block, such as a whole card with a heading and text — but never another link.

Looks can be changed

"Block" and "inline" are the browser's default display. With CSS you can make a list sit in one line, or a link behave like a block. So choose elements for their meaning; change the look later.

Try it yourself

  1. Select the example and choose Try it.
  2. Put an <h2> inside the first paragraph. In the preview, the heading breaks the paragraph apart.
  3. Open the developer tools on your own copy and compare with your code: the browser closed the <p> before the <h2>.

Common mistakes

  • A heading, list or table inside <p>. The browser ends the paragraph early. Fix: close the <p> first.
  • Expecting two block elements to sit side by side. They stack by default. Fix: that is a CSS job; leave the HTML as it is.
  • A link inside a link. Browsers cannot handle it. Fix: one link at a time.

Check yourself

  1. Does <strong> start a new line?
  2. Can a <ul> go inside a <p>?
  3. Name two block elements and two inline elements.

Answers

  1. No. It is inline and stays inside the text.
  2. No. A paragraph can hold only inline content.
  3. For example block: <p>, <h2>; inline: <a>, <em>.

Next: when no element describes your content, HTML has two plain containers — one block and one inline.

Chat with us on WhatsApp