Sub topic 2
When div and span are the right choice
Prerequisites
This chapter assumes you can read and write basic HTML (tags, attributes, nesting) and open your browser's developer tools. A little CSS helps, because most uses of div and span exist to give CSS or JavaScript something to attach to.
What generic containers are
div and span are the two elements in HTML that carry no meaning of their own. They exist so that you can group content when no other element describes it.
divis a block-level container. By default the browser starts it on a new line and stretches it across the available width.spanis an inline container. It sits inside a line of text and only takes up the space its content needs.
Both elements have no semantics. They have no role in the accessibility tree, and a screen reader does not announce them. They do not tell search engines or other developers anything about the content. The browser treats them as plain boxes.
That is a feature, not a flaw. HTML needs an escape hatch for the cases where no meaningful element fits. The mistake is not using div and span. The mistake is using them when a meaningful element already exists.
The rule of thumb
Ask these questions in order:
- Does an element exist that describes this content (a
nav, abutton, aul, afigure)? If so, use it. - If not, do you need a wrapper only for styling, layout, scripting, or language and direction attributes? If so, use
divfor block content orspanfor inline content.
Reach for the generic element last, not first.
Legitimate uses of div
Layout wrappers
CSS Grid and Flexbox often need an extra box to arrange items. A wrapper that exists only to position children has no meaning, so div is the right choice.
<main>
<h1>Latest articles</h1>
<div class="card-grid">
<article>
<h2>First article</h2>
<p>Summary of the first article.</p>
</article>
<article>
<h2>Second article</h2>
<p>Summary of the second article.</p>
</article>
</div>
</main>The article elements carry meaning. The div only exists so that CSS can lay the cards out in a grid. If you removed the CSS, the div would add nothing to the document's structure.
Styling hooks and visual grouping
Sometimes you need a box for a background, a border, a shadow, or a width limit. Again, the box is presentational.
<div class="page-width">
<p>Content limited to a readable line length.</p>
</div>Script mount points
JavaScript frameworks often render into an empty container. A div with an id is the usual choice.
<div id="app"></div>Grouping that has no better name
Occasionally you group elements because they share a lang or dir attribute, a data attribute, or a class, and no element describes the group. A div is correct here.
<div lang="fr">
<p>Bonjour tout le monde.</p>
<p>Ceci est un paragraphe en français.</p>
</div>Legitimate uses of span
Styling part of a sentence
When you need to style a few words and the words have no special meaning, use span.
<p>Your balance is <span class="amount-positive">$42.00</span>.</p>If the words do have meaning, pick the matching element instead. Use strong for importance, em for stress, code for code, abbr for abbreviations, and time for dates. A later chapter covers these.
Marking a language change
Use lang on a span to tell screen readers to switch pronunciation for a phrase.
<p>The menu offers <span lang="it">tagliatelle al ragù</span> and a salad.</p>Attaching a hook for scripts
A span can carry a data-* attribute or an id so JavaScript can find and update a value.
<p>You have <span id="cart-count">0</span> items in your cart.</p>If the updated text should be announced to assistive technology, you will need more than a span. You will need a live region, which is covered in the chapter on ARIA.
Misuses to avoid
The problem called "div soup" happens when generic containers stand in for elements that exist. The table below shows common cases.
| Instead of this | Use this | What you gain |
|---|---|---|
| `<div class="header">` | `<header>` | A banner landmark when it is a direct child of `body`. |
| `<div class="nav">` | `<nav>` | A navigation landmark that screen reader users can jump to. |
| `<div class="title">` | `<h1>` to `<h6>` | A place in the heading hierarchy. |
| `<div class="btn" onclick="...">` | `<button type="button">` | Focus, Enter and Space activation, and the button role. |
| `<span class="link" onclick="...">` | `<a href="...">` | Focus, a role, right-click options, and open-in-new-tab. |
| `<div class="item">` repeated inside `<div class="list">` | `<ul>` and `<li>` | An announced list and item count. |
| `<span class="bold">` | `<strong>` or `<b>` | Meaning (importance), or at least a deliberate choice. |
A div with a class name such as header does not make it a header. Class names are for you and your CSS. Browsers and assistive technology ignore them when working out meaning.
A worked before and after
Here is a navigation built only from generic containers.
<div class="nav">
<div class="nav-item"><span class="link" onclick="go('/articles')">Articles</span></div>
<div class="nav-item"><span class="link" onclick="go('/about')">About</span></div>
</div>This markup has four problems. There is no navigation landmark. There is no list. The "links" cannot be focused with the keyboard. They cannot be opened in a new tab, because they are not links.
Here is the fixed version.
<nav aria-label="Primary">
<ul>
<li><a href="/articles">Articles</a></li>
<li><a href="/about">About</a></li>
</ul>
</nav>Nothing is lost. CSS can make this look identical to the first version, so the visual design stays the same.
Edge cases and gotchas
Empty wrappers for spacing
Do not add empty div or span elements to create space. Use CSS margin, padding, or gap. Empty elements clutter the DOM and can confuse some assistive technology.
Over-nesting
Deeply nested div elements make the DOM harder to read and slow down style calculations in extreme cases. Each wrapper should have a reason. If you can remove one and fix the layout with CSS on its parent, do so.
Block content inside span
A span is meant for inline (phrasing) content. Putting a div, p, or a list inside a span is invalid HTML. Switch the span to a div, or change the structure.
<!-- Invalid: block content inside span -->
<span><p>Some text</p></span>
<!-- Valid -->
<div><p>Some text</p></div>Adding a role to a div
You can give a div a role with ARIA, for example role="button". This only changes what assistive technology announces. It does not add keyboard focus or key handling, so you would have to write all of that yourself. A native button does it for free. The ARIA chapter explains when this trade-off is justified.
Changing display with CSS
Setting display: block on a span or display: inline on a div changes how it looks, not what it is. Validity rules still follow the element type, not the CSS. A caution for other elements: in some browsers, setting display on a table, ul, or button can change how assistive technology exposes it, so test after you restyle.
Generic elements in the accessibility tree
Browsers usually leave div and span out of the accessibility tree, or expose them as a generic role. Their children still appear. This means that wrapping a list item or link in a div does not hide it, but it also does not add any meaning. In devtools, you can confirm this in the Accessibility panel.
Checking your own markup
After you write a page, scan for generic containers and ask what each one is for.
- Open devtools and select each
divorspan. - Ask whether it is a layout wrapper, a style hook, a script mount point, or a language or direction marker. If it is, keep it.
- If it stands in for a heading, list, button, link, form control, or landmark, replace it.
- Review class names such as
header,footer,button,list, ortitle. These often point to a missing element.
Summary
divandspanhave no meaning, no role, and no keyboard behavior.- Use them for layout, styling hooks, script mount points, and language or direction markers when no meaningful element fits.
- Do not use them as a replacement for landmarks, headings, lists, buttons, links, or text-level elements.
- Choose the meaningful element first and the generic one last.
- Put block content in
divand inline content inspan.
Exercise
A teammate writes <div class="card-list"> around three <article> elements so that CSS Grid can arrange them in columns. A reviewer asks to replace the div with a semantic element. Is the div correct? Explain, and then fix this line from the same page: <span class="link" onclick="go('/pricing')">Pricing</span>.
Show answer
The div is correct. It exists only for layout, and no element describes "a grid wrapper", so a generic container is the right choice. The article elements already carry the meaning. The span is wrong because it has no role, cannot be focused with the keyboard, and cannot be opened in a new tab. Replace it with <a href="/pricing">Pricing</a>, and use CSS for appearance. Move the navigation logic to the href rather than the onclick.
Exercise
This markup is invalid. Explain why and give two ways to fix it: <span><p>Order shipped.</p></span>.
Show answer
A span accepts only phrasing (inline) content, and p is flow content, so a p cannot be placed inside it. Fix 1: change the wrapper to a div, giving <div><p>Order shipped.</p></div>. Fix 2: remove the wrapper if it is not needed and keep <p>Order shipped.</p>, putting any class on the p itself.