Why does a <div> always start on a new line while a <span> sits inside a sentence? Why does width: 300px do nothing on a link? Why is there a mysterious gap under every image? All three questions have the same answer: the display property.
display is the starting point of all CSS layout. It decides what kind of box an element generates and how that box behaves among its neighbours. In this chapter we'll study block, inline, inline-block and none in depth, compare ways of hiding elements, and get a first look at flex, grid, flow-root and contents. You should be comfortable with The Box Model and Margin & Padding before reading this.
What Does display Do?
The
displayproperty defines the type of box an element generates. It has two parts: the outer display type (how the box takes part in its parent's layout — block or inline) and the inner display type (how the box lays out its own children — normal flow, flex, grid, table ...).
display: inline-flex
/ \
outer: inline inner: flex
"I sit in a line of text "my children are laid
like a word" out with flexbox"Every element has a default display value that comes from the browser's built-in stylesheet. We checked these with getComputedStyle() in Chrome:
| Default display | Elements |
|---|---|
block | div, p, h1–h6, ul, ol, section, article, header, footer, main, nav, form, details |
inline | span, a, strong, em, code, label, img, video, iframe, canvas |
inline-block | button, input, select, textarea |
list-item | li, summary (the clickable heading of details) |
table, table-row, table-cell | table, tr, td/th |
none | head, script, style, template, a closed dialog |
An important point: display changes only the look, not the meaning. A <span> with display: block is still a span to screen readers and search engines, and a link with display: block is still a link. Choose HTML elements for meaning, then use display for layout.
display: block
Block boxes stack vertically, like paragraphs in a document.
Starts on a new line, and the next element also starts on a new line.
By default, stretches to fill the full available width of its parent (its width is
auto, not 100% — so padding and border fit inside without overflowing).width,height,marginandpaddingall work on all sides.Vertical margins between adjacent blocks can collapse (from Margin & Padding).
margin-inline: autocentres a block that has a set width.
.card {
display: block; /* already the default for div */
width: 300px;
margin: 20px auto; /* centred, 20px above and below */
padding: 16px;
}A common use is making a whole link clickable as a large area:
.menu a {
display: block; /* the entire row is clickable, not just the words */
padding: 12px 16px;
}display: inline
Inline boxes flow along a line of text, like words in a sentence. When the line is full, they wrap onto the next line — an inline box can even be split across two lines.
Does not start a new line.
Takes only as much width as its content.
widthandheightare ignored.Horizontal
margin,paddingandborderwork and push neighbouring text away.Vertical
marginis ignored. Verticalpaddingandborderare painted, but do not push lines apart, so they can overlap the lines above and below.
Let's prove it. This span has a size, margins and padding:
span.tag {
width: 300px;
height: 100px;
margin: 20px;
padding: 10px;
border: 2px solid #3366cc;
background: #ccddee;
}<p>Text before <span class="tag">inline span</span> text after ...</p>Measured in Chrome (Arial 16px):
Rendered box: 100.5px × 41px
width = text width (~76.5) + padding 10+10 + border 2+2 ← width: 300px ignored
height = line of text (~17) + padding 10+10 + border 2+2 ← height: 100px ignored
Horizontal margins of 20px: visible gaps on both sides
Vertical margins of 20px: no effect; the line spacing is unchangedThe blue background box extends above and below the line, but the paragraph's lines don't move to make room for it. That's why "my padding on a link overlaps the text above" is such a common surprise.
display: inline-block
inline-block is a hybrid: outside it behaves like inline (sits on the line next to other content), but inside it behaves like a block (it respects width, height, and all margins and padding).
.badge {
display: inline-block;
width: 100px;
padding: 6px 0;
margin: 4px;
text-align: center;
background: #ffcc99;
}Now the width works, vertical padding and margin push other lines away properly, and the badges still sit side by side. Buttons and form inputs are inline-block by default for exactly this reason: they must fit into a line of text but need a real size.
Comparison
| Behaviour | block | inline | inline-block |
|---|---|---|---|
| Starts on a new line | Yes | No | No |
| Default width | Fills the parent | Fits content | Fits content |
width / height | Respected | Ignored | Respected |
| Vertical margin | Respected (can collapse) | Ignored | Respected (never collapses) |
| Vertical padding/border | Pushes content | Painted, but doesn't affect line spacing | Pushes content |
| Can break across lines | No | Yes | No (moves to the next line as a whole) |
| Typical elements | div, p, h1 | span, a, em | button, input |
The gap between inline-block elements
Put three inline-block boxes next to each other in HTML:
<div class="ib">A</div> <div class="ib">B</div><div class="ib">C</div>In Chrome, there was a 4.45px gap between A and B and 0px between B and C. Why? Inline-block boxes are treated like words, and the space (or newline) in the HTML between A and B is rendered as a normal space character. In 16px Arial a space is about 4.45px wide. Between B and C there was no whitespace in the source, so no gap.
This gap breaks layouts like "four boxes of exactly 25% each" (they no longer fit on one line). Old fixes included removing whitespace or setting font-size: 0 on the parent. The modern fix is simply to use flexbox or grid for rows of boxes, which ignore that whitespace.
The gap under images
Images are inline by default, and inline content sits on the text baseline. Below the baseline the line keeps room for descenders (the tails of letters like g, p, y). So a 50px-tall image inside a div made the div 54px tall in our test (Arial 16px), with 4px of empty space under the image.
/* Fix 1: make the image a block (most common) */
img {
display: block;
}
/* Fix 2: keep it inline but align it differently */
img {
vertical-align: middle; /* or top / bottom */
}Many CSS resets include img { display: block; max-width: 100%; } for this reason.
display: none — and Other Ways to Hide
display: none removes the element completely: it generates no box, takes no space, can't be clicked or focused, and is removed from the accessibility tree (screen readers ignore it). Its children are hidden too, and no child can override that.
.modal { display: none; }
.modal.open { display: block; }There are three popular ways to hide something, and they behave differently:
display: none | visibility: hidden | opacity: 0 | |
|---|---|---|---|
| Takes up space | No | Yes (an empty gap) | Yes |
| Can be clicked / focused | No | No | Yes (invisible but still there!) |
| Read by screen readers | No | No | Yes |
| Children can show themselves | No | Yes, with visibility: visible | No |
| Can be animated smoothly | Not directly | Switches at a step | Yes (fades) |
So: use display: none to remove something entirely, visibility: hidden to hide it while keeping its space reserved, and opacity for fade effects (combined with something that makes it non-interactive when fully hidden).
Sometimes you want text to be hidden visually but still read by screen readers (for example a label for an icon-only button). None of the three above does that; the usual solution is a "visually hidden" utility class that shrinks the element to 1px and clips it.
The hidden attribute trap
HTML has a hidden attribute, which works because the browser stylesheet contains [hidden] { display: none; }. But browser styles have the lowest priority in the cascade, so any author rule that sets display wins:
.toolbar { display: flex; }<div class="toolbar" hidden>You should not see me</div>In Chrome, this element was visible: its computed display was flex. A common safety rule in CSS resets fixes it:
[hidden] {
display: none !important;
}display: flex and display: grid (a Preview)
Block and inline layout (together called normal flow) was designed for documents. For application-style layouts — navbars, card rows, page grids — CSS has two dedicated layout systems, switched on with display:
.navbar { display: flex; } /* children laid out in a row (or column) */
.gallery { display: grid; } /* children placed into rows AND columns */The element itself stays a block on the outside; only the layout of its children changes. Their inline cousins inline-flex and inline-grid do the same but make the container sit inside a line like an inline-block. These are big topics with their own chapters: Flexbox and CSS Grid.
The Two-Value Syntax
Modern CSS lets you write the outer and inner types explicitly. The familiar one-word values are just short forms:
| Short (classic) | Two-value equivalent |
|---|---|
block | block flow |
inline | inline flow |
inline-block | inline flow-root |
flex | block flex |
inline-flex | inline flex |
grid | block grid |
inline-grid | inline grid |
flow-root | block flow-root |
list-item | block flow list-item |
This table explains the whole model: inline-block really means "inline on the outside, a new independent block layout on the inside". The two-value syntax is Baseline since July 2023 (Chrome 115, Firefox 70, Safari 15), and browsers report the short form when you read the computed value — writing display: inline flex gives a computed value of inline-flex. You will still mostly see the classic one-word values in real code, and they are perfectly fine to use.
display: flow-root
flow-root creates a normal block box that starts a new block formatting context (BFC) — an independent layout area. Two practical effects:
The child margins don't collapse through it with the parent's margins (the "margin escaping the parent" problem from Margin & Padding).
It grows to contain floated children instead of letting them hang out of the bottom (you'll meet floats in Float & Clear).
.container {
display: flow-root; /* contain floats, stop margin collapsing with children */
}Before flow-root existed, developers used hacks like overflow: hidden or a "clearfix" for the same effect. flow-root has been supported in all major browsers since 2020.
display: contents
display: contents makes the element's own box disappear, while its children are displayed as if they were direct children of the element's parent. Its background, border and padding are not drawn at all.
.row { display: flex; gap: 8px; } /* children side by side */
.group { display: contents; } /* the wrapper "vanishes" */<div class="row">
<div>One</div>
<div class="group">
<div>Two</div>
<div>Three</div>
</div>
</div>Without display: contents the flex container has only two items — One, and the group (with Two and Three stacked inside it). With it, One, Two and Three all become flex items and sit side by side in one row. This is useful when a wrapper element gets in the way of a flex or grid layout. Be careful: browsers historically had accessibility bugs with display: contents on elements like buttons, lists and tables (their semantics could be lost), and its web-features status is still listed as "limited" because of these interoperability issues. Use it on generic wrappers such as div, and test with a screen reader.
Other display Values
| Value | What it does |
|---|---|
list-item | A block that also gets a marker (bullet/number) through the ::marker pseudo-element, like <li> |
table, table-row, table-cell, ... | Make any elements behave like table parts. Was used for vertical centring before flexbox; rarely needed now |
inline-table | A table that sits inline |
ruby | Annotation layout for East Asian text (used by <ruby>) |
Complete Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>The Display Property</title>
<style>
body {
font-family: system-ui, sans-serif;
padding: 20px;
}
.menu a {
display: block;
padding: 10px 14px;
border-bottom: 1px solid #e5e7eb;
color: #111827;
text-decoration: none;
}
.menu a:hover {
background: #f3f4f6;
}
.tag {
display: inline-block;
padding: 4px 10px;
margin: 4px 2px;
border-radius: 9999px;
background: #dbeafe;
font-size: 0.875rem;
}
.note {
display: none;
}
.menu:hover + .note {
display: block;
}
img {
display: block;
max-width: 100%;
}
</style>
</head>
<body>
<nav class="menu">
<a href="#">Operating Systems</a>
<a href="#">Computer Networks</a>
<a href="#">DBMS</a>
</nav>
<p class="note">Pick a subject to start revising.</p>
<p>
Topics:
<span class="tag">Paging</span>
<span class="tag">TCP</span>
<span class="tag">Normalization</span>
</p>
</body>
</html>Result: three full-width menu rows separated by thin lines, each row clickable across its entire width (not just on the words) and turning light grey on hover. Below them, a line reading "Topics:" followed by three rounded light-blue pill tags sitting side by side in the line, with real padding. The note paragraph is hidden and takes no space until you hover over the menu (using the + combinator from Combinators & Attribute Selectors).
Common Mistakes
Setting width/height on an inline element (
span,a) and wondering why nothing happens. Useinline-blockorblock.Expecting vertical margin on an inline element to create space.
Using
opacity: 0to "hide" a button: it is still clickable and focusable.Overriding the
hiddenattribute by settingdisplayon the same element.Building rows of boxes with
inline-blockand fighting whitespace gaps. Use flexbox or grid.Forgetting the image baseline gap: set
img { display: block; }.Changing
displayto change meaning: adivstyled to look like a button is still not a button (no keyboard support). Use the right HTML element.
Interview / Exam Questions
1. What is the difference between inline, inline-block and block?
Block elements start on a new line, fill the available width and respect width, height and all margins/padding. Inline elements flow within text, size to their content and ignore width, height and vertical margins (vertical padding is painted but doesn't affect line spacing). Inline-block elements flow within the line like inline elements but respect width, height, margins and padding like blocks.
2. Difference between display: none and visibility: hidden?
display: none removes the element from layout entirely — it takes no space. visibility: hidden makes it invisible but its space is kept. Neither is clickable nor read by screen readers; a child of a visibility: hidden parent can be made visible again with visibility: visible.
3. Why doesn't width: 200px work on a span?
A span is display: inline by default, and width/height don't apply to non-replaced inline boxes. Change it to inline-block or block.
4. Why is there a small gap between inline-block elements?
The whitespace (spaces/newlines) between the elements in the HTML is rendered as a space character, because inline-block elements are laid out like words. Its width depends on the font (about 0.25em). Using flexbox or grid avoids it.
5. Why is there extra space below an image inside a div?
Images are inline and sit on the text baseline; space is reserved below the baseline for letter descenders. Fix with display: block or vertical-align: middle/top/bottom on the image.
6. What does inline-block mean in the two-value syntax?
inline flow-root: the outer display type is inline, and the inner display type is flow-root, i.e. it establishes a new block formatting context for its contents.
7. Which of display: none, visibility: hidden and opacity: 0 keeps the element clickable?
Only opacity: 0. The element is fully transparent but still occupies space, receives clicks and focus, and is read by screen readers.
Summary
| Value | Key behaviour |
|---|---|
block | New line, full width, all box properties work |
inline | Flows in text; no width/height; no vertical margin |
inline-block | Flows in text but has a real size |
none | Removed from layout and accessibility tree |
flex / grid | Block outside, flex/grid layout for the children |
flow-root | Block that starts a new formatting context |
contents | Element's box disappears; children take its place |
display= outer type (block/inline) + inner type (flow, flex, grid ...).It changes appearance, never HTML meaning.
Choose the right hiding technique:
display: none,visibility: hiddenoropacity: 0.
Normal flow places boxes one after another. In the next chapter, Positioning, we'll learn how to take boxes out of that flow and place them exactly where we want.