Understanding Display Behavior

Every HTML element has a default display behavior that determines how it appears in the page layout. The two main types are block-level and inline elements.

Why This Matters:

  • Controls how elements stack and flow on the page
  • Affects spacing, width, and positioning
  • Determines which elements can contain others
  • Essential for understanding CSS layout

Key Point: You can change an element's display with CSS (display: block;, display: inline;, etc.), but understanding defaults is crucial.

Block-Level Elements

Block elements are the structural building blocks of a webpage. They create distinct sections and stack vertically.

Characteristics:

  • Always start on a new line - Forces content after it to the next line
  • Take full width available - Stretches to parent container width by default
  • Can contain block or inline elements - Very flexible nesting
  • Respect width/height properties - Can be sized with CSS
  • Accept all margin/padding - Top, right, bottom, left spacing works

Common Block Elements:

<div>      Generic container (most common)
<p>        Paragraph
<h1>-<h6>  Headings
<ul>, <ol> Lists
<li>       List item
<table>    Table
<form>     Form
<header>   Header section
<nav>      Navigation
<main>     Main content
<section>  Section
<article>  Article
<aside>    Sidebar
<footer>   Footer
<blockquote> Long quote

Block Element Example:

<!-- Each element starts on a new line -->
<div style="background: lightblue; padding: 10px;">
  This is a div (block element)
</div>
<p style="background: lightgreen; padding: 10px;">
  This is a paragraph (block element)
</p>
<h2 style="background: lightyellow; padding: 10px;">
  This is a heading (block element)
</h2>

Visual Result:

Each element appears on its own line, taking full width:

┌────────────────────────────────┐
│ This is a div (block element)  │
└────────────────────────────────┘
┌────────────────────────────────┐
│ This is a paragraph            │
└────────────────────────────────┘
┌────────────────────────────────┐
│ This is a heading              │
└────────────────────────────────┘

When to Use Block Elements:

  • Creating page structure and layout sections
  • Grouping related content together
  • Building card components or containers
  • Organizing content vertically

Back to top

Inline Elements

Inline elements flow with text and don't break the line. They're used within block elements to style or add meaning to text.

Characteristics:

  • Don't start on a new line - Flow with surrounding content
  • Take only necessary width - Width matches content size
  • Cannot contain block elements - Only other inline elements
  • Ignore width/height properties - Size determined by content
  • Vertical margin/padding partially ignored - Left/right work, top/bottom don't affect layout

Common Inline Elements:

<span>     Generic inline container
<a>        Link (anchor)
<img>      Image
<strong>   Important text (bold)
<em>       Emphasized text (italic)
<b>        Bold text
<i>        Italic text
<code>     Inline code
<mark>     Highlighted text
<small>    Small text
<sub>      Subscript
<sup>      Superscript
<label>    Form label
<input>    Form input
<button>   Button

Inline Element Example:

<p>
  This is a paragraph with 
  <span style="color: red;">red text</span>, 
  <span style="color: blue;">blue text</span>, and 
  <a href="#">a link</a> all on the same line.
</p>

Visual Result:

All inline elements flow together:

This is a paragraph with red text, blue text, and a link all on the same line.

Width/Height Behavior:

<!-- Width/height ignored on inline elements -->
<span style="width: 200px; height: 100px; background: yellow;">
  This span ignores width/height
</span>

<!-- Only takes content width -->
<span style="background: lightblue; padding: 5px;">
  Fits content
</span>

When to Use Inline Elements:

  • Styling parts of text within paragraphs
  • Adding links within sentences
  • Inserting images within text flow
  • Applying formatting to specific words

Back to top

Inline-Block Elements

CSS allows a hybrid behavior: display: inline-block; combines benefits of both types.

Inline-Block Characteristics:

  • Flows inline like inline elements (doesn't break line)
  • Accepts width/height like block elements
  • Respects all margin/padding like block elements
  • Perfect for creating side-by-side boxes

Example:

<!-- Create boxes that sit side-by-side -->
<div style="display: inline-block; width: 100px; height: 100px; background: red; margin: 5px;">
  Box 1
</div>
<div style="display: inline-block; width: 100px; height: 100px; background: blue; margin: 5px;">
  Box 2
</div>
<div style="display: inline-block; width: 100px; height: 100px; background: green; margin: 5px;">
  Box 3
</div>

Visual Result:

┌──────┐ ┌──────┐ ┌──────┐
│ Box 1│ │ Box 2│ │ Box 3│
└──────┘ └──────┘ └──────┘

Back to top

<div> vs <span>

The most common generic containers - understanding when to use each is essential.

Comparison Table:

Feature <div> <span>
Display Type Block-level Inline
Line Break Starts on new line Stays on same line
Default Width 100% of parent Content width only
Can Contain Block & inline elements Only inline elements
Sizing Accepts width/height Ignores width/height
Typical Use Layout sections, containers Text styling, small portions

Practical Example:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Div vs Span Example</title>
  <style>
    .container {
      background: lightgray;
      padding: 15px;
      margin: 10px 0;
    }

    .highlight {
      background: yellow;
      padding: 2px 5px;
    }
  </style>
</head>
<body>
  <!-- Div: Block-level container -->
  <div class="container">
    <h2>This is a Section</h2>
    <p>This entire section is wrapped in a div.</p>
  </div>

  <!-- Span: Inline styling -->
  <p>
    This paragraph has <span class="highlight">highlighted text</span> 
    using span. The span only affects specific words.
  </p>

  <!-- Multiple divs stack vertically -->
  <div class="container">First div</div>
  <div class="container">Second div</div>

  <!-- Multiple spans stay inline -->
  <p>
    <span style="color: red;">Red</span>
    <span style="color: green;">Green</span>
    <span style="color: blue;">Blue</span>
  </p>
</body>
</html>

Best Practices:

  • Use <div> for: Layout structure, grouping content sections, creating containers
  • Use <span> for: Styling specific text portions, inline elements within text
  • Semantic alternatives: Prefer semantic HTML when possible (header, nav, section vs div)
  • Avoid nested divs: Too many divs create "div soup" - use semantic elements

Back to top

Quick Quiz

Q1. Which of the following is a block element?
Q2. Which element is used as an inline container?
Q3. What happens with inline elements and width/height CSS?
Q4. What display value combines inline flow with block sizing?
Q5. Which element can contain both block and inline elements?

Back to top

Practice Tasks

  1. Block Element Demo:
    • Create 3 divs with different background colors
    • Add padding and margin to each
    • Observe how they stack vertically
  2. Inline Element Demo:
    • Create a paragraph with multiple span elements
    • Style each span with different colors
    • Try adding width/height and observe what happens
  3. Inline-Block Cards:
    • Create 3 card divs with display: inline-block
    • Set width: 200px, height: 150px for each
    • Add different background colors
    • They should sit side-by-side
  4. Div vs Span Comparison:
    • Create a page showing div behavior (stacking)
    • Create a page showing span behavior (inline flow)
    • Use borders/backgrounds to visualize differences
  5. Real-World Layout:
    • Build a simple blog post layout using divs for structure
    • Use spans to highlight important keywords in text
    • Add inline-block social media icons

Back to top

Lesson Summary

Key Takeaways:

  • Block elements: Start new line, take full width, accept all sizing/spacing
  • Inline elements: Flow with text, take content width, ignore width/height
  • Common block elements: div, p, h1-h6, ul, ol, table, header, footer, section
  • Common inline elements: span, a, img, strong, em, code, button
  • Div: Generic block container for layout and structure
  • Span: Generic inline container for text styling
  • Inline-block: Flows inline but accepts width/height (best of both)
  • CSS control: Can change any element's display with display property
  • Best practice: Use semantic HTML when possible, avoid excessive divs

Understanding block vs inline behavior is fundamental to HTML layout. This knowledge is essential before learning CSS positioning and flexbox/grid!