Sub topic 2
When div and span are the right choice
This chapter assumes you know basic HTML syntax (tags, attributes, nesting) and can open your browser's developer tools. CSS knowledge helps but is not required.
What div and span actually mean
div and span are generic containers. They carry no meaning of their own. A div is a block-level box, and a span is an inline box. Neither one adds a role to the accessibility tree, so assistive technology treats them as plain grouping.
That makes them the last resort, not a mistake. Use them when no element describes your content, and you need a hook for styling or scripting.
| Element | Display by default | Typical use |
|---|---|---|
| `div` | block | Wrapping a group of elements for layout or scripting |
| `span` | inline | Wrapping a piece of text for styling or scripting |
Legitimate uses
Reach for a generic container when the reason is purely presentational or behavioral:
- A layout wrapper, such as a grid or flex container around cards.
- A styling hook for a few words, such as a highlighted price.
- A mount point for a JavaScript component.
- A wrapper that groups elements so they can share a class,
lang, ordata-attribute.
<main>
<h1>Pricing</h1>
<div class="card-grid">
<article class="card">
<h2>Basic</h2>
<p>Just <span class="price">$5</span> per month.</p>
</article>
<article class="card">
<h2>Pro</h2>
<p>Just <span class="price">$12</span> per month.</p>
</article>
</div>
</main>Here main, article, h1 and h2 describe the content. The div only exists so CSS can lay the cards out in a grid. The span only styles the price. Removing either would change the look, not the meaning.
<p>
The word <span lang="fr">bonjour</span> is French for hello.
</p>This second example shows a span that does carry information, through the lang attribute. The span itself is still generic, but the attribute gives screen readers a pronunciation cue.
When they are the wrong choice
Ask one question first: does an element already exist for this content? If so, use it.
<!-- Avoid -->
<div class="header"><div class="nav">...</div></div>
<div class="title">Latest articles</div>
<span class="bold">Warning</span>
<!-- Prefer -->
<header><nav>...</nav></header>
<h1>Latest articles</h1>
<strong>Warning</strong>Class names such as header or title are a warning sign. If you name a div after a role, an element probably exists for that role.
Common errors
- "Divitis": nesting many divs where one wrapper, or none, would do. Each extra level makes the markup harder to read and style.
- Fake controls: a
divwith a click handler is not focusable and has no button role. Chapter 11 covers native interactive elements. - Fake headings: styling a
divorspanto look like a heading removes it from the heading outline. - Block inside inline: a
divinside aspanis invalid. Check what content each element may contain.
Exercise
A developer writes <div class="nav"><div class="link">Home</div><div class="link">About</div></div> for a site menu. Which parts should change, and would a div still be appropriate anywhere?
Show answer
The outer div should be a nav element, and the links should be anchor elements (a), ideally inside a ul with li items. A div would still be fine as a layout wrapper, for example around the nav and a search box, if CSS needs a single container and no semantic element fits.