HTML Basics
HTML Block and Inline Elements
Every HTML element is either block-level or inline by default, and that determines how it sits on the page. Understanding this difference is key to controlling layout.
What are Block and Inline Elements?
A block-level element starts on a new line and stretches to fill the full width available. Headings, paragraphs, and <div> are block elements — they stack vertically like boxes.
An inline element only takes up as much width as its content and sits within a line of text without breaking it. Links, <strong>, and <span> are inline elements — they flow alongside the surrounding words.
Basic Usage
<div>Block element (new line, full width)</div>
<span>Inline element</span> <span>stays on the same line</span>Example: See the Difference
<!DOCTYPE html>
<html>
<body>
<div style="background:#cfe2ff;">Block 1 fills the whole width.</div>
<div style="background:#d1e7dd;">Block 2 starts on a new line.</div>
<p>
Inline elements like
<span style="background:#f8d7da;">this span</span> and
<span style="background:#fff3cd;">this one</span>
sit inside the text.
</p>
</body>
</html>More Examples
<div style="border:2px solid #0d6efd; padding:12px;">
<h3>A card</h3>
<p>The div groups these elements into one block.</p>
</div><p>Your balance is
<span style="color:green; font-weight:bold;">₹1,200</span>
as of today.</p>Block vs Inline at a Glance
| Feature | Block | Inline |
|---|---|---|
| Starts on new line | Yes | No |
| Takes full width | Yes | No |
| Width / height settable | Yes | Not by default |
| Examples | div, p, h1, ul, li | span, a, strong, em, img |
Use <div> as a generic block container and <span> as a generic inline container. Neither adds meaning — reach for semantic tags first, and use these only when no better tag fits.
You can change an element's behaviour with CSS: display:inline, display:block, or display:inline-block override the default.
Key Takeaways
- Block elements start on a new line and fill the width.
- Inline elements flow within text and hug their content.
- <div> is a block container; <span> is an inline container.
- CSS display can switch an element between block and inline.
