Lesson 10 of 36
HTML Tutorial

Block vs Inline, div and span

How elements flow on the page, and when to reach for the generic div and span containers.

3 min readBeginnerWeb FundamentalsInline & Block Elements

Every element has a default display behaviour. The two most common are block and inline. Understanding the difference explains why some elements stack vertically while others sit side by side, and it is the foundation of CSS layout later.

Block elements#

A block-level element:

  • starts on a new line,
  • stretches to fill the available width,
  • can contain other blocks and inline elements,
  • can have width, height, margin and padding applied in full.

Examples: <p>, <h1> to <h6>, <ul>, <ol>, <li>, <table>, <form>, <section>, <div>.

Inline elements#

An inline element:

  • stays within the flow of the text,
  • only takes as much width as its content,
  • should contain only text or other inline elements,
  • ignores top and bottom margins and fixed width and height.

Examples: <a>, <strong>, <em>, <code>, <span>, <img>, <abbr>.

html
<!-- Two blocks (paragraphs) containing inline elements (strong, a) -->
<p>Today's special is <strong>pumpkin soup</strong>.</p>
<p>Read the <a href="menu.html">full menu</a> for more.</p>

Each <p> takes its own line. Inside them, the <strong> and <a> flow along with the words.

The nesting rule#

A block can sit inside another block, and inline elements can sit inside blocks. But blocks should not be placed inside inline text elements. Do not put a <div> inside a <p> or a <p> inside a <span>. (An <a> is allowed to wrap blocks in modern HTML, but be careful.)

div: the generic block#

<div> has no meaning of its own. It is a plain block container used to group elements so you can style or script them together.

html
<div class="special-card">
  <h3>Cardamom Latte</h3>
  <p>Warm, sweet and a little bit spicy.</p>
  <p>Rs. 300</p>
</div>

The class attribute attaches a label that CSS can target.

span: the generic inline#

<span> is the inline equivalent. Use it to wrap a few words that need a hook for styling or scripting.

html
<p>Today's price: <span class="price">Rs. 300</span> only.</p>

Prefer meaningful elements#

div and span are tools of last resort. First ask: is there an element that already describes this content? A list of items should be <ul>, a navigation area should be <nav>, a highlighted word might be <mark> or <strong>. Reach for div or span only when no semantic element fits, such as a purely visual wrapper.

html
<!-- Weak: meaning is lost -->
<div class="title">Our menu</div>

<!-- Better: a real heading -->
<h2>Our menu</h2>

Overusing div leads to "div soup": pages that are hard to read for people and hard to navigate for assistive technology.

Common mistakes#

  • Using <div> for everything.
  • Putting a block-level element inside <p>. The browser will close the paragraph early.
  • Trying to set width on a <span> and wondering why nothing happens.
  • Using <br> to separate divs, instead of CSS spacing.

Try it yourself#

Create three "special" cards for the cafe. Wrap each in a div with a class, containing a heading, a paragraph and a price inside a span. Then inspect the page in Developer Tools and identify which elements are block and which are inline. Finally, replace one div with a semantic element that fits better and justify your choice in a comment.

Recap#

Block elements start new lines and fill the width; inline elements flow within text. div and span are generic containers with no meaning, so use them only when no semantic element fits.

Check your understanding

Lesson quiz

Optional

A short quiz to check what you just learned.

Questions
4
Time limit
5 min
Pass mark
70%
Best score
-