Inline & Block — HTML

A block element starts on a new line and uses the whole line (<div>, <p>, <h1>). An inline element stays on the same line as the text around it (<span>, <a>…

You've already met two ways elements appear on a page. Every <p> starts on a new line, so paragraphs go one below the other. <strong> stays in the middle of the sentence, and only the words it wraps turn bold. These two ways have names:

<span>: the inline wrapper

A <span> wraps part of a sentence, like one word or one price:

<p>Coconut rice is only <span>$1</span> a plate.</p>

The result: the sentence appears complete on one line, exactly as it would without the <span>. That is how it should be. A <span> doesn't change how the text looks, and it has no meaning. Its only job is to mark that part, so that CSS (the language that sets how a page looks) can later change the look of that part only, for example its colour.

<div>: the block wrapper

A <div> wraps a whole part of the page, like a few paragraphs that belong together:

<div>
  <p>Coconut rice $1.</p>
  <p>Sweet tea $0.30.</p>
</div>

In the browser, the two paragraphs look the same as usual. The <div> itself is invisible, just like a <span>. Its job is also only to mark, for CSS later. There is only one difference: a <div> is block, and a <span> is inline. So wrap part of a sentence in a <span>, and a whole part of the page (a block) in a <div>.

Same line or a new line

The difference between block and inline is easy to see when you write <span>s and <div>s one after the other. Guess before you read the result: how many lines do these four lines of code make on the page?

<span>Coconut rice</span>
<span>Sweet tea</span>
<div>Coffee</div>
<div>Orange juice</div>

The result is three lines. Coconut rice and Sweet tea share the first line, because a <span> is inline. Coffee and Orange juice each get a line of their own, because every <div> starts on a new line.

So if you wrap one word in the middle of a sentence in a <div>, the sentence breaks. That word moves down to a line of its own, and the rest of the sentence moves to the next line.

The tags you already know, in two groups:

Nesting <div>, <p> and <span>

A <div> can hold paragraphs, headings, lists, even other <div>s. A <p> can only hold text and inline elements like <span>. So they fit together like boxes inside boxes: the <div> outside, the <p> in the middle, the <span> deepest inside:

<div>
  <p>Today it costs <span>$15</span>, tomorrow <span>$20</span>.</p>
</div>

What shows is one whole sentence: Today it costs $15, tomorrow $20. An empty <span>, <span></span>, marks nothing. So always write some text between <span> and </span>.

⚠️ Pick the wrong wrapper and the sentence breaks

💡 Later in CSS: display

Block and inline are only the default ways to appear. CSS can change them with a setting called display. display: block makes an element move down to its own line. display: inline keeps an element on the same line. display: inline-block also keeps an element on the same line, but you can set its width and height. You'll meet display again when you learn CSS.

TL;DR: A block element starts on a new line and uses the whole line (<div>, <p>, <h1>). An inline element stays on the same line as the text around it (<span>, <a>, <strong>). Wrap part of a sentence in a <span>, a whole part of the page in a <div>. From outside to inside: a <div> holds a <p>, and a <p> holds <span>s.

⚡ Same line or a new line

Write:

<span>Coconut rice</span>
<span>Sweet tea</span>
<div>Coffee</div>
<div>Orange juice</div>
Guess first: how many lines do these four lines of code make on the page?

Three. Coconut rice and Sweet tea share the first line, because a <span> stays on the same line. Coffee and Orange juice each get a line of their own, because every <div> starts on a new line.

Meaning: A <span> is inline: it stays on the same line as the text around it, like <a> and <strong>. A <div> is block: it always starts on a new line, like <p> and <h1>.

⚡ A <span> for part of a sentence

Write:

<p>Coconut rice is only <span>$1</span> a plate.</p>

Meaning: The sentence appears complete, exactly as it would without the <span>. The <span> only marks the price, so CSS can later change the look of that price only. If you wrap one word in a <div> instead, the sentence breaks, because a <div> always starts on a new line.

⚡ Boxes inside boxes

Write:

<div>
  <p>Today it costs <span>$15</span>, tomorrow <span>$20</span>.</p>
</div>

Meaning: A <div> can hold a <p>, and a <p> can only hold text and inline elements like <span>. Not the reverse: a <div> inside a <p> makes the browser close the paragraph early.

What you will learn