Display and Normal Flow
How block and inline boxes lay themselves out by default, what display actually controls, and when to take an element out of the flow.
How CSS Works and Where It LivesThe Box Model and box-sizing
Display and Normal Flow
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Normal flow is the layout you get with no layout CSS: block boxes stack vertically and fill the available width, inline boxes run left to right and wrap like words.
- `width`, `height` and vertical margins do not apply to a plain `display: inline` box. `inline-block` keeps the line-flow behavior and accepts them.
- `display` has two parts, an outer type (how the box sits among siblings) and an inner type (how its children are laid out). `display: flex` is shorthand for `block flex`, and `inline-block` is `inline flow-root`.
- `display: none` removes the element from layout and from the accessibility tree. `visibility: hidden` keeps its space but hides it.
- `display: contents` removes the element's own box so its children take part in the parent's layout, but be careful with semantics on buttons, lists and tables.
The layout you get for free
Write HTML with no CSS and the page is still readable. Paragraphs stack, headings sit between them, links stay inside the sentence they belong to. That behavior is normal flow, and it is the baseline every other layout system modifies. Flexbox and Grid do not replace it; they switch it off for a container’s children. If you do not understand normal flow, a lot of CSS looks like unrelated tricks.
Two kinds of box drive it.
Block boxes
A block box starts on a new line and, with width: auto, stretches to fill its container’s available width. Siblings stack top to bottom. <div>, <p>, <h1>, <ul> and <section> are blocks by default. You can give a block a width, height, padding, border and margin on all four sides, and they all behave.
Inline boxes
An inline box does not start a new line. It sits in a line of text like a word, and wraps to the next line when there is no room. <span>, <a>, <em> and <strong> are inline. Their size comes from their content, so:
widthandheighthave no effect.- Horizontal padding and margin work.
- Vertical padding and border paint, but do not push lines apart.
I set width: 300px; height: 100px on a <span> in Chromium; the box measured about 32px wide and the declarations did nothing, while the same span with display: inline-block measured 320px. That is the usual “my width doesn’t work” bug.
inline-block: the hybrid
display: inline-block flows with text like an inline box but is a full block inside, so it accepts width, height and vertical margins. Buttons and badges often want this.
One side effect catches everyone once. Inline-level boxes are separated by whitespace in the HTML, and a line break renders as a space. Two inline-block elements on separate source lines get a gap of about the width of a space character. Flex and Grid children do not have this problem, because they are not laid out as text.
display is really two settings
Old CSS had one keyword per behavior. The modern model splits it:
- the outer type:
blockorinline, which says how the box participates in its parent’s layout. - the inner type:
flow,flow-root,flex,grid, which says how its children are laid out.
display: block flex; /* same as display: flex */
display: inline flex; /* same as display: inline-flex */
display: inline flow-root; /* same as display: inline-block */
Chromium reported flex and inline-block for the first and third. The two-value form is clearer, once you know it exists, about why inline-flex and inline-grid are not separate inventions. flow-root is an inner type that makes a box a self-contained block formatting context; the next lesson uses it to stop margin collapse and contain floats.
Making things disappear
display: noneremoves the element from layout and from the accessibility tree.visibility: hiddenkeeps the space, paints nothing, and also hides it from assistive technology.- The HTML
hiddenattribute isdisplay: nonefrom the browser’s stylesheet, which anydisplayrule of yours overrides. opacity: 0hides the paint but the element still takes space and still receives clicks and focus.
display: contents
display: contents deletes the element’s own box and lets its children behave as if they were children of its parent:
<div class="row">
<div class="group">
<p>a</p><p>b</p>
</div>
</div>
.row { display: flex; }
.group { display: contents; } /* a and b become flex items of .row */
I confirmed in Chromium that .group ends up with a width of 0, with its children placed by the row. It is useful for grid and flex when a wrapper div gets in the way. Historically it also removed some elements from the accessibility tree, so test buttons, lists and tables with a screen reader before applying it to them; the video has a chapter on accessibility concerns.
Going beyond the flow
Out-of-flow layout is the next step: position: absolute, float, and the multi-dimensional systems. For the latter, see the flexbox and grid lesson in the frontend roadmap for a first pass, and the Flexbox and Grid groups of this roadmap for depth.
What the video covers
Kevin Powell’s “Block, Inline, and Inline-Block explained”, about 14 minutes, from May 2020. By its title and description it explains how block, inline and inline-block each behave, and why you might switch an element’s display value. The description also links his videos on outlines for debugging and on the two-value display syntax. I could not read a transcript, so the experiment numbers and the display: contents and visibility notes are this lesson’s own. The runner-up is a 2022 video on display: contents with a chapter on accessibility.
Where this fits
Normal flow is where margin collapsing comes from, which is the next lesson: margin collapsing and formatting contexts. Flexbox and Grid begin from this lesson: flexbox fundamentals and grid fundamentals and tracks both start by changing a container’s inner display type.
Check yourself
You set `width: 300px; height: 100px` on a `<span>` and nothing changes. Why, and what are two fixes?
A `<span>` is `display: inline`, and inline boxes ignore `width` and `height` because their size comes from their content as it flows in a line. Set `display: inline-block` to keep it in the line and accept dimensions, or `display: block` to make it a full box on its own line.
What is the difference between `display: none` and `visibility: hidden`?
`display: none` removes the element from layout entirely, so it takes no space and is not exposed to assistive technology. `visibility: hidden` hides the paint but leaves the box in the layout at the same size.
Why do two `inline-block` buttons written on separate lines in the HTML have a gap between them, and how does flex avoid it?
The line break between them is whitespace, which collapses to a single space character in an inline formatting context, and that space is rendered. Flex lays out its children as flex items, not as inline content, so inter-element whitespace is ignored.
What does `display: inline flow-root` compute to?
`inline-block`. The two-keyword syntax names the outer type (`inline`) and the inner type (`flow-root`) separately, and the single keyword `inline-block` is the older name for that pair. Chromium reports `inline-block` for it.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.