Tutorials HTML Layout Elements
Ch 14 / 40
Chapter 14 Beginner 8 min read HTML

HTML Layout Elements

Every web page is a hierarchy of containers. Understanding how HTML elements flow — block vs inline — is the foundation of all web layout. In this chapter you'll master <div> and <span>, understand the block/inline model, learn how the CSS display property transforms elements, and explore the most common layout container patterns used in real websites.

What you'll learn
What <div> and <span> are and when to use each
Block-level vs inline elements — the core layout model
Every block and inline HTML element listed
The CSS display property — block, inline, inline-block, flex, grid, none
Common layout container patterns used in real projects
Common layout mistakes and how to avoid them

<div> and <span> — The Generic Containers

Before HTML5 semantic elements, <div> and <span> were the only layout containers. They still play a vital role today — but for purely structural or styling purposes when no semantic element fits.

<div> — Block container
A block-level generic container. It takes up the full width of its parent and starts on a new line. Use it for CSS layout wrappers, flexbox/grid containers, card borders, modal overlays, and any grouping that needs block-level flow with no semantic meaning.
<span> — Inline container
An inline generic container. It wraps a portion of text or inline content without breaking the line flow. Use it to apply CSS styles or JavaScript hooks to a specific word or phrase inside a paragraph.
HTML — div and span in use
<!-- div: block container for CSS layout -->
<div class="card">
  <h2>Web Development Course</h2>
  <p>Learn HTML, CSS and JavaScript at K2infocom.</p>
</div>

<!-- span: inline container for text styling -->
<p>
  The course fee is
  <span style="color:#22c55e; font-weight:700;">₹8,999</span>
  with a
  <span style="color:#ef4444; text-decoration:line-through;">₹2,000</span>
  discount applied.
</p>
Div vs Semantic — Quick Rule
If a semantic element (<article>, <section>, <nav>, etc.) accurately describes the content's role, use it. If you're creating a layout structure with no inherent meaning — a card border, a flex row, a grid cell — <div> is the right choice.

Block-level vs Inline Elements

Every HTML element has a default display behaviour — either block or inline. This is the most fundamental concept in HTML layout:

Block-level elements — each starts on a new line, full width
<p> This is a paragraph — takes full width, starts new line <h2> This is a heading — also block, starts new line <div> This is a div — also block, starts new line
Block elements always start on a new line and take up the full available width of their parent container, pushing the next element down.
Inline elements — flow within text, only as wide as content
This is text with <span> an inline span and a <strong> strong tag and an <a> link all on the same line.
Inline elements flow within surrounding text, only as wide as their content. They do not start a new line and cannot have top/bottom margin or width/height set directly.
Block-level Elements
<div> <p> <h1>–<h6> <ul> <ol> <li> <table> <form> <header> <nav> <main> <section> <article> <aside> <footer> <figure> <blockquote> <pre> <hr> <address>
Inline Elements
<span> <a> <strong> <em> <b> <i> <u> <small> <mark> <del> <ins> <sub> <sup> <code> <abbr> <img> <input> <label> <button> <br>
Inline Limitations
Inline elements cannot have top/bottom margins, fixed width, or fixed height applied via CSS — these properties are ignored. To apply sizing to an inline element, change it to display: inline-block first.

The CSS display Property

The CSS display property overrides any element's default layout behaviour. You can make a <span> behave like a block, or a <div> behave like a flex container. Here are all the essential values:

block Block
The element takes up the full width of its parent and starts on a new line. Width, height, margin, and padding all work normally. Default for <div>, <p>, headings.
display: block; width: 200px; margin: 10px auto; /* centering works */
inline Inline
The element flows within text — only as wide as its content. Width, height, and top/bottom margins are ignored. Default for <span>, <a>, <strong>.
display: inline; /* width/height ignored */ /* flows within text */
inline-block Inline-Block
Best of both worlds — flows inline like text, but accepts width, height, and top/bottom margins like a block. Perfect for navigation buttons, badges, and icon+text combos.
display: inline-block; width: 120px; padding: 8px 16px; /* all work! */ margin-top: 10px;
flex Flexbox
Turns the element into a flex container. Its direct children become flex items arranged in a row (default) or column. The most powerful and commonly used layout system in modern CSS.
display: flex; gap: 16px; align-items: center; justify-content: space-between;
grid Grid
Turns the element into a grid container. Items are placed in defined rows and columns. Best for two-dimensional layouts — page structures, dashboards, image galleries.
display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px;
none None (Hidden)
The element is completely removed from the document flow — it takes up no space and is invisible. Different from visibility: hidden which hides the element but keeps its space.
display: none; /* removed from layout */ /* vs visibility:hidden */ /* (space preserved) */
CSS — display property examples
/* Make a span behave like a block */
span.banner {
  display: block;
  width: 100%;
  padding: 16px;
}

/* Make a div flow inline */
div.badge {
  display: inline;
}

/* inline-block: inline flow + box sizing */
.nav-btn {
  display: inline-block;
  width: 120px;
  padding: 10px 0;
  text-align: center;
}

/* Flexbox container */
.card-row {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
}

/* Grid container — 3 equal columns */
.course-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

/* Toggle visibility with JS */
.modal { display: none; }
.modal.open { display: flex; }

Block vs Inline — Key Differences at a Glance

Here is a complete side-by-side comparison of how block and inline elements behave in CSS:

Property Block Elements Inline Elements Inline-Block
Starts new line Yes — always No — flows inline No — flows inline
Default width 100% of parent Content width only Content width only
width / height Respected Ignored Respected
margin (top/bottom) Respected Ignored Respected
padding (top/bottom) Respected ⚠️ Visual only — doesn't push layout Respected
Can contain Block + inline elements Inline elements only Block + inline elements
Examples <div>, <p>, <h1> <span>, <a>, <strong> <img>, <button>, <input>

Common Layout Container Patterns

Here are the most frequently used <div>-based container patterns you'll see in every real project:

Container / Page Wrapper
Centres content horizontally and constrains the max-width. The most universal pattern — wraps every page section.
<div class="container"> ...page content... </div> /* CSS */ .container { max-width: 1200px; margin: 0 auto; padding: 0 20px; }
Section Wrapper
Wraps a full-width section with padding and a background colour. Stacked vertically to build page sections.
<div class="section-wrap"> <div class="container"> ... </div> </div> /* CSS */ .section-wrap { padding: 60px 0; background: #f9fafb; }
Card Component
A bordered, rounded box for individual content units — course cards, blog post previews, feature cards.
<div class="card"> <img src="..." alt="..."> <div class="card-body"> <h3>Title</h3> <p>Description</p> </div> </div>
Flex Row
Arranges children side by side using Flexbox. The backbone of navigation bars, button groups, and two-column layouts.
<div class="flex-row"> <div>Column 1</div> <div>Column 2</div> </div> /* CSS */ .flex-row { display: flex; gap: 20px; align-items: center; }
Grid Container
Arranges children in a multi-column CSS Grid. Perfect for course listings, image galleries, and dashboard panels.
<div class="grid"> <div class="card">...</div> <div class="card">...</div> <div class="card">...</div> </div> /* CSS */ .grid { display: grid; grid-template-columns: repeat(3,1fr); gap: 24px; }
Overlay / Modal Backdrop
A fixed full-screen div for modal backgrounds, lightboxes, or loading screens. Positioned over all other content.
<div class="overlay"> <div class="modal">...</div> </div> /* CSS */ .overlay { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: flex; align-items: center; justify-content: center; }

Common Mistakes — Do's & Don'ts

These are the most frequent layout element mistakes beginners make:

Do — Correct
<!-- Use semantic element when available -->
<nav class="main-nav">...</nav>

<!-- div for layout — no semantic meaning -->
<div class="flex-row">
  <img src="avatar.jpg" alt="...">
  <span>Rahul Shah</span>
</div>

<!-- inline-block for sizeable inline item -->
<span style="display:inline-block;
  width:24px;height:24px;">
  ★
</span>
Don't — Wrong
<!-- div when semantic exists -->
<div class="navigation">...</div>

<!-- block inside inline — invalid HTML -->
<span>
  <div>This breaks the model</div>
</span>

<!-- width on inline — has no effect -->
<span style="width:200px;">
  width is ignored on inline
</span>
Never Place Block Inside Inline
Placing a block-level element (like <div> or <p>) inside an inline element (like <span> or <a>) is invalid HTML. The browser will attempt to auto-correct it — but the result is unpredictable rendering. Always nest block elements inside block elements and inline elements inside inline or block elements.

Complete Example — Page Layout with div & span

A realistic page structure combining all layout concepts from this chapter:

HTML + CSS — Full page layout
<style>
  /* Container — centres and constrains width */
  .container {
    max-width: 1100px;
    margin: 0 auto;
    padding: 0 20px;
  }

  /* Section wrapper — full-width background band */
  .section-courses { padding: 60px 0; background: #f9fafb; }

  /* Flex row — side-by-side items */
  .course-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 32px;
  }

  /* Grid — 3-column card layout */
  .course-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 24px;
  }

  /* Card — individual item box */
  .card {
    background: white;
    border: 1px solid #e5e7eb;
    border-radius: 12px;
    overflow: hidden;
  }
  .card-body { padding: 18px; }
  .card-body h3 { font-size: 18px; margin-bottom: 8px; }
  .card-body p  { font-size: 15px; color: #6b7280; }

  /* Badge — inline styling with span */
  .badge {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 999px;
    font-size: 14px;
    font-weight: 600;
    background: #dcfce7;
    color: #16a34a;
    margin-bottom: 8px;
  }

  /* Price highlight */
  .price { font-size: 22px; font-weight: 800; color: #e44d26; }
  .old-price { text-decoration: line-through; color: #9ca3af; font-size: 15px; }
</style>

<!-- Section wrapper -->
<div class="section-courses">
  <div class="container">

    <!-- Flex row header -->
    <div class="course-header">
      <h2>Our Courses</h2>
      <a href="/courses">View All →</a>
    </div>

    <!-- Grid of cards -->
    <div class="course-grid">

      <div class="card">
        <img src="webdev.jpg" alt="Web Dev">
        <div class="card-body">
          <span class="badge">Bestseller</span>
          <h3>Web Development With AI</h3>
          <p>
            <span class="price">₹8,999</span>
            <span class="old-price">₹11,999</span>
          </p>
        </div>
      </div>

    </div>
  </div>
</div>

Try It Yourself

Edit the layout below and click Open in Editor to see block/inline behaviour and the display property live in full screen.

Live Editor
✎ Editor
👁 Preview
Try This
Click Toggle Box in the preview — it switches between display: none and display: block via JavaScript's classList.toggle(). Then try changing grid-template-columns: repeat(3, 1fr) to repeat(2, 1fr) and see the grid reflow instantly.
Free Resource
HTML5 Cheat Sheet — All Tags & Attributes
One printable PDF covering block vs inline elements, the CSS display property values, layout patterns, and div/span usage rules.
Download PDF
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React, and Node.js in one industry-ready curriculum with real projects and placement support.
120+
Chapters
40 hrs
Content
10
Projects
4.9 ⭐
Rating
HTML5 CSS3 JavaScript React.js Node.js MongoDB AI Tools

Quick Check — Chapter 14

Expand each question to check your understanding before moving on to the multiple choice quiz.

Questions & Answers
<div> is a block-level generic container — it starts on a new line, takes up the full width of its parent, and can contain both block and inline elements. <span> is an inline generic container — it flows within surrounding text without breaking the line, is only as wide as its content, and should only wrap inline content. Use <div> for layout sections and card wrappers; use <span> for styling a word or phrase within a paragraph.
Inline elements follow the inline box model, which explicitly ignores width, height, and top/bottom margin — these properties only apply to block-level boxes. Inline elements are sized purely by their content. To make an inline element accept sizing, change its display to inline-block — this gives it inline flow (stays in the text line) while also accepting width, height, padding, and margins like a block element.
display: inline-block is a hybrid — the element flows inline like text (sits on the same line as other inline content) but respects box model properties like width, height, and top/bottom margins like a block. Common use cases: navigation buttons that sit side by side but need fixed width; image+text inline combos that need padding; badges, tags, and pills where you need controlled sizing within flowing text. Many HTML elements like <img>, <button>, and <input> default to inline-block behaviour.
Both hide elements visually, but they behave differently in the layout. display: none completely removes the element from the document flow — it takes up no space, as if it doesn't exist in the DOM at all. visibility: hidden hides the element but preserves its space in the layout — you'll see a blank gap where the element would be. Use display: none when you want to toggle elements on and off (modals, dropdowns). Use visibility: hidden when you need to hide content but maintain the page layout dimensions.
The HTML spec defines that inline elements can only contain other inline elements — they cannot contain block-level elements. A <span> is inline, and a <div> is block-level. Placing a block inside an inline creates an invalid nesting that violates the content model. While browsers attempt to auto-correct this (typically by closing the inline element and opening the block outside it), the result is unpredictable and browser-specific. Always nest block inside block and inline inside block or inline.
The container (or wrapper) pattern is a <div> with max-width: 1200px (or similar) and margin: 0 auto. The max-width prevents content from stretching uncomfortably wide on large monitors, and margin: 0 auto centres it horizontally. Adding padding: 0 20px ensures content doesn't touch screen edges on mobile. This pattern is used on virtually every modern website to create readable, centred content columns while keeping section backgrounds full width. It is the universal starting point for any page layout.
Multiple Choice Questions
MCQ 1 of 4
Which HTML element is a block-level generic container used for CSS layout purposes?
MCQ 2 of 4
Which CSS display value makes an element flow inline but still accept width and height?
MCQ 3 of 4
Which CSS value removes an element completely from the document layout and makes it take no space?
MCQ 4 of 4
To centre a <div> with a fixed max-width horizontally on a page, which CSS property is used?
Chapter Summary
  • <div> is a block-level generic container — full width, new line, accepts all box model properties. Use for layout wrappers, card borders, flex/grid containers.
  • <span> is an inline generic container — flows within text, width/height/top-bottom margins ignored. Use for styling words or phrases inside paragraphs.
  • Block elements start on new lines and take full width. Inline elements flow within text and are only as wide as their content. Never place block elements inside inline elements.
  • The CSS display property overrides default behaviour: block, inline, inline-block (inline flow + box sizing), flex, grid, none (removes from layout).
  • display: none removes the element entirely (no space). visibility: hidden hides visually but preserves space.
  • Key layout patterns: container (max-width + margin: 0 auto), flex row, CSS grid, card, section wrapper, modal overlay — all built from <div> + CSS.
~8 min read
Beginner level
Chapter 14 of 40

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
If this tutorial helped you, a 5-star Google review means a lot. It helps other learners discover our free content and motivates us to keep improving.
500+ Reviews (Target)
Write a Google Review
Join WhatsApp Channel