Ad – 728Γ—90
🌐 Core Elements

HTML div and span – Generic Container Elements

<div> and <span> are the two most common HTML elements β€” yet they carry no semantic meaning on their own. They are pure containers used to group content so you can target it with CSS or JavaScript. Understanding when to use them (and when not to) is a core HTML skill.

⏱️ 10 min read🎯 BeginnerπŸ“… Updated 2026

What Is a div?

The <div> element (short for division) is a block-level generic container. It groups one or more elements together so you can apply CSS styles, position them with flexbox or grid, or manipulate them as a unit with JavaScript.

On its own, a <div> is invisible β€” it doesn't add any visual effect or meaning until you style it.

HTML
<!-- div with no class: invisible container -->
<div>
  <h2>Card Title</h2>
  <p>Some descriptive text about this card.</p>
  <a href="/details">Read more</a>
</div>

<!-- div with class: targeted by CSS -->
<div class="card">
  <h2>Card Title</h2>
  <p>Some descriptive text about this card.</p>
  <a href="/details" class="btn">Read more</a>
</div>

What Is a span?

The <span> element is an inline generic container. It wraps a portion of text (or other inline content) so you can style that specific piece without breaking the text flow.

HTML
<!-- Colour a word in a sentence without breaking the line -->
<p>Your score is <span class="highlight">98%</span> β€” excellent!</p>

<!-- Tag a price for JavaScript targeting -->
<p>Total: <span id="price">Β£29.99</span></p>

<!-- Apply multiple styles to an abbreviation -->
<p>Welcome to <span class="brand brand--bold">ylearner</span>.</p>
CSS
.highlight {
  color: #198754;
  font-weight: bold;
}

.brand {
  color: #0d6efd;
}

.brand--bold {
  font-weight: 700;
}

div vs span Comparison

Property <div> <span>
Display typeBlockInline
Starts new line?YesNo
Semantic meaningNoneNone
Common useLayout grouping, cards, sectionsInline text targeting
Can contain block elements?YesNo (inline only)
Width / height settable via CSS?YesNo (use inline-block)
Typical CSS targetFlexbox/grid parent, card wrapperColour, badge, label

Code Example: div for Card Layout

HTML
<div class="card-grid">

  <div class="card">
    <img src="python.png" alt="Python logo" class="card__img">
    <div class="card__body">
      <h2 class="card__title">Python</h2>
      <p class="card__desc">Learn Python from zero to advanced.</p>
      <a href="/python/" class="btn btn--primary">Start Learning</a>
    </div>
  </div>

  <div class="card">
    <img src="js.png" alt="JavaScript logo" class="card__img">
    <div class="card__body">
      <h2 class="card__title">JavaScript</h2>
      <p class="card__desc">Master modern JavaScript.</p>
      <a href="/javascript/" class="btn btn--primary">Start Learning</a>
    </div>
  </div>

</div>
CSS
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 1.5rem;
}

.card {
  border: 1px solid #dee2e6;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}

.card__body {
  padding: 1rem;
}

.card__title {
  margin: 0 0 0.5rem;
  font-size: 1.2rem;
}
Ad – 336Γ—280

class and id on div and span

The most common attributes you'll put on <div> and <span> are class and id:

  • class β€” reusable label for CSS and JavaScript. Multiple elements can share the same class.
  • id β€” unique identifier. Only one element on the page should have a given id.
HTML
<!-- class: shared across multiple elements -->
<div class="alert alert--success">Changes saved!</div>
<div class="alert alert--error">Something went wrong.</div>

<!-- id: unique on the page -->
<div id="main-navigation">…</div>
<div id="hero-banner">…</div>

<!-- JS targeting -->
<script>
  document.getElementById('main-navigation').classList.add('active');
  document.querySelectorAll('.alert').forEach(el => el.style.display = 'none');
</script>

Nesting divs

Divs nest naturally and this is normal for layout. A three-column page might look like:

HTML
<div class="page-wrapper">          <!-- centres content, max-width -->
  <div class="layout">             <!-- flex or grid row -->
    <div class="sidebar">…</div>  <!-- left column -->
    <div class="content">…</div>  <!-- main column -->
    <div class="aside">…</div>   <!-- right column -->
  </div>
</div>
Keep nesting shallow when possible. Deep nesting makes CSS harder to maintain. If you have more than 4–5 levels of nested divs for a single component, consider refactoring into a component or using a simpler layout approach.

When to Use Semantic Elements Instead

HTML5 introduced semantic elements that carry meaning β€” they tell browsers, search engines, and screen readers what the content is, not just how it's grouped. Prefer semantic elements over divs whenever the content fits:

Instead ofUseWhen
<div id="header"><header>Site or section header
<div id="nav"><nav>Navigation links
<div id="main"><main>Primary page content
<div id="footer"><footer>Site or section footer
<div class="article"><article>Self-contained content (blog post, card)
<div class="section"><section>Thematic grouping with a heading
<div class="sidebar"><aside>Supplementary content

The "div Soup" Anti-Pattern

Div soup refers to HTML where every element is a <div> with no semantic meaning β€” an endless sea of nested divs that conveys nothing to screen readers, search engines, or other developers.

Div soup β€” avoid this
HTML
<div id="wrapper">
  <div id="header">
    <div id="logo"><div>ylearner</div></div>
    <div id="nav">
      <div class="nav-item"><div>Home</div></div>
      <div class="nav-item"><div>Courses</div></div>
    </div>
  </div>
  <div id="main">
    <div class="post">
      <div class="post-title">Lesson One</div>
      <div class="post-body">Content here.</div>
    </div>
  </div>
</div>
Semantic HTML β€” prefer this
HTML
<header class="site-header">
  <a href="/" class="logo">ylearner</a>
  <nav class="site-nav">
    <a href="/">Home</a>
    <a href="/courses">Courses</a>
  </nav>
</header>
<main>
  <article class="post">
    <h1>Lesson One</h1>
    <p>Content here.</p>
  </article>
</main>

πŸ“‹ Summary

  • <div> is a block-level generic container β€” use it to group elements for layout (flexbox, grid) or JavaScript manipulation.
  • <span> is an inline generic container β€” use it to target a portion of text for styling or scripting without breaking text flow.
  • Neither element carries semantic meaning. They are transparent wrappers.
  • Prefer semantic elements (header, nav, main, article, section, footer) whenever the content has a clear role.
  • Avoid "div soup" β€” excessive nested divs with no semantic value hurt accessibility and maintainability.

Frequently Asked Questions

Should I always replace divs with semantic elements?

No β€” there will always be cases where you need a generic container for layout purposes and no semantic element fits. Use <div> for those cases. The rule is: use a semantic element when one exists and is appropriate; fall back to div when no semantic match fits.

Can I put a div inside a span?

No. A <span> is inline and should only contain inline content. A <div> is a block element, so placing it inside a <span> is invalid HTML. The browser will attempt to auto-correct it, but the result is unpredictable. Use a <div> inside another <div> for block-level nesting.

Why does my div have an unwanted gap at the bottom?

If the div contains an <img>, the gap is caused by the image's default inline baseline alignment. Fix it with display: block on the image, or line-height: 0 on the containing div.

What is the difference between div and section?

A <section> represents a thematic grouping of content β€” it should have a heading, and it carries semantic meaning (search engines and screen readers understand it as a distinct topic area). A <div> has no semantic meaning β€” it is a pure styling/scripting hook. Use <section> when the content is a distinct topic; use <div> when you just need a styling wrapper.