Chapter 15 of 26

Block vs Inline Elements: div & span

Write two paragraphs and they stack one below the other. Write two links and they sit side by side on the same line. Make a word <strong> and it stays in the middle of its sentence, but make it an <h2> and it suddenly jumps onto its own line.

Why do some elements start on a new line while others flow along with the text? The answer is the difference between block and inline elements. Once you understand it, a lot of "why is my layout doing this?" moments disappear.

In this chapter we'll learn how block and inline elements behave, which elements are which, what you're allowed to put inside what, and how the two generic containers – <div> (block) and <span> (inline) – are used.

Block vs Inline in One Picture

Block elements stack vertically, each taking the full width:

┌──────────────────────────────────────────┐
│ <h1> Heading                              │
└──────────────────────────────────────────┘
┌──────────────────────────────────────────┐
│ <p> A paragraph of text ...               │
└──────────────────────────────────────────┘
┌──────────────────────────────────────────┐
│ <div> Another block                       │
└──────────────────────────────────────────┘

Inline elements flow inside a line, like words:

  This is <strong>bold</strong>, this is a <a>link</a>, and <span>this</span> too.

Block-level Elements

A block-level element starts on a new line, stretches to fill the full width available to it, and pushes the next element onto a new line below it.

Key properties:

  • Always starts on a new line.

  • Takes the full available width by default, even if its content is one word.

  • Respects width, height, margin and padding in all four directions (these are CSS properties).

  • Can contain other blocks and inline content (subject to each element's rules, as we'll see).

Common block-level elements:

<div>  <p>  <h1>–<h6>  <ul>  <ol>  <li>  <table>  <form>  <fieldset>
<header>  <footer>  <main>  <nav>  <section>  <article>  <aside>
<blockquote>  <pre>  <hr>  <figure>  <address>  <dl>

(Strictly, <li> defaults to display: list-item and <table> to display: table, but both still start on a new line and stack like blocks, so they are counted as block-level here.)


Inline Elements

An inline element does not start on a new line. It takes only as much width as its content needs and flows with the surrounding text, wrapping across lines like words.

Key properties:

  • Sits within the current line.

  • Width is decided by its content.

  • CSS width and height are ignored.

  • Horizontal margin and padding work. Vertical padding is drawn (its background shows) but does not push lines above and below apart, and vertical margins have no effect.

  • Meant to contain text and other inline elements.

Common inline elements:

<span>  <a>  <strong>  <em>  <b>  <i>  <code>  <mark>  <small>  <abbr>
<sub>  <sup>  <time>  <br>  <label>  <q>  <cite>
<img>  <input>  <button>  <select>  <textarea>   (special: see below)

A special group: images and form controls

<img>, <input>, <button>, <select> and <textarea> sit in a line like inline elements – that's why the label and text box in the Forms chapter appeared side by side. But unlike normal inline elements, they do accept width and height. Images and similar elements are called replaced elements, because their content comes from outside the HTML (an image file, a native control). Form controls behave in CSS as inline-block, a mix of both worlds that we'll meet shortly.


Seeing the Difference

Let's prove these rules in a browser. We'll add a coloured border with a little CSS so the boxes become visible:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Block vs Inline</title>
  <style>
    div  { border: 2px solid steelblue; background: #e8f1fb; }
    span { border: 2px solid crimson;  background: #fde8e8; }
    .sized { width: 200px; height: 60px; }
  </style>
</head>
<body>
  <div>Div 1</div>
  <div>Div 2</div>
  <p>
    Here is <span>span 1</span> and <span>span 2</span> inside a paragraph.
  </p>
  <div class="sized">Div with width 200px, height 60px</div>
  <p>
    A <span class="sized">span with the same class</span> ignores the size.
  </p>
</body>
</html>

What you see in the browser:

  1. Div 1 and Div 2 appear as two blue boxes stacked on top of each other, each stretching across the entire width of the window, even though their text is short.

  2. span 1 and span 2 appear as small red boxes that hug their text and sit in the middle of the sentence, on the same line.

  3. The sized div becomes a 200 × 60 pixel blue box, and its text wraps onto two lines to fit.

  4. The sized span looks exactly like the other spans – a thin red outline around its text. width and height had no effect.


A Modern Note: Block/Inline is Really About CSS

In older HTML (HTML 4), elements were officially divided into "block-level" and "inline" in the specification itself. Modern HTML has replaced this with more detailed content categories, the two most important being:

Category

Roughly means

Examples

Flow content

Almost anything that can go in <body>

<div>, <p>, <ul>, <table>, plus all phrasing content

Phrasing content

Text and the elements that mark up text within a paragraph

<span>, <a>, <strong>, <em>, <img>, <input>

Whether an element is displayed as a block or inline is now decided by the CSS display property. Browsers have a built-in default stylesheet that sets display: block for <div>, <p>, <h1> and others, while most phrasing elements default to display: inline. You can change it:

/* make links stack like blocks (e.g. a vertical menu) */
nav a { display: block; }

/* make list items sit in a row */
.menu li { display: inline; }

This separation is important: choose the HTML element for its meaning (as we learnt in Semantic HTML), then use CSS to choose how it is displayed. Never use an <h3> just because you want a new line, or a <span> just because you want something on the same line.

The everyday words "block element" and "inline element" are still used everywhere (including interviews), meaning "an element whose default display is block/inline".


The Middle Ground: inline-block

Sometimes you want elements side by side (like inline) and with a fixed width and height (like block) – think of a row of buttons or cards. That's display: inline-block:

Behaviour

block

inline

inline-block

Starts on a new line

Yes

No

No

Default width

Full available width

Fits content

Fits content

width / height apply

Yes

No

Yes

Vertical margin / padding affect layout

Yes

No

Yes

Default for

div, p, h1, ul ...

span, a, strong ...

button, input, select (in practice)

(For real page layouts, modern CSS uses Flexbox and Grid – topics for the CSS tutorial.)


The <div> Element

<div> ("division") is a generic block-level container with no meaning of its own. It exists to group content for styling with CSS or for scripting with JavaScript.

<div class="card">
  <h2>Data Structures</h2>
  <p>Arrays, linked lists, stacks, queues and trees.</p>
  <a href="/dsa">Start learning</a>
</div>

Here the <div> groups a heading, paragraph and link so that CSS can give the whole group a border, padding and shadow – a "card". The div adds no meaning; screen readers ignore it.

Typical uses of <div>:

  • Wrappers for layout: centring a page, Flexbox or Grid containers.

  • Grouping elements to style them together (cards, rows, columns).

  • Hooks for JavaScript (an element to fill with content later).

  • Grouping each name–value pair inside a description list (<dl>).


The <span> Element

<span> is a generic inline container with no meaning of its own. It wraps a piece of text (or other inline content) so that it can be styled or targeted by script.

<p>Your order status: <span class="status-shipped">Shipped</span></p>
<p>Price: ₹<span id="price">499</span></p>

The first span lets CSS colour just the word "Shipped" green; the second gives JavaScript a hook to update the price without touching the rest of the sentence.

div vs span

Point

<div>

<span>

Default display

Block

Inline

Starts on new line

Yes

No

Content allowed

Flow content (blocks and inline)

Phrasing content only (text, inline elements)

Typical use

Group sections/blocks for layout

Style or target part of a line of text

Semantic meaning

None

None

When not to use div and span

Both are the "last resort" elements. Before writing one, ask: is there an element that says what this is?

Instead of

Use

<div class="header">

<header>

<div class="nav">

<nav>

<div class="title">

<h2> (correct level)

<div onclick="...">Submit</div>

<button>

<span class="important">

<strong>

<span class="highlight">

<mark>

<div> with line breaks for a list

<ul>/<ol> with <li>

The fake button deserves a special warning: a clickable <div> cannot be reached with the Tab key and doesn't respond to Enter or Space unless you write extra code and ARIA. A real <button> does all of that for free.


Nesting Rules: What Can Go Inside What?

Each element has a content model – the list of what it may contain. The ones that matter most for block vs inline:

Rule 1: Inline (phrasing) elements should contain only phrasing content

<!-- ✔ correct -->
<p>Read the <span><strong>terms</strong></span> first.</p>

<!-- ✘ wrong: a block inside an inline element -->
<span><div>Hello</div></span>

Browsers will still display the wrong version, but the HTML is invalid, and the result is unpredictable once CSS is involved.

Rule 2: A <p> cannot contain block elements

This one causes real, visible bugs. <p> may only contain phrasing content. Look what happens when we break the rule:

<p>Intro <div>Box</div> end</p>

The HTML parser closes the paragraph automatically the moment it sees <div>. When we load this in Chrome and inspect the resulting DOM, we get:

<p>Intro </p><div>Box</div> end<p></p>

Step by step:

  1. <p> opens and "Intro " goes inside it.

  2. <div> arrives. A div isn't allowed in a paragraph, so the parser closes the <p> first, then opens the div.

  3. " end" is now outside any paragraph.

  4. The final </p> has no open <p> to close, so the parser creates an empty <p></p> – an extra blank paragraph with its own margins.

So any CSS written for "the paragraph" won't apply to the div or to " end". The same auto-closing happens with <ul>, <table>, headings, <section> and other blocks inside <p>. If you need a block of mixed content, use a <div> as the outer element instead.

Rule 3: <a> may wrap blocks

An exception worth knowing: <a> has a transparent content model – it may contain whatever its parent may contain (except other interactive elements like links or buttons). So this is valid HTML and makes a whole card clickable:

<a href="/dsa/arrays">
  <div>
    <h3>Arrays</h3>
    <p>Learn indexing, traversal and common problems.</p>
  </div>
</a>

This works when the <a> itself is somewhere a <div> would be allowed (for example directly in <body> or another <div>), but not inside a <p> or a <span>.

Summary of the rules

Parent

Can contain

<div>, <section>, <article>, <li>, <td> ...

Blocks and inline (flow content)

<p>, <h1>–<h6>, <span>, <strong>, <em>, <label>, <button>

Only text and inline (phrasing content)

<a>

Whatever its parent allows, but no interactive elements

<ul>, <ol>

Only <li> (and script-supporting elements)


A Practical Example

A student profile card using <div> for layout grouping, <span> for styling parts of text, and semantic elements wherever there's real meaning:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Student Card</title>
  <style>
    .card  { border: 1px solid #ccc; padding: 12px; width: 300px; }
    .badge { background: gold; padding: 2px 6px; }
    .muted { color: gray; }
  </style>
</head>
<body>
  <article class="card">
    <h2>Priya Sharma <span class="badge">Topper</span></h2>
    <p>Branch: CSE <span class="muted">(2022–2026)</span></p>
    <div>
      <a href="/profile/priya">View profile</a>
      <a href="/message/priya">Message</a>
    </div>
  </article>
</body>
</html>

In the browser this shows a thin grey bordered card about 300 pixels wide (plus its padding). The heading "Priya Sharma" has a gold-highlighted "Topper" badge right after the name on the same line (a span inside the heading). "Branch: CSE" is followed by a grey "(2022–2026)". The two links sit side by side on one line, because links are inline – the <div> simply groups them into their own row below the paragraph.

Notice the card itself is an <article> (a self-contained profile), not a <div>, while the inner row of links is a <div> because it's just a layout group.


Common Mistakes

  • Putting a <div>, list or table inside a <p>. The parser splits the paragraph and adds an empty one.

  • Putting block elements inside <span> (or <strong>, <em> ...). Invalid HTML.

  • Setting width/height on a <span> or <a> and wondering why nothing happens. Change its display to inline-block or block.

  • Choosing elements by default display. Using <h4> to get a line break or <br><br> to make space. Use CSS for spacing.

  • "Divitis" – wrapping everything in divs when semantic elements exist.

  • Thinking inline elements can't have padding. They can; it just doesn't push surrounding lines apart vertically.


Interview Questions

1. What is the difference between block and inline elements?

Block elements start on a new line, take the full available width, and respect width/height and vertical margins. Inline elements stay within the line, are as wide as their content, ignore width/height, and their vertical margins/padding don't affect line layout.

2. Difference between <div> and <span>?

Both are meaning-free containers. <div> is block-level and can hold flow content (used for layout groups); <span> is inline and holds only phrasing content (used for part of a line of text).

3. Is <img> block or inline?

It displays inline (sits in a line of text) but, being a replaced element, it accepts width and height – which is why it often seems to behave like inline-block.

4. What happens to <p><div>x</div></p>?

The parser closes the <p> when it reaches <div>, so the div becomes a sibling of the paragraph. The stray </p> then creates an extra empty <p></p>. The resulting DOM is <p></p><div>x</div><p></p>.

5. Can an <a> contain a <div>?

Yes, in modern HTML, because <a> has a transparent content model – as long as the <a> itself is in a place where a <div> is allowed and it contains no other interactive elements.

6. What is inline-block?

A CSS display value where the element flows inline with other content (no new line) but behaves like a block inside – width, height and vertical margins/padding all apply.


Conclusion

Block elements build the vertical structure of the page; inline elements live inside lines of text.

Block   → new line, full width, width/height work      (div, p, h1, ul, section)
Inline  → same line, content width, width/height ignored (span, a, strong, em)
Inline-block → same line, but width/height work          (button, input in practice)

<div>  = generic block container   (layout, grouping)
<span> = generic inline container  (style part of text)
  • Modern HTML talks about flow and phrasing content; "block" and "inline" are the CSS display defaults.

  • Pick elements for meaning; change their display with CSS.

  • Never put blocks inside <p> or inline elements – except <a>, which is transparent.

  • Use <div> and <span> only when no semantic element fits.

Next, in Audio & Video, we'll bring media into our pages with the <audio> and <video> elements.