Lesson 29 of 51 · Module 5, lesson 2 of 7 · 3 min

Generic containers: div and span

Group and mark content with div and span — and know when a meaningful element is better.

What you'll learn

  • What <div> and <span> are for
  • Why they are the last choice, not the first
  • How they prepare your page for CSS and JavaScript

Why this matters

Every element so far has a meaning: a heading, a list, a quotation. Sometimes you need to group or mark content only so that you can style it or change it later — with no special meaning. For that, HTML has two containers with no meaning at all.

<div>: a block container

<div> (short for division) groups other elements. It is a block element.

<span>: an inline container

<span> marks a piece of text inside a line. It is an inline element.

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Recipe card</title>
</head>
<body>
  <div>
    <h2>Tomato soup</h2>
    <p>Cooking time: <span>30 minutes</span></p>
  </div>
  <div>
    <h2>Green salad</h2>
    <p>Cooking time: <span>10 minutes</span></p>
  </div>
</body>
</html>

What you'll see: two headings and two paragraphs — exactly as if the <div> and <span> were not there. They change nothing on their own.

Two small recipe blocks: the heading "Tomato soup" with "Cooking time: 30 minutes", and the heading "Green salad" with "Cooking time: 10 minutes". The div and span elements add no visible change.

So why use them? Because later, with CSS, you can give each recipe <div> a border and a background, like a card — and with JavaScript you can change the text in a <span>. You will learn how to name them for that in the next two lessons.

The last choice, not the first

Before you reach for <div>, ask: is there an element that says what this is?

If the content is…UseNot
A paragraph<p><div>
A list<ul> or <ol><div> for each item
Important words<strong><span>
The site's main navigation<nav> (later in this module)<div>

Meaningful elements help screen readers, search engines and other developers. A page built only from <div> elements tells them nothing. Use <div> and <span> only when nothing else fits.

Try it yourself

  1. Select the example and choose Try it.
  2. Add a third recipe card with its own <div>.
  3. Look at your code and ask for every <div> and <span>: is there a more meaningful element?

Common mistakes

  • <div> for everything, such as <div> instead of <p> or <ul>. The page loses its meaning. Fix: use the meaningful element; <div> is the fallback.
  • A <div> inside a <p>. A block cannot go inside a paragraph. Fix: use <span> inside text, or close the paragraph first.
  • Expecting <div> to change the look. It does nothing visible alone. Fix: styles come with CSS.

Check yourself

  1. Is <span> block or inline?
  2. You want to group a heading and a paragraph so that you can style them as a card later. Which element fits?
  3. Should you use <div> for a paragraph?

Answers

  1. Inline.
  2. <div>, unless a meaningful element fits better (you will meet <article> and <section> soon).
  3. No. Use <p>.

Next: to style one group with CSS, you need to give it a name. The next lesson shows the class attribute.

Chat with us on WhatsApp