Block and inline
Overview
Before flexbox, before grid, there’s normal flow — the way browsers lay out content when you haven’t asked for anything else. It’s designed for documents: headings, paragraphs, lists, links and images, readable on any screen. Every layout you build starts from it, and much of a good page stays in it.
Two kinds of boxes
Block boxes
Elements like <div>, <p>, <h1>–<h6>, <ul>, <section>, <header> and <form> are block by default. Block boxes:
- start on a new line and stack vertically, one after another,
- fill the available width of their container (unless you set
width), - are as tall as their content,
- respect
width,height,padding,borderandmarginon all four sides.
Inline boxes
Elements like <span>, <a>, <strong>, <em>, <code> and <abbr> are inline. Inline boxes:
- flow along the line, next to the text around them,
- wrap onto the next line when they run out of space — a single link can be split across two lines,
- are as wide as their content;
widthandheightare ignored, - accept horizontal padding and margins, but vertical padding and margins don’t push other lines away — vertical padding on an inline background overlaps the lines above and below.
a {
padding: 0.5rem; /* the background grows, but the line spacing doesn't change */
background: #eaf0fc;
}This is why making a link look like a button “doesn’t work” until you change its display.
Changing display
nav a { display: block; } /* each link on its own line, clickable across the full width */
li { display: inline; } /* list items in a line */
.badge { display: inline-block; } /* flows like a word, sizes like a block */
.hidden { display: none; } /* removed from layout and from screen readers */inline-block
inline-block combines the two: the box sits in a line of text like a word (outside behaviour), but inside it acts like a block — it accepts width, height, and vertical padding and margins that push the lines apart. It’s great for badges, tags and small buttons within text.
For rows of items — navigation links, toolbars, card rows — flexbox is a better tool than inline-block; you’ll learn it in the Flexbox chapter. One reason: inline-block elements are separated by the whitespace in your HTML (line breaks and spaces between tags show up as small gaps).
display: none vs visibility: hidden
display: noneremoves the element completely: it takes no space, isn’t announced by screen readers, and can’t be focused.visibility: hiddenhides it but keeps its space in the layout (and also hides it from screen readers).- To hide something visually but keep it available to screen readers, use a visually hidden utility class (see Layouts that work for everyone).
Inline content and line boxes
Text and inline boxes are arranged into line boxes — one per line of text. Each line box is tall enough for its tallest inline content, which is why a large inline image or an inline-block with a big font pushes the line taller. The height of a line of text comes from line-height:
body { line-height: 1.5; } /* unitless: 1.5 × the element's font size */Use a unitless line-height so it scales with each element’s own font size.
The gap under images
Images are inline by default and sit on the text baseline, leaving a few pixels of space below them for letters like “g” and “y”. If you see a mysterious gap under an image in a card, make the image a block:
img { display: block; max-width: 100%; }That rule — plus max-width: 100% so images never overflow their container — is in nearly every CSS reset.
Block formatting contexts
A block formatting context (BFC) is a self-contained region of normal flow. Inside it, blocks stack; floats and margins don’t leak out of it. The root <html> element creates one, and so do flex and grid items, positioned elements, and elements with overflow other than visible.
You can create one on purpose with display: flow-root:
.media {
display: flow-root; /* contains floated children and stops child margins escaping */
}You’ll see why that matters in the next lesson, on margins.
Floats, briefly
float: left takes an element to one side and lets text wrap around it — the classic magazine layout:
.article img.pull-left {
float: inline-start;
width: 40%;
margin-inline-end: 1rem;
}Floats were once used for whole page layouts. Don’t: flexbox and grid do that job far better. Use floats only for what they were designed for — wrapping text around an image.
Remember: most content should stay in normal flow. Reach for flex and grid to arrange components, not to replace paragraphs flowing naturally down the page.
Pill-shaped tags in a sentence
Inside a paragraph, style <a class="tag"> links as pill-shaped tags: 0.25rem × 0.75rem padding, a rounded background, and a little space between them. The vertical padding must not overlap the lines of text above and below, and the tags must wrap with the text like words. Then make the .note block shrink to fit its text instead of spanning the whole width.
<p>
Topics:
<a class="tag" href="#">flexbox</a>
<a class="tag" href="#">grid</a>
<a class="tag" href="#">container queries</a>
<a class="tag" href="#">anchor positioning</a>
— and much more in this course.
</p>
<div class="note">Updated December 2026</div>
<style>
.tag { background: #eaf0fc; color: #1e44ae; text-decoration: none; }
.note { background: #fff6e5; padding: 0.5rem; }
/* your code here */
</style><p>
Topics:
<a class="tag" href="#">flexbox</a>
<a class="tag" href="#">grid</a>
<a class="tag" href="#">container queries</a>
<a class="tag" href="#">anchor positioning</a>
— and much more in this course.
</p>
<div class="note">Updated December 2026</div>
<style>
.tag { background: #eaf0fc; color: #1e44ae; text-decoration: none; }
.note { background: #fff6e5; padding: 0.5rem; }
p { line-height: 2; }
.tag {
display: inline-block; /* flows like a word, but padding takes up space */
padding: 0.25rem 0.75rem;
border-radius: 999px;
margin-block: 0.125rem;
line-height: 1.4;
}
.note {
display: inline-block; /* or: width: fit-content on a block */
}
</style>Summary
- Normal flow stacks block boxes vertically and lays inline boxes along lines that wrap.
- Block boxes fill the available width and respect width, height and all margins and padding.
- Inline boxes ignore width and height, and their vertical padding and margins don’t push other lines away.
inline-blockflows like text but behaves like a block inside;display: noneremoves the box entirely.display: flow-rootcreates a new block formatting context that contains floats and stops margins escaping.