HTML Block and Inline
HTML Block and Inline Elements: How Display Flow Works
Every HTML element defaults to a specific layout behavior, most commonly block or inline. Knowing whether an element starts on a new line or sits inside text flow explains why some tags stack vertically while others sit side by side.
Block-Level Elements
A block-level element always begins on a new line and stretches across the full available width of its parent container. You can set explicit width, height, top/bottom margin, and padding on block elements freely.
Common block elements include div, p, h1 through h6, section, article, header, footer, ul, ol, and table. The div element serves as a generic block container when no semantic landmark tag fits.
Inline Elements
An inline element stays inside the current line of text and takes up only as much width as its content needs. Vertical margin-top and margin-bottom, as well as width and height, have no effect on pure inline elements.
Common inline elements include span, a, strong, em, code, label, and img (which behaves as replaced inline content). The span element acts as a generic inline hook for styling a single phrase inside a paragraph.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Block vs Inline Demo</title>
<style>
.block-box { background: #e2e8f0; padding: 12px; margin-bottom: 12px; }
.badge { background: #dbeafe; color: #1e40af; padding: 2px 8px; border-radius: 4px; }
</style>
</head>
<body>
<div class="block-box">This div is a block element occupying 100% width.</div>
<p>
This paragraph contains an inline
<span class="badge">HTML5</span>
span badge that stays on the same line.
</p>
</body>
</html>
Common Mistakes to Avoid
- Nesting block elements inside inline elements: Never wrap a
divorpinside aspan. Note that HTML5 permitsatags to wrap block cards, thoughspanandstrongmust only hold inline content. - Trying to set
heighton aspan: Switch the element todisplay: inline-blockordisplay: blockin CSS if you need explicit width or height. - Using
diveverywhere ("div soup"): Pick semantic block tags likemain,nav, andarticlebefore reaching fordiv.
Frequently Asked Questions
Can I change an inline element into a block element?
Yes. CSS display: block; or display: inline-block; overrides any element's default layout behavior.
What is the difference between div and span?
div is a block-level container for grouping sections and layout blocks, while span is an inline wrapper for styling small pieces of text inside a line.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.