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.
<div> and <span> are and when to use eachdisplay property —
block, inline, inline-block, flex, grid, none<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 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>
<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:
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:
<div>, <p>, headings.
<span>, <a>, <strong>.visibility: hidden
which hides the element but keeps its space./* 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 | |||
| Default width | 100% of parent | Content width only | Content width only |
| width / height | |||
| margin (top/bottom) | |||
| padding (top/bottom) | ⚠️ Visual only — doesn't push layout | ||
| 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:
Common Mistakes — Do's & Don'ts
These are the most frequent layout element mistakes beginners make:
<!-- 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>
<!-- 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>
<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:
<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.
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.
Quick Check — Chapter 14
Expand each question to check your understanding before moving on to the multiple choice quiz.
<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.
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.
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.
<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.
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.
display value makes an element flow
inline but still accept width and height?<div> with a fixed max-width horizontally on a page, which CSS property is used?<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
displayproperty overrides default behaviour:block,inline,inline-block(inline flow + box sizing),flex,grid,none(removes from layout). display: noneremoves the element entirely (no space).visibility: hiddenhides 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.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions, and stay updated with new tutorials.