CSS Display Property
Every CSS layout decision starts with display.
It controls whether an element stacks vertically, flows with text, creates a flex container or grid
container, or disappears entirely. Understanding it deeply is the key to mastering every CSS layout system.
display controls and why
it mattersblock vs inline — the two fundamental typesinline-block — best of both
worldsflex and grid — modern layout containersnone vs visibility: hiddenWhat Does display Control?
The display property defines two things for an
element: its outer display type (how it participates in the parent layout flow — block or
inline) and its inner display type (what layout algorithm it creates for its children —
normal flow, flex, or grid).
stacks on its own line in flow
children flow normally
div, p, h1–h6, section, ul…takes up full line in flow
children become flex items
flows with text on same line
inline children only
span, a, strong, em, img…takes up full line in flow
children become grid items
.element { display: block; }
.element { display: inline; }
.element { display: inline-block; }
.element { display: flex; }
.element { display: inline-flex; }
.element { display: grid; }
.element { display: inline-grid; }
.element { display: none; } /* removes from layout */
.element { display: contents; } /* element's box disappears */
.element { display: table; } /* behaves like <table> */
.element { display: list-item; } /* like <li> with marker */
display: block
Block elements start on a new line, stretch to fill the full width of
their parent, and respect all box model properties (width, height, margin, padding). The next element always starts below.
/* HTML block elements by default: */
/* div, p, h1–h6, section, article, header,
footer, nav, main, ul, ol, li, form, hr… */
/* Making an inline element block */
span { display: block; } /* span now acts like div */
a { display: block; } /* link fills full width */
button { display: block; width: 100%; } /* full-width button */
/* Block element characteristics */
div {
display: block;
width: 300px; /* ✓ respected */
height: 100px; /* ✓ respected */
margin: 16px 0; /* ✓ all 4 sides work */
padding: 12px; /* ✓ all 4 sides work */
}
display: inline
Inline elements flow with the surrounding text. They only take up as
much width as their content, do not start on a new line, and ignore width, height and top/bottom margins.
/* HTML inline elements by default: */
/* span, a, strong, em, b, i, u, code, small,
label, button, img, input, select… */
/* Inline element behaviour */
span {
display: inline;
width: 200px; /* ✗ IGNORED */
height: 50px; /* ✗ IGNORED */
margin-top: 20px; /* ✗ IGNORED (vertical margins) */
margin-left: 10px; /* ✓ horizontal margin works */
padding-left: 8px; /* ✓ horizontal padding works */
padding-top: 4px; /* ⚠ visual only, no layout effect */
}
/* Making a block element inline */
div { display: inline; } /* div now flows like span */
width and height have no
effect on inline elements. Vertical margin-top and margin-bottom are also ignored. If you need to set dimensions on an element
that stays in the text flow, use display: inline-block instead.
display: inline-block
inline-block combines both worlds: it flows
inline with text like inline elements, but respects width, height and all
margins and padding like block elements.
.badge {
display: inline-block;
padding: 2px 10px; /* ✓ works on all sides */
border-radius: 999px;
font-size: 11px;
font-weight: 700;
/* Flows inline with text but respects padding/size */
}
.nav a {
display: inline-block;
padding: 8px 16px; /* clickable area */
font-weight: 600;
}
.icon-box {
display: inline-block;
width: 48px; /* ✓ width respected */
height: 48px; /* ✓ height respected */
vertical-align:middle; /* align with surrounding text */
}
/* Note: inline-block elements have a tiny whitespace
gap between them (caused by HTML whitespace).
Use flexbox or gap:0 on parent to remove it. */
Block vs Inline vs Inline-Block — Comparison
| Property | block | inline | inline-block |
|---|---|---|---|
| Starts on new line | Yes | No | No |
| Default width | 100% of parent | Content width | Content width |
| Respects width/height | Yes | No | Yes |
| Horizontal margin/padding | Yes | Yes | Yes |
| Vertical margin (top/bottom) | Yes | No | Yes |
| Vertical padding (top/bottom) | Yes | Visual only | Yes |
| Flows with text | No | Yes | Yes |
| Typical elements | div, p, h1, section | span, a, strong, em | img, button, input |
display: flex & display: grid
flex and grid
create layout containers for their children. The container itself still behaves as a block
element in the outer flow — the magic happens inside:
/* ── Flex container ── */
.nav {
display: flex;
align-items: center;
gap: 16px;
}
.card {
display: flex;
flex-direction: column;
justify-content: space-between;
}
/* Centre content — classic flex trick */
.centered {
display: flex;
align-items: center;
justify-content: center;
}
/* ── Grid container ── */
.card-grid {
display: grid;
grid-template-columns:repeat(auto-fill, minmax(250px, 1fr));
gap: 20px;
}
.page-layout {
display: grid;
grid-template-columns:260px 1fr; /* sidebar + main */
grid-template-rows: auto 1fr auto; /* header + content + footer */
min-height: 100vh;
}
display: none — Hiding Elements
display: none completely removes an element from
the document flow. It takes up no space, is invisible, and its position is given to the elements around it.
Compare with visibility: hidden which hides visually but keeps the
space:
/* ── display: none — removes from flow completely ── */
.hidden { display: none; }
/* ── visibility: hidden — invisible, space preserved ── */
.invisible { visibility: hidden; }
/* ── opacity: 0 — invisible, space preserved, events fire ── */
.transparent { opacity: 0; }
/* ── Comparison summary ──
display:none → invisible, no space, no events
visibility:hidden→ invisible, space kept, no events
opacity:0 → invisible, space kept, events fire ── */
/* ── Common toggle pattern (with JavaScript) ── */
.menu { display: none; }
.menu.is-open { display: block; }
/* ── Responsive show/hide ── */
.mobile-only { display: none; }
@media (max-width: 640px) {
.mobile-only{ display: block; }
.desktop-only{ display: none; }
}
All display Values — Quick Reference
CSS has many display values. Here are all the ones you need to know:
display: contents — The "Vanishing Box"
display: contents makes the element's own box
disappear while its children participate in the parent's layout as if the wrapper never existed. Useful for
semantic wrappers in flex/grid layouts:
<!-- Grid parent with a semantic wrapper -->
<div class="grid-parent">
<div class="item">Item 1</div>
<div class="wrapper"> <!-- semantic grouping -->
<div class="item">Item 2</div>
<div class="item">Item 3</div>
</div>
<div class="item">Item 4</div>
</div>
/* Without display:contents — wrapper breaks the grid */
.grid-parent { display: grid; grid-template-columns: repeat(4,1fr); }
.wrapper { } /* wrapper = one grid cell, items 2&3 inside it */
/* With display:contents — wrapper disappears, 4 items in grid */
.wrapper { display: contents; }
/* Now all 4 items are direct grid children */
display: contents removes the element's box from the accessibility tree
in some browsers — interactive elements like <button> and <a> may lose their role. Use it only on non-interactive wrapper divs.
Try It Yourself
Change the display value in the editor. Notice how
switching from block to inline
removes the ability to set width, and how none removes elements from
flow entirely.
Quick Check — Chapter 20
Inline elements: flow with the surrounding text on the same line, only take as much width as their content, and ignore width, height, and vertical margins (top/bottom). Horizontal padding and margin work, but vertical padding is visual-only (doesn't push layout).
Examples:
div, p, h1 are block by default. span, a, strong are inline by
default.You can override the default:
span { display: block; } makes a span
behave like a div, and div { display: inline; } makes a div flow
like a span.
inline-block solves the limitations of both block and inline:— Like inline: stays in the text flow, does not start on a new line, multiple elements sit side by side
— Like block: respects width, height, and all four sides of margin and padding
Common uses:
— Navigation links with explicit padding (clickable area)
— Badge/tag/chip components inside text
— Button groups that flow inline
— Icon boxes with fixed dimensions in text
Note: inline-block elements have a subtle whitespace gap between them (caused by HTML newlines/spaces between tags). You can eliminate this by using flexbox on the parent, setting
font-size: 0 on the parent (and resetting on children), or keeping HTML
tags on the same line.
display: none:— Completely removes the element from the document flow
— Takes up zero space — surrounding elements move to fill the gap
— Invisible, not focusable, events do not fire
— The element is also removed from the accessibility tree
visibility: hidden:— Makes the element invisible but preserves its space in the layout
— Surrounding elements stay in position — a blank gap remains
— Not visible, not interactive, but the space is still there
A third option is
opacity: 0 — invisible and space preserved, but
events still fire (clicks work on transparent elements).Use
display: none when you want to toggle layout (menus, modals).
Use visibility: hidden when you need a placeholder space.
display: flex or display: grid changes two things:1. The element itself still behaves as a block-level element in the outer flow — it takes a full line (just like
display: block).2. Its children are now controlled by a new layout algorithm — either flex formatting context or grid formatting context. They become flex items or grid items.
This is the "outer/inner display" model.
flex is short for
"block-level container with a flex inner layout". Use inline-flex to
make the container itself flow inline while still creating a flex context for children.
display: contents makes the element's own box "disappear" — its
children participate in the parent layout context as if the element itself were not there.Practical use: you have a semantic wrapper
<div class="group"> inside a flex or grid container. Without display: contents, the wrapper becomes one flex/grid item and the items
inside it are children of the wrapper, not the container. With display: contents, the wrapper disappears from layout while keeping the
semantic grouping in HTML — its children become direct flex/grid items of the outer container.Accessibility warning: in some browsers,
display: contents on interactive elements (<button>, <a>) removes
them from the accessibility tree. Use only on non-interactive wrapper divs.
display: none?display: inline element
ignore?display: flex on an element, what
changes about its children?displaycontrols an element's outer type (block/inline in the parent flow) and inner type (how it lays out its children).block— starts on a new line, full width, respects all box model properties. Default fordiv,p,h1–h6,section.inline— flows with text, content width only, ignores width/height and vertical margins. Default forspan,a,strong.inline-block— flows inline but respects all dimensions and margins. Default forimg,button,input. Great for badges, nav links, icon boxes.flex— block-level container, one-dimensional flex layout for children.inline-flexflows inline.grid— block-level container, two-dimensional grid layout for children.inline-gridflows inline.none— removes from flow: no space, no visibility, no events.visibility: hidden— invisible but space preserved.opacity: 0— invisible, space preserved, events still fire.contents— element's box disappears; children join parent layout directly.flow-root— creates a new block formatting context (modern float-clearing).
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.