HTML Academy · Lesson

The span and div Elements

Use generic inline and block containers for styling hooks.

Lesson 4 of 413 steps

The span and div Elements is a free HTML Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the HTML Academy learning path, and your progress syncs across the web and the CoddyKit app. The HTML Academy course includes 4 lessons in total.

Generic Containers

HTML has two generic, semantically neutral container elements:

  • <div> — a block-level container
  • <span> — an inline container

They carry no meaning by themselves — they exist purely as styling and scripting hooks.

The div Element

<div> is a block-level container — it creates a new line before and after:

<div class="card">
  <h2>Card Title</h2>
  <p>Card content here.</p>
</div>

<div class="sidebar">
  <p>Sidebar content.</p>
</div>
<!-- div groups related elements for CSS layout or JavaScript selection -->

The span Element

<span> is an inline container — it flows within text:

<p>The price is <span class="price">$29.99</span> per month.</p>

<p>Error: <span class="error-text" role="alert">Invalid email address</span></p>

<!-- span targets a part of text for styling or scripting
     without adding block-level line breaks -->

When to Use div vs Semantic Elements

Before reaching for <div>, check if a semantic element fits:

  • Page header → <header>
  • Navigation → <nav>
  • Main content → <main>
  • Blog post → <article>
  • Sidebar → <aside>
  • Page footer → <footer>

Use <div> only when no semantic element applies.

Divitis

Divitis — overusing <div> for everything:

<!-- Divitis: meaningless nesting -->
<div class="header">
  <div class="nav">
    <div class="nav-item"><a href="/">Home</a></div>
  </div>
</div>

<!-- Better: semantic HTML -->
<header>
  <nav>
    <a href="/">Home</a>
  </nav>
</header>

CSS Hooks with class and id

Both <div> and <span> are used with class or id attributes for CSS targeting:

<div class="hero-section">
  <h1>Welcome</h1>
  <p>Text with a <span class="highlight">highlighted phrase</span> inside.</p>
</div>

<!-- CSS: -->
<!--
.hero-section { background: #f0f4f8; padding: 2rem; }
.highlight { background-color: yellow; font-weight: bold; }
-->

JavaScript Targeting

<div> and <span> are also JavaScript targets:

<span id="counter">0</span>

<script>
  const counter = document.getElementById('counter');
  let count = 0;
  setInterval(() => {
    count++;
    counter.textContent = count;
  }, 1000);
</script>

Nesting Rules

Nesting rules for generic containers:

  • <div> can contain <div> and <span>
  • <span> can only contain <span> and inline content
  • Do not put <div> inside <span>
<span>
  Inline text
  <span>Nested span OK</span>
  <!-- <div> inside span: INVALID -->
</span>

div for Layout

Common layout pattern using divs:

<div class="layout">
  <div class="sidebar"><!-- sidebar --></div>
  <div class="content"><!-- main content --></div>
</div>

<!-- CSS Grid layout: -->
<!--
.layout { display: grid; grid-template-columns: 250px 1fr; gap: 1rem; }
-->

span for Inline Styling

Common inline styling patterns:

<p>Status: <span class="badge badge-success">Active</span></p>

<p>Price: <span style="color: red; text-decoration: line-through;">$50</span>
        <span style="color: green; font-weight: bold;">$30</span></p>

div and span Are Invisible

By default, <div> and <span> are invisible wrappers:

  • <div> adds no visual styling beyond block display
  • <span> adds absolutely no styling
  • All appearance comes from CSS you apply
  • This makes them perfect neutral containers

Quick Check

Which element should you use to style a single word within a paragraph?

Recap: span and div

Generic container essentials:

  • <div> — block-level, groups elements for layout
  • <span> — inline, targets text portions for styling
  • Both are semantically neutral — they add no meaning
  • Prefer semantic HTML5 elements when they apply
  • Use class and id to connect them to CSS and JavaScript
Free to start

Learn HTML with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

Frequently Asked Questions

Is the “The span and div Elements” lesson free?

Yes — any 3 lessons of the HTML Academy learning path, including “The span and div Elements”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The HTML Academy course includes 4 lessons in total.

What will I learn in “The span and div Elements”?

Use generic inline and block containers for styling hooks. You practise HTML Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start HTML Academy?

No prior experience is required. HTML Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “The span and div Elements” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this HTML Academy lesson?

Yes. Every HTML Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Headings h1-h6 and Their Hierarchy
  2. Paragraphs em strong and b i
  3. br hr blockquote and pre
  4. The span and div Elements
← Back to HTML Academy