Aral Code

Page Structure Lesson 24 of 42

Block and Inline Elements — div and span

Understand how block and inline elements flow on a page, and how to use div and span as containers for grouping and styling content.

When you build a page, the browser places every element according to its display type. The two most common types are block and inline. Understanding them explains a lot of "Why is this on a new line?" and "Why won't this go beside that?" moments. In this lesson you'll also meet two of the most used elements in HTML: <div> and <span>.

Block elements

A block element:

  • always starts on a new line,
  • stretches to fill the full width available,
  • can contain other block elements and inline elements.

Examples: <div>, <p>, <h1>–<h6>, <ul>, <ol>, <li>, <table>, <form>, <header>, <section>, <footer>.

Example Block elements, with borders to show their size
<style>
  p, h3, div { border: 2px dashed #0f5c4d; }
</style>
<h3>A heading</h3>
<p>A short paragraph.</p>
<div>A div</div>

Even though "A short paragraph." is short, its border stretches all the way across. That's block behavior.

Inline elements

An inline element:

  • does not start on a new line,
  • takes up only as much width as its content,
  • sits inside a line of text, next to other inline content.

Examples: <span>, <a>, <strong>, <em>, <img>, <code>, <abbr>, <input>, <button>.

Example Inline elements flow with the text
<style>
  a, strong, em, span { border: 2px dashed #ce1126; }
</style>
<p>This sentence has a <a href="#">link</a>, some <strong>strong text</strong>, an <em>emphasized word</em>, and a <span>span</span> — all on the same line.</p>

The div element: a general-purpose box

<div> (division) is a block container with no special meaning. It's used to group elements together so you can style or position them as one unit. Think of it as a plastic container: it doesn't care what you put inside, it just holds things together.

Example Grouping with div to make cards
<div style="background:#e3f1ec; padding:14px; border-radius:10px; margin-bottom:10px;">
  <h3>Coding Club</h3>
  <p>Every Wednesday, 3:00 PM, ICT Lab.</p>
</div>
<div style="background:#fdf3dc; padding:14px; border-radius:10px;">
  <h3>Science Club</h3>
  <p>Every Friday, 3:00 PM, Science Room.</p>
</div>

Each <div> holds a heading and a paragraph, and we styled the whole group with a background and rounded corners. Without the div, you'd have to style the heading and paragraph separately and they wouldn't look like one card.

The span element: a general-purpose inline wrapper

<span> is the inline version of <div>. It also has no meaning of its own. Use it to mark a part of a line so you can style it, when no other element fits.

Example Using span to color part of a sentence
<p>Status: <span style="color: white; background: #1d7a43; padding: 2px 8px; border-radius: 6px;">PAID</span></p>
<p>Status: <span style="color: white; background: #b4372b; padding: 2px 8px; border-radius: 6px;">UNPAID</span></p>
<p>The temperature today is <span style="color: #ce1126; font-weight: bold;">35°C</span>. Stay hydrated!</p>

div and span vs. meaningful elements

Since <div> and <span> have no meaning, use them only when no better element exists:

  • Important text? Use <strong>, not a bold <span>.
  • A paragraph? Use <p>, not a <div> with text.
  • A navigation menu? Use <nav> (you'll learn it in Semantic Elements).
  • Just grouping for style or layout? That's what <div> and <span> are for.

Changing display with CSS

Block and inline are defaults, not fixed rules. CSS can change them with the display property. For example, links in a menu are often turned into blocks so they're easier to tap on a phone:

Example Changing display
<a href="#" style="display: block; background: #0f5c4d; color: white; padding: 12px; margin-bottom: 6px; text-decoration: none;">Home</a>
<a href="#" style="display: block; background: #0f5c4d; color: white; padding: 12px; text-decoration: none;">Lessons</a>
<p>These are <span style="display: inline-block; width: 60px; height: 20px; background: gold;"></span> inline-block boxes <span style="display: inline-block; width: 60px; height: 20px; background: tomato;"></span> in a line.</p>

inline-block is a mix: it sits in the line like inline, but you can give it a width and height like a block.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Which statement describes a block element?
2. Which of these is an inline element?
3. You want to color one word in a paragraph. Which element fits best when no other meaning applies?
4. What is "div soup"?

By Sir Reyel · Last updated 2026-12-04