On this page10 sections
  1. Learning Objectives
  2. 1. Block Elements
  3. 2. Inline Elements
  4. 3. <div>: a Box for Grouping
  5. 4. <span>: a Wrapper Inside Text
  6. 5. Classes Make Groups Reusable
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 016: Block & Inline: div and span

Category4: Page Structure
Lecture016 of 048
Estimated video length10 min
TargetHTML5 · any modern browser
LevelBeginner

Some elements start on a new line and take the full width; others sit inside a line of text. Knowing which is which explains most "why is this on its own line?" questions. Two plain elements, <div> and <span>, let you group anything for styling.


Learning Objectives

  • Tell block elements from inline elements
  • Group content with <div> (block) and <span> (inline)
  • Use class on these elements so CSS can style them

1. Block Elements

A block element starts on a new line and stretches across the full width available.

Common block elements: <h1>–<h6>, <p>, <ul>, <ol>, <li>, <table>, <div>, <form>.

Example: the background colour shows how wide each block is:

html
<p style="background: #ffe8cc;">I am a paragraph, so I fill the whole width.</p>
<h3 style="background: #d3f9d8;">So does this heading.</h3>

2. Inline Elements

An inline element sits inside a line of text and is only as wide as its content.

Common inline elements: <a>, <strong>, <em>, <img>, <code>, <span>.

Example

html
<p>
  This has a <a href="#">link</a>, some <strong>bold text</strong>
  and <span style="background: #ffe066;">a highlighted span</span>, all on one line.
</p>

Rule of thumb: inline elements go inside block elements, not the other way round.


3. <div>: a Box for Grouping

<div> has no meaning of its own. It's a plain block box that groups other elements, so you can style or move them together.

Example

html
<div style="border: 2px solid #4c6ef5; padding: 12px;">
  <h3>Ali Raza</h3>
  <p>Flutter developer, Lahore</p>
  <a href="mailto:ali@example.com">Email Ali</a>
</div>

4. <span>: a Wrapper Inside Text

<span> is the inline version: it wraps a few words so you can style them.

Example

html
<p>Your order is <span style="color: green; font-weight: bold;">confirmed</span>.</p>

5. Classes Make Groups Reusable

Inline style gets messy fast. Give elements a class, and in the CSS course one rule will style them all.

Example: the same idea using a <style> block (a preview of CSS):

html
<style>
  .card { border: 1px solid #ccc; border-radius: 8px; padding: 12px; margin-bottom: 10px; }
  .price { color: crimson; font-weight: bold; }
</style>

<div class="card">
  <h3>Wireless mouse</h3>
  <p>Only <span class="price">Rs 1,800</span></p>
</div>
<div class="card">
  <h3>USB keyboard</h3>
  <p>Only <span class="price">Rs 2,500</span></p>
</div>

Common Beginner Mistakes

1. "Div soup". Wrapping everything in <div>s when a meaningful element exists. A list should be <ul>, a heading <h2>. Lecture 023 adds more meaningful boxes.

2. Block inside inline. <span><p>…</p></span> is invalid. Put spans inside paragraphs.

3. Expecting <span> to start a new line. It's inline. Use a <div> or <p> for a new line.


Practice Questions

  1. Does a <p> start on a new line?
  2. Name three inline elements.
  3. Which element groups several elements into one block?
  4. Which element would you use to colour just one word in a sentence?
  5. Is <strong><div>Hi</div></strong> a good idea? Why?
  6. Wrap a heading and a paragraph in a div with the class notice.

Practice Answers

  1. Yes. It's a block element.
  2. Any three of <a>, <strong>, <em>, <img>, <code>, <span>.
  3. <div>
  4. <span>
  5. No. A block element (<div>) shouldn't go inside an inline element (<strong>).
  6. html
    <div class="notice">
      <h3>Holiday</h3>
      <p>The office is closed on Friday.</p>
    </div>

Key Takeaways

  • Block elements start a new line and fill the width; inline elements flow inside text.
  • <div> = a plain block box; <span> = a plain inline wrapper.
  • Neither has meaning; use them for grouping and styling, with a class.
  • Prefer meaningful elements whenever one fits.