What is <div> vs <span>?

<div> and <span> are both generic containers with no meaning. <div> is a block-level element (starts on a new line and takes the full width by default) used to group larger chunks of content for layout. <span> is an inline element (flows within text) used to wrap a few words for styling or scripting.

<div>

  • Default display: block.
  • Groups sections, cards, layout wrappers, grid and flex containers.
  • Can contain almost any flow content.

<span>

  • Default display: inline.
  • Wraps part of a text: a colored word, a badge inside a sentence, a highlighted price.
  • Must contain only phrasing (inline) content; putting a <div> inside a <span> is invalid.

Both are non-semantic

Screen readers and search engines treat them as plain containers. Before reaching for them, check whether a semantic element fits:

  • Instead of <div class="header">, use <header>.
  • Instead of <div onclick> acting as a button, use <button>.
  • Instead of <span class="bold"> for important text, use <strong>.
  • Instead of <span class="date">, use <time datetime>.

Display can be changed

CSS can make a <span> behave like a block (display: block) or a <div> inline (display: inline-flex), but the content model rules (what may go inside) stay the same.

Interview tip

Say "block vs inline", "both non-semantic", and "prefer semantic elements first".

Code example

html
1<style>
2 body { font-family: system-ui; }
3 .card { border: 2px solid #4f46e5; border-radius: 8px; padding: 12px; margin-bottom: 8px; }
4 .price { color: #16a34a; font-weight: 700; }
5 .badge { background: #fee2e2; color: #b91c1c; padding: 0 6px; border-radius: 999px; font-size: 12px; }
6</style>
7
8<!-- div: block-level grouping, each starts on a new line -->
9<div class="card">
10 <h3>Running shoes <span class="badge">Sale</span></h3>
11 <!-- span: inline, flows inside the sentence -->
12 <p>Now only <span class="price">$49</span> instead of $79.</p>
13</div>
14<div class="card">Another card on its own line</div>
15
16<p>Inline spans: <span>one</span> <span>two</span> <span>three</span></p>

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.

Open in study app