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:
- Block, like
<p>: always starts on a new line and uses the whole line, even when its content is short. - Inline, like
<strong>: stays on the same line as the text around it, and is only as wide as its content.
<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>
<p>and</p>wrap the whole sentence.<span>is the opening tag, right before the part you want to mark.$1is the part being marked.</span>is the closing tag, right after it.
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>
<div>opens the group, and</div>closes it.- The two
<p>s inside are indented (they have spaces in front), so you can see they are inside the<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:
- Block:
<div>,<p>,<h1>to<h6>,<ul>,<ol>,<li>,<pre>. - Inline:
<span>,<a>,<strong>,<em>,<b>,<i>,<code>,<kbd>.
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>
- Each price has its own
<span>. Two prices, two<span>s. - The comma and the full stop stay outside the
<span>s, because they aren't part of the prices.
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
- A
<div>to mark one word. The sentence breaks. Use a<span>. - A
<div>inside a<p>. The browser closes the paragraph right before the<div>, so the<div>and the text after it are outside the paragraph. Put the<p>inside the<div>, not the reverse. - One
<span>for two parts. If you want to mark two parts separately, each part needs its own<span>.
💡 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.