Generic Containers: div and span

Beginner · 12 min

div and span have no meaning of their own. Learn when they are the right wrapper, what they lack compared with native elements, and what the parser allows.

Before thisArticle, Section and the OutlineElements, Attributes and Nesting

Kevin Powell · 12 min

Generic Containers: div and span

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Kevin PowellReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `div` is a generic block container and `span` a generic inline one. They carry no meaning, so they are for grouping content for styling or scripting when no semantic element fits.
  • Reach for a semantic element first. In a test, a page of `div`s showed up in the accessibility tree as one run of plain text, with no landmarks, lists or buttons.
  • A `div` with a click handler is not a button. In a test, Tab skipped a clickable `div` and a clickable `span` and stopped only on a real `<button>` and a `div` that had `tabindex="0"`.
  • A `div` cannot go inside a `p`, because the parser closes the paragraph first. A `div` inside a `span` is left in place by the parser, but it is invalid markup.
  • Use `class` for the styling hook, not the element. A name such as `.card` describes the role, while `div` only describes the box behaviour.

Elements with no opinion

Most HTML elements say something: nav is navigation, ul is a list, button is a control. But sometimes you only need a box. You want to put a border around three paragraphs, or colour one word, and nothing in the vocabulary describes the content. HTML gives you two elements for that, and they are deliberately empty of meaning.

  • <div> is a generic block container. It starts a new line and takes the full width.
  • <span> is a generic inline container. It sits within a line of text.

In a test with headless Chromium 159 their computed display values were block and inline. That is the whole difference, and CSS can change it either way. Think of them as plain cardboard boxes: they hold things and can be labelled, but the box itself says nothing about what is inside.

Last resort, not first reach

Because they carry no meaning, div and span are what you use when no meaningful element fits. The cost of reaching for them too early is real. In a test, a page built as <div class="header"><div class="nav"><div onclick="x()">Home</div></div></div><div class="main">content</div> appeared in the accessibility tree as a single run of text, Home content. There was no banner, no navigation, no main and nothing marked as clickable. The same structure built from header, nav, a and main has all four. See landmarks for those elements.

The same is true of behaviour. I put four elements on a page, with click handlers on a div and a span, a real <button>, and a div with tabindex="0" and role="button", then pressed Tab. Focus went to the button, then to the tabindex div, then left the page. The clickable div and span were skipped entirely. A keyboard user can’t reach them. A <button> gives you focus, a role and keyboard activation for free. The keyboard and focus lesson and the native semantics lesson go deeper.

A useful habit: before typing <div>, ask what the thing is. A list? A group of links? A figure? A form control? Use that element. If the honest answer is “just a box for layout”, use div.

Using them well

Give a generic container a class that names its job:

<div class="callout">
  <p>Heads up: this deletes the file.</p>
</div>

<p>Total: <span class="price">$12</span></p>

The class describes the role (callout, price), which survives restyling, where the element name only describes how it flows. Which classes to write, and how to name them, is a CSS question. The CSS architecture lesson takes it up, and display and normal flow explains block versus inline.

What the parser allows

Containers have nesting rules. A div is not allowed inside a p: in a test <p>a<div>b</div>c</p> came out as <p>a</p><div>b</div>c<p></p>, because the parser closed the paragraph before the div. A div inside a span is also invalid, but the parser does not repair it: in a test <span><div id="in">in span</div></span> kept the nesting exactly as written. Because invalid markup that happens to render is hard to spot, an HTML validator is worth running. The practical rule is block-level boxes go in block-level boxes, and inline ones go inside text.

You may also see “div soup”, where every wrapper is a div nested six deep. It is usually a sign that CSS layout was bolted on without thinking about structure. Modern layout tools like grid and flexbox reduce the need for extra wrappers.

What the video covers

The embed is Kevin Powell’s “HTML & CSS for Absolute Beginners: Divs and Spans” (January 2025, about 11.5 minutes), from his free beginner course. Its chapters are an introduction to divs (0:00), styling div containers (1:56), using classes for styling (3:29), an introduction to spans (6:55), styling span elements (8:35) and optimising text styling (9:58). It is styling-heavy, which matches how the course uses them. I could not read a transcript, so I describe it only from the chapters. The accessibility-tree, Tab-order and nesting tests are this lesson’s own. The deeper links are MDN’s reference pages for both elements.

Where this fits

This follows article, section and the outline, which covers the meaningful grouping elements, and comes right after the vocabulary in elements, attributes and nesting. Next, figure, blockquote and citations adds elements for specific kinds of content.

Check yourself

  1. 01You need to put a coloured border around three related paragraphs and no semantic element fits. What do you use?

    A `div` with a class, such as `<div class="callout">`. It is a block wrapper with no meaning, so it adds a styling hook without claiming anything false. If the group is a self-contained piece, use `article` or `section` instead.

  2. 02Why is `<div onclick="save()">Save</div>` worse than `<button>`?

    The `div` has no role, is not focusable and does not respond to Enter or Space. In a test Tab skipped it. A `button` gets focus, keyboard activation and its role for free. Adding `tabindex` and `role` repairs only part of that.

  3. 03What is the difference between `div` and `span` in layout terms?

    `div` is `display: block` by default, so it starts a new line and fills the width. `span` is `display: inline`, so it flows within text. In a test the computed values were `block` and `inline`. CSS can change either.

  4. 04When is `span` the right choice?

    When you need to style or script a run of words inside a sentence and no inline semantic element, such as `strong`, `em`, `mark` or `code`, describes it. For example, wrapping a price in `<span class="price">` to colour it.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark