HTML Block & Inline Elements Explained â Div, Span & Display
In this tutorial, you'll learn about HTML Block & Inline Elements Explained. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
HTML elements are either block-level (starting on a new line and taking full width) or inline (flowing within text) â understanding this difference is key to controlling page layout.
What You'll Learn
By the end of this tutorial, you'll know the difference between block and inline elements, when to use <div> vs <span>, and how to display code snippets with <code>, <pre>, <kbd>, and <samp>.
Prerequisite: You should know basic HTML tags. If not, start with HTML Basics.
Why Display Behavior Matters
Imagine packing a suitcase. Some items (like a shirt) take up the whole width of the suitcase â that's block behavior. Other items (like socks) can share space with other items â that's inline behavior.
Security note: Understanding Html Block Inline helps build more secure applications â a core principle at DodaTech, where tools like Durga Antivirus Pro and Doda Browser rely on solid implementation practices.
Every HTML element has a default display type. Knowing whether an element is block or inline helps you predict how it will look on the page and how to style it.
Real-world use: When building layouts, you use <div> (block) for sections and <span> (inline) for highlighting text inside paragraphs. Getting this wrong leads to broken layouts.
Where This Fits in Your Learning Path
flowchart LR
A["Quotations & Comments"] --> B["**Block & Inline**"]
B --> C["Classes & Id"]
C --> D["Buttons & Iframes"]
D --> E["Head & Meta"]
E --> F["Master HTML Page"]
style B fill:#f97316,stroke:#c2410c,color:#fff
style A fill:#e5e7eb,stroke:#9ca3af,color:#374151
style F fill:#22c55e,stroke:#16a34a,color:#fff
Block-Level Elements
A block-level element:
- Starts on a new line (it pushes everything after it to the next line)
- Takes up the full width available (stretches from left to right)
- Has height (you can set width, height, margin, and padding)
<div>Block element 1 â takes the full width</div>
<div>Block element 2 â appears on a new line below</div>
Common block elements:
| Element | Purpose |
|---|---|
<div> |
Generic block container |
<p> |
Paragraph |
<h1>â<h6> |
Headings |
<ul>, <ol>, <li> |
Lists |
<table> |
Tables |
<form> |
Forms |
<header>, <footer>, <section>, <article>, <main> |
Semantic containers |
Stacking Behavior
Block elements stack vertically â like bricks in a wall. Each one sits below the previous one. This is the natural flow of HTML content.
Inline Elements
An inline element:
- Does NOT start on a new line (it sits within the current line)
- Takes only as much width as necessary (just enough for its content)
- Ignores width and height settings (margin and padding work differently)
<p>
<span>Inline 1</span>
<span>Inline 2</span>
<span>Inline 3</span>
â all on the same line!
</p>
Common inline elements:
| Element | Purpose |
|---|---|
<span> |
Generic inline container |
<a> |
Links |
<strong>, <em> |
Bold and italic text |
<img> |
Images (inline but with height/width) |
<input>, <label> |
Form elements |
<code> |
Inline code |
Side-by-Side Behavior
Inline elements sit horizontally â like words in a sentence. They only wrap to the next line when there's not enough space.
The <div> Element â Generic Block Container
<div> is the most versatile block element. It has no default styling â it's just a container that you can style with CSS:
<div class="card">
<h2>Card Title</h2>
<p>Card content goes here.</p>
</div>
Use <div> when you need to group elements for:
- Styling â apply a background, border, or padding to a group
- Layout â create columns or sections with CSS Flexbox or grid
- JavaScript â target a group of elements with a script
The <span> Element â Generic Inline Container
<span> is the inline equivalent of <div>. It has no default styling and is used to wrap a piece of text within a larger block:
<p>This is <span class="highlight">highlighted text</span> within a paragraph.</p>
Use <span> when you need to style or target a specific part of text without breaking the line.
Computer Code Elements
HTML provides special elements for displaying code. These are essential for any tutorial site (like this one!).
| Element | Purpose | Display |
|---|---|---|
<code> |
Inline code snippet | Inline, monospace font |
<pre> |
Preformatted text (preserves spaces/line breaks) | Block, monospace font |
<kbd> |
Keyboard input | Inline, monospace |
<samp> |
Sample program output | Inline, monospace |
<var> |
Variable name | Inline, italic |
<p>Use <code>console.log()</code> to print to the console.</p>
<pre>
function greet(name) {
return "Hello, " + name + "!";
}
</pre>
<p>Press <kbd>Ctrl</kbd> + <kbd>S</kbd> to save.</p>
<p>Output: <samp>Hello, World!</samp></p>
<p>The variable <var>x</var> equals 5.</p>
Why <pre> + <code> for Code Blocks
For multi-line code blocks, always wrap <code> inside <pre>:
<pre><code>
// This preserves indentation and line breaks
function hello() {
console.log("Hello, World!");
}
</code></pre>
<pre>preserves whitespace (spaces, tabs, newlines) â without it, browsers collapse multiple spaces into one<code>marks the content as code (semantic meaning for search engines and screen readers)
Changing Display with CSS
You can change any element's display behavior with CSS:
/* Make a block element behave like inline */
.block-element {
display: inline;
}
/* Make an inline element behave like block */
.inline-element {
display: block;
}
/* Hide an element completely */
.hidden {
display: none;
}
This is useful when you want a <div> to sit side-by-side with another element, or when you want a <span> to have width and height (normally impossible for inline elements).
Common Mistakes
1. Putting Block Elements Inside Inline Elements
<!-- â Wrong: block element (h2) inside inline (a) -->
<a href="#">
<h2>Heading</h2>
</a>
<!-- â
Correct: put inline inside block instead -->
<h2>
<a href="#">Heading</a>
</h2>
2. Expecting Inline Elements to Respect Width/Height
<!-- â Wrong: span ignores width and height -->
<span style="width: 200px; height: 100px;">Text</span>
<!-- â
Correct: use display: inline-block or a block element -->
<span style="display: inline-block; width: 200px; height: 100px;">Text</span>
3. Using <br> for Spacing Instead of Block Elements
<!-- â Wrong: br for spacing -->
Text<br><br>More text
<!-- â
Correct: use block elements -->
<p>Text</p>
<p>More text</p>
4. Forgetting That <img> Is Inline (But Behaves Differently)
<!-- Images sit inline with text by default -->
<p>Text <img src="icon.png" alt="icon"> continues on same line</p>
<img> is technically inline but behaves like inline-block â it respects width/height.
5. Using <div> When <span> Would Do
<!-- â Overkill: div forces a line break for no reason -->
<div class="highlight">This word is highlighted</div>
<!-- â
Correct: span keeps text inline -->
<span class="highlight">This word</span> is highlighted
Try It Yourself
Experiment with block and inline elements in this sandbox:
Practice Questions
Q1: What's the main difference between block and inline elements? {{< details "Show answer" >}} Block elements start on a new line and take full width. Inline elements stay on the same line and only take as much width as needed. {{< /details >}}
Q2: Can you set width and height on a <span>?
{{< details "Show answer" >}}
Not by default â <span> is inline and ignores width/height. Use display: inline-block to make it respect dimensions.
{{< /details >}}
Q3: When should you use <div> vs <span>?
{{< details "Show answer" >}}
Use <div> for grouping larger sections of content (block). Use <span> for styling small pieces of text within a line (inline).
{{< /details >}}
Q4: Why is <pre> used for code blocks?
{{< details "Show answer" >}}
<pre> preserves whitespace (spaces, tabs, newlines) â without it, browsers collapse multiple spaces into one and ignore line breaks.
{{< /details >}}
Q5: What does <kbd> represent?
{{< details "Show answer" >}}
Keyboard input â like <kbd>Ctrl</kbd> + <kbd>S</kbd>. Browsers render it in monospace font.
{{< /details >}}
Challenge
Create a "Documentation Page" that includes:
- A main heading and several sections using
<div>or semantic block elements - At least 3 inline
<span>elements with different CSS classes - A code block using
<pre>+<code> - Keyboard shortcuts using
<kbd> - A variable reference using
<var>
Frequently Asked Questions
{{< faq "Can I change an inline element to block with CSS?" >}}
Yes! Use display: block to make any element block-level, or display: inline-block for a hybrid that respects width/height but sits inline.
{{< /faq >}}