CSS/layout
Display & Layout
The display property determines how an element participates in the page's layout — whether it takes a full line, sits inline, or becomes a flex/grid container. It's the foundati...
3 min readcss#css#display#layout
Introduction
The display property determines how an element participates in the page's layout — whether it takes a full line, sits inline, or becomes a flex/grid container. It's the foundation before diving into Flexbox or Grid.
Subtopics
display: block,inline,inline-blockdisplay: nonevsvisibility: hiddendisplay: flex/grid(overview; detailed in their own chapters)- Normal document flow
overflowand its values
Syntax
css
.block-el { display: block; }
.inline-el { display: inline; }
.inline-block { display: inline-block; }
.hidden-none { display: none; }
.hidden-vis { visibility: hidden; }
.flex-parent { display: flex; }
.grid-parent { display: grid; }
.scrollable {
overflow: auto;
}
block— takes up the full available width, starts on a new line (e.g.,div,p).inline— takes only as much width as its content, doesn't start a new line, ignoreswidth/height(e.g.,span,a).inline-block— flows inline but respectswidth,height, and verticalmargin/padding.none— removes the element entirely from layout (no space reserved).visibility: hidden— hides the element visually but keeps its space in the layout.flex/grid— turns the element into a flex/grid container for its children (see dedicated chapters).overflow— controls what happens when content exceeds its box (visible,hidden,scroll,auto).
Important Properties
| Property | Purpose | Syntax | Example |
|---|---|---|---|
display | Layout behavior type | display: value; | display: flex; |
visibility | Show/hide, keeps space | visibility: value; | visibility: hidden; |
overflow | Handle content overflow | overflow: value; | overflow: auto; |
overflow-x/overflow-y | Axis-specific overflow | overflow-x: value; | overflow-x: scroll; |
Common Use Cases
- Beginner: switching a
<span>todisplay: blockto force it onto its own line. - Practical UI:
inline-blocknav items so links sit side by side but accept padding/width. - Real-world: a dashboard layout combining
display: nonefor a collapsed sidebar (removed from flow) versusvisibility: hiddenfor a tooltip that should still reserve space.
Common Errors
- ❌ Setting
width/heighton aninlineelement expecting it to resize → ✅ change todisplay: inline-block;since plaininlineignores box dimensions. - ❌ Using
display: nonewhen the element should just be visually hidden but still take up space → ✅ usevisibility: hidden;instead. - ❌ Forgetting
overflow: autoon a fixed-height container, causing content to spill outside visually → ✅ addoverflow: auto;oroverflow-y: scroll;. - ❌
display: block inline;(invalid combined value) → ✅ use a single valid keyword, e.g.display: inline-block;. - ❌ Expecting
display: noneelements to still be accessible/focusable → ✅ understanddisplay: noneremoves the element from both the visual layout and the accessibility tree entirely.
Common Mistakes
- Confusing
display: nonewithvisibility: hidden(different layout impact). - Applying
width/heighttoinlineelements and expecting them to work. - Forgetting that
overflow: hiddenclips content, which can unintentionally hide dropdowns/tooltips.
Quick Reference
css
.block { display: block; }
.inline { display: inline; }
.inline-block { display: inline-block; }
.hide-space { visibility: hidden; }
.hide-remove { display: none; }
.scroll-box { overflow: auto; }
LB
Author
Lakshya Bansal