Skip to content
Tutorials Center
My learning

Topics

Lesson 44 of 122 ยท Page Structure and Layout

Div and Span

Learn what div and span do, how they group content for CSS and JavaScript, and when a semantic element like section, nav or button is the better choice.

Most HTML elements carry meaning: <h2> is a heading, <nav> is navigation, <em> is emphasis. Two elements deliberately carry no meaning at all: <div> and <span>. They are generic containers, empty boxes you use to group content so you can style it or work with it in JavaScript.

That makes them incredibly useful and also easy to overuse. This lesson shows what each one is for, how they differ, and how to decide when a meaningful element would serve your readers better.

<div>: a generic block

<div> (short for division) groups a chunk of content into one block. By default it has no visible effect except starting on a new line, like other block elements (see Block and Inline Elements).

Its power comes from what you attach to it, usually a class that CSS can target:

Grouping with div

The heading and paragraphs keep their meaning; the <div> just draws a box around them so the three elements can be styled and positioned as one card. Remove the class and the card disappears, while the content stays exactly the same.

<span>: a generic inline wrapper

<span> does the same job inside a line of text. It wraps a word or phrase without breaking the line, so you can style it or find it with JavaScript:

Marking up phrases with span

A span adds no meaning. To screen readers, $84 is just text. If the phrase has real meaning, there may be a better element: <strong> for importance, <em> for stress emphasis, <s> for content that's no longer accurate (a good fit for the old price), <time> for dates. See Formatting Text for these.

A span with a purpose: lang

One case where a span does add information is through global attributes. Wrapping a foreign phrase in a span with lang tells screen readers to switch pronunciation and helps translation tools:

HTML
<p>Our cafรฉ's motto is <span lang="it">piano piano</span>: slowly, slowly.</p>

Div vs span at a glance

<div> <span>
Default display Block Inline
Wraps Sections of content: blocks, cards, groups Words and phrases inside text
Can contain Almost anything (flow content) Only phrasing content (text, links, other inline elements)
Meaning None None

Because a span is inline, never put a <div>, heading or paragraph inside a <span>. If you need a box inside a block, use a <div>.

Prefer semantic elements first

HTML has dozens of elements that say what their content is, and browsers, screen readers and search engines use that information. A <div> says nothing. The rule of thumb is:

Use a meaningful element when one fits. Use <div> or <span> only when none does.

Here's a page header written two ways:

HTML
<!-- Div soup: no meaning for assistive technology -->
<div class="header">
  <div class="logo">Lantern Books</div>
  <div class="menu">
    <div><a href="/">Home</a></div>
    <div><a href="/shop">Shop</a></div>
  </div>
</div>

<!-- Semantic: same look with CSS, much more meaning -->
<header>
  <p class="logo">Lantern Books</p>
  <nav>
    <ul>
      <li><a href="/">Home</a></li>
      <li><a href="/shop">Shop</a></li>
    </ul>
  </nav>
</header>

Both can look identical. But in the second version, a screen-reader user can jump straight to the navigation, hears "list, 2 items" and knows where the page header ends. You'll explore these elements in detail in Semantic Elements.

Some useful swaps:

Instead ofโ€ฆ Useโ€ฆ
<div class="nav"> <nav>
<div class="main"> <main>
<div class="article"> <article>
<div class="footer"> <footer>
a <div> that holds a single paragraph of text <p>
<div class="list-item"> repeated <ul> / <ol> with <li>
<span class="bold"> for important text <strong>

Never use a div as a button. A <div onclick="โ€ฆ"> looks clickable, but it can't be reached with the Tab key, doesn't respond to Enter or Space and isn't announced as a button. A real <button> does all of that for free. The same goes for links: if it navigates somewhere, use <a href>.

You don't have to take that on trust. Try it with the keyboard:

A div button versus a real button

The mouse can click both. The keyboard can only reach the real button: focus skips the <div> completely, so keyboard and switch users can never activate it. You could patch the div with tabindex, a role and key handlers, but that is several lines of extra work to rebuild, imperfectly, what <button> gives you in one word. See Keyboard and Focus for more.

When div and span are the right choice

Generic containers are not bad; they just have a specific job. Reach for them when:

  • You need a styling hook with no meaning. A wrapper that centers the page content, a box that creates a grid of cards, the scrolling container around a wide table.
  • You need a target for JavaScript. A <span> that shows a live counter, a <div> where search results are inserted.
  • You group items inside a description list. The spec specifically allows a <div> around each <dt>/<dd> pair in a <dl> (see Description Lists).
  • You need to set an attribute on part of the text that has no better element, like lang or dir.

A well-built page usually has both: semantic elements for the structure and meaning, and a sensible number of <div> and <span> elements for layout and styling.

Semantic structure plus a layout div

The .wrapper and .columns divs exist only for layout: one centers the page, one puts the articles side by side. Everything with meaning uses a meaningful element.

Spotting div soup

"Div soup" is the nickname for pages built almost entirely from nested <div> elements, often five or six deep around a single piece of text. It usually happens gradually: each new layout tweak adds one more wrapper. A few habits keep your markup lean:

  • Ask what each wrapper does. If you cannot name the CSS rule or script that needs it, remove it.
  • Use the element you are styling. You rarely need a <div> around a <ul> just to add padding; style the list itself.
  • Check the structure without CSS. Read the HTML on its own, or look at it in the Elements panel of DevTools. If the outline makes sense without any styling, the meaning is in the markup where it belongs.

Modern CSS layout tools such as Flexbox and Grid work on any element, so you seldom need extra wrappers just for layout anymore.

Common mistakes

  • Blocks inside a span. A <span> may only contain inline content. Putting a <div> or <p> inside it produces invalid HTML that browsers handle inconsistently.
  • A div inside a paragraph. As you saw in Elements and Tags, a <p> closes itself when it meets a <div>, splitting your content in two. Use a <span> inside text, or make the outer element a <div>.
  • Class names that describe looks. class="red-text" becomes a lie the day the design turns blue. Name classes after what the content is, such as class="price" or class="warning". Classes covers naming in detail.
  • Adding roles instead of choosing elements. <div role="navigation"> works, but <nav> is shorter and needs no ARIA. The first rule of ARIA is to use the native element when one exists.
  • Spans for headings. A large, bold <span> is not a heading. Use <h2>, <h3> and so on, and style them as you like.

A quick test: imagine your page with all CSS turned off and read it top to bottom. Headings, lists, paragraphs, links and buttons should still be obvious. Anything that only made sense because of its styling probably needs a more meaningful element.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What meaning do <div> and <span> add to their content?

    Both are deliberately meaningless. They exist to group content for CSS, JavaScript or attributes like lang.
  2. 2. Which is the main difference between <div> and <span>?

    A div groups blocks of content and starts on a new line. A span wraps a phrase inside a line of text.
  3. 3. You need a clickable control that opens a menu. What should you use?

    A real <button> is focusable, works with the keyboard and is announced as a button. A div or span gives you none of that.
  4. 4. Which of these is a good use of a <div>?

    Pure layout wrappers are what divs are for. The menu belongs in <nav>, a paragraph in <p> and the footer in <footer>.

Summary

  • <div> is a generic block container; <span> is a generic inline wrapper.
  • Neither adds meaning; they are hooks for CSS, JavaScript and attributes like lang.
  • A span holds only phrasing content, so never put blocks inside it.
  • Prefer semantic elements (<nav>, <main>, <article>, <button>, <strong>, โ€ฆ) whenever one fits.
  • Use div and span for layout wrappers, script targets and parts of text that have no better element.

Next lesson: Classes.