Elements and Boxes: Block, Inline, Replaced Elements and the display Property
How CSS turns HTML elements into boxes: block and inline boxes, replaced and non-replaced elements, anonymous boxes, the outer and inner display types behind the display property, its two-value syntax, inline-block, flow-root, list-item, and why HTML elements have the display they do.
kvi5hnuCode Sprout team member11 min readOn this page
CSS doesn't style HTML tags directly. It turns each element into one or more boxes and lays those boxes out. Whether an element starts on a new line, whether it accepts a width, and how its children are arranged all depend on the kind of box it generates, which the display property controls. This article covers the basic kinds of boxes and elements. Later parts cover each layout mode in depth: normal flow in Part 6, flexbox in Part 9 and grid in Part 10.
Block and inline boxes
Load an unstyled page and two behaviors are visible right away:
- Block boxes stack vertically. Each starts on a new line and, by default, stretches across the full width of its container. Headings, paragraphs, lists,
<div>,<section>and<form>are block-level by default. - Inline boxes sit inside lines of text, side by side, and wrap onto the next line when the line is full.
<a>,<strong>,<em>,<span>,<code>and<abbr>are inline by default.
div, p { outline: 2px solid #8a3b12; }
span, a, strong { outline: 2px dashed #2f7d4f; }
The dashed outline around the long span is drawn in two pieces: an inline box that wraps is split into fragments, one per line.
The two kinds also respond differently to sizing:
| Block box | Inline box (non-replaced) | |
|---|---|---|
| starts a new line | yes | no |
| default width | fills its container | as wide as its content |
width and height | apply | ignored |
vertical margin | pushes other boxes away | ignored by line layout |
vertical padding and border | take up space | drawn, but don't push lines apart |
| contains | blocks, or lines of inline content | text and other inline boxes |
That last pair of rows surprises people. Padding on a link spreads its background, but neighboring lines don't move to make room, so a big vertical padding overlaps the lines above and below:
a {
padding: 10px 4px;
background: #f4c95d;
margin: 30px; /* the vertical part does nothing */
}
Replaced and non-replaced elements
Most elements have their content inside the document: text and child elements, which CSS lays out. These are non-replaced elements.
A replaced element shows content that comes from outside CSS formatting: an image file, a video, a form control drawn by the browser, or a separate document. <img>, <video>, <iframe>, <embed> and, in practice, form controls such as <input> are replaced elements. They have three special traits:
- They have an intrinsic size, taken from their content: a 600 × 400 image is 600px wide unless CSS says otherwise.
widthandheightapply even when they're inline. An inline image accepts a width, while an inline<span>doesn't.- CSS can't style their insides.
object-fitandobject-positioncontrol how the content fits the box (article 38), but you can't select the pixels of an image.
Images are inline by default, which is why an image inside a block sits on the text baseline with a small gap underneath for descenders. Making it display: block removes the gap (article 45).
The display property
display decides what kind of box an element generates. Its value has two parts, even when you write only one keyword:
- The outer display type says how the box takes part in its parent's layout:
block(stacks, starts a new line) orinline(sits in a line). - The inner display type says how the box lays out its own children:
flow(normal flow, for blocks and text),flow-root(normal flow in a new formatting context),flex,grid,tableorruby.
The familiar single keywords are shorthands for pairs:
| Short form | Two-value form | Outer behavior | Children laid out as |
|---|---|---|---|
block | block flow | block | normal flow |
inline | inline flow | inline | normal flow |
inline-block | inline flow-root | inline | normal flow, isolated |
flow-root | block flow-root | block | normal flow, isolated |
flex | block flex | block | flexbox |
inline-flex | inline flex | inline | flexbox |
grid | block grid | block | grid |
inline-grid | inline grid | inline | grid |
list-item | block flow list-item | block, with a marker | normal flow |
table | block table | block | table layout |
So display: flex makes a box that behaves like a block toward its neighbors, while arranging its own children as flex items. display: inline-flex does the same inside a line of text. The two-value syntax spells this out, and every current browser supports it, but the single keywords are shorter and more common.
.tags { display: flex; gap: 6px; outline: 2px solid #8a3b12; }
.inline-tags { display: inline flex; gap: 6px; outline: 2px solid #2f7d4f; }
.tag { background: #f4efe6; padding: 2px 8px; border-radius: 4px; }

Only the display value differs between these panels: the same width, height and margin are ignored, partly applied or fully applied depending on the kind of box.
The first container is a block that fills the line. The second is an inline flex container that sits within the sentence. In both, the tags inside are flex items.
Other display values
inline-blockmakes an inline box whose inside is a block: it sits in a line of text like a word, but acceptswidth,heightand vertical margins. It was the standard way to put boxes side by side before flexbox, and it's still handy for buttons and badges.flow-rootmakes a block that contains its floats and stops its children's margins from collapsing through it (articles 40 and 44).list-itemgenerates a block plus a marker box, which is how<li>gets its bullet (article 94). Any element withdisplay: list-itemgets a marker.nonegenerates no box at all: the element and its descendants disappear from layout.contentsgenerates no box for the element itself, but keeps its children, as if they were children of its parent. Article 46 covers both, including the accessibility history ofcontents.- Table values (
table,table-row,table-celland others) give any elements table layout (article 74). rubylays out ruby annotations, the small pronunciation text placed above East Asian characters.
Default display values come from the browser
HTML elements have no built-in display type in CSS. Their defaults come from the browser's user-agent stylesheet, which says, among many other things:
div, p, h1, ul, section, form { display: block; }
li { display: list-item; }
head, script, style, [hidden] { display: none; }
table { display: table; }
Elements without a rule, including any element whose name the browser doesn't know, such as a custom element <book-card>, default to inline, the initial value of display. That's why a custom element sits inside text until you give it display: block.
Since display is just a property, you can change any element's role. A list of links can become a horizontal row, and a link can become a block that fills its card:
nav ul { display: flex; gap: 1rem; list-style: none; padding: 0; }
.card { width: 14rem; border: 1px solid #ccc; }
.card a { display: block; padding: 0.75rem; text-decoration: none; }
.card a:hover { background: #f4efe6; }
Changing display doesn't change how HTML is parsed either: a <div> inside a <p> still closes the paragraph, because the parser follows HTML's nesting rules, not CSS. And it changes only the box, not the meaning of the element: a <ul> with display: flex is still a list to screen readers, and a <span> made display: block doesn't become a paragraph. Choose HTML elements for their meaning, and use CSS for their appearance.
Anonymous boxes
Sometimes CSS needs a box that no element provides, so it creates an anonymous box. The common case is text mixed with blocks:
<div>
Opening hours
<p>Monday to Saturday, 9:00 to 18:00.</p>
Closed on Sunday.
</div>
A block container holds either only blocks or only inline content, never both. So the browser wraps "Opening hours" and "Closed on Sunday." in anonymous block boxes. They behave like blocks, but you can't select them, so they inherit styles from the <div> and nothing else. The same happens in flex and grid containers, where loose text becomes an anonymous flex or grid item, and in tables, where missing rows or cells are created as anonymous table boxes.

In the right panel, the two text runs are spaced by gap just like the paragraph between them: each is an anonymous flex item, even though no element wraps it.
What's current
- The two-value syntax (
display: inline flex,display: block flow-root) has been supported in all major browsers since 2023. display: contentsis supported everywhere. Its long-standing accessibility bugs, where elements such as buttons and headings lost their role, were mostly fixed between 2022 and 2024, but test with a screen reader before using it on interactive or semantic elements (article 46).display: rubyis supported in Firefox and in Chromium since 2024, but not yet in Safari, which still lays out<ruby>correctly with its own internal styles.run-in, a value that would merge a heading into the next paragraph, is in the specification but not supported by any current browser.
Common questions
Why does width do nothing on my <span> or <a>?
They're inline boxes, and width and height don't apply to non-replaced inline boxes. Give the element display: inline-block if it should stay in the line of text, or display: block if it should stand alone. Inside a flex or grid container, children are flex or grid items and accept widths whatever their own display is.
Is <img> block or inline?
Inline by default, but it's a replaced element, so width and height work on it. Because it's inline, it sits on the text baseline, which leaves a gap below it inside a container. display: block on the image, or vertical-align: middle, removes the gap.
What's the difference between inline-block and inline?
Both sit inside a line of text. An inline box flows as text: it can break across lines, and ignores width, height and vertical margins. An inline-block box is a solid rectangle: it never breaks, and it accepts sizes, margins and padding that push lines apart.
Does changing display change what the element means?
No. Semantics come from the HTML element. Screen readers announce a <ul> as a list and a <button> as a button whatever their display. The exceptions are display: none, which hides the element from assistive technology too, and historically display: contents, which some browsers used to mishandle.
Why is my custom element, such as <book-card>, behaving like a span?
Browsers don't have default styles for custom elements, so they get the initial value of display, which is inline. Add book-card { display: block; }, or set it with :host inside the component's shadow DOM (article 14).
Summary
- CSS turns elements into boxes. Block boxes stack and fill their container's width, and inline boxes flow inside lines of text.
- Inline non-replaced boxes ignore
width,heightand vertical margins, and their vertical padding doesn't move other lines. - Replaced elements, such as images, videos and iframes, have intrinsic sizes and accept widths even when inline.
displayhas an outer type (blockorinline) and an inner type (flow,flow-root,flex,grid,tableorruby), and the familiar keywords are shorthands for these pairs.inline-blockis inline outside and a block inside,flow-rootis a block that contains floats, andlist-itemadds a marker.- Default display values come from the browser's stylesheet. Unknown and custom elements default to
inline. - Browsers create anonymous boxes to wrap text that sits next to blocks, and around loose content in flex, grid and table containers.
Practice questions
What are the outer and inner display types of display: inline-grid? How does the element behave?
Answer
The outer type is inline and the inner type is grid, so the two-value form is display: inline grid. The element sits inside a line of text like a word, while laying out its own children as a grid.
An inline <a> has padding: 20px. Its background grows in every direction, but the text lines above and below don't move apart. Why?
Answer
Padding on an inline non-replaced box is drawn but doesn't affect line height. The line boxes are sized from line-height, so the padded background overlaps neighboring lines. Use display: inline-block if the padding should take up space.
Why does <img width="300"> accept a width while <span style="width: 300px"> doesn't, even though both are inline?
Answer
An image is a replaced element, whose content comes from outside CSS and has an intrinsic size, so width and height apply to it even when it's inline. A span is a non-replaced inline element, whose size comes from its text, so width doesn't apply.
In this markup, how many boxes are generated for the <div>'s contents, and which are anonymous?
<div>Today only: <p>20% off all poetry.</p></div>
Answer
Two block boxes inside the div. The text "Today only:" sits next to a block, the <p>, so the browser wraps it in an anonymous block box. The <p> generates its own block box, which contains a line with the text. The anonymous box can't be selected and inherits from the div.
Previous: Using Browser DevTools for CSS: Inspecting, Computed Styles and Live Editing
Next: Progressive Enhancement: Fallbacks, @supports and Writing CSS That Degrades Gracefully

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.