DIV/SPAN8 min20 XP

div and span

Learn div and span with live examples, interactive editor, and Shiv AI code review.

div and span — Generic Containers

When no semantic HTML tag fits, use <div> for block-level grouping or <span> for inline text grouping. They have no meaning on their own — they exist purely as CSS and JavaScript hooks.

div — Block Container

Example 1 — div as Layout Container
<!-- div groups block-level elements -->
<div class="card">
  <img src="photo.jpg" alt="Profile"/>
  <h3>Shiva Saini</h3>
  <p>Founder, CIWeb</p>
</div>

<div class="card">
  <img src="photo2.jpg" alt="Profile"/>
  <h3>Team Member</h3>
  <p>Developer</p>
</div>
S
Shiva Saini
Founder, CIWeb
T
Team Member
Developer
Each div.card is a block — it will stack vertically by default. CSS Flexbox or Grid makes them side by side.

span — Inline Container

Example 2 — span for Inline Styling
<!-- span targets a specific word/phrase within text -->
<p>
  This course has 
  <span style="color:red;font-weight:bold">22 lessons</span>
   and a real certificate.
</p>

<p>
  Price: 
  <span class="price">FREE</span>
   — No credit card needed.
</p>

This course has 22 lessons and a real certificate.

Price: FREE — No credit card needed.

span stays inline — it does not break to a new line. Perfect for styling specific words within a paragraph.
div vs span
divBlock-level. Takes full width. Starts on a new line. Use for sections, cards, wrappers.
spanInline. Only as wide as content. Stays in the text flow. Use for highlighting words.
When to use divWhen you need to group multiple elements for layout or styling and no semantic tag fits.
When to use spanWhen you need to style or target a word/phrase inside a block of text.
Prefer Semantic Tags Over div

Before using a div, ask: "Is there a more meaningful tag?" Navigation? Use nav. Blog post? Use article. Page section? Use section. Header area? Use header. div should be the last resort, not the first choice.

Why div Has No Meaning

The div element was created in HTML as a "generic flow container" — a blank box with no semantic implication. It was designed to be styled by CSS. Overusing div (called div soup) makes code hard to read and hurts accessibility.

CIWeb HTML Course • 22 Lessons • Free

Explain with Analogies

Shiv AI explains this lesson using real-life analogies — no code, just clear examples.

Click the Explain tab to generate analogies.
index.html
Live Preview
Write code then click Deep Analyze

What is the key difference between div and span?

Explanationdiv is a block-level element — it starts on a new line and takes full width. span is inline — it stays within the text flow. Use div for layout containers, span for targeting text within content.
Build Challenge
Profile Cards
Build a page with two div cards side by side using class names, and use span to highlight specific words inside paragraphs.
Requirements (5 to pass)
div with class
multiple divs
span used
text content
heading
solution.html
Preview
Community Wall
Lesson 11
0/500
0/20 lines
as
Recent Posts
Lesson 11
AI
Shiv AI
Online