The CSS Display Property & Formatting Contexts
The display property is the most fundamental layout control in CSS. It determines how an element behaves in the normal document flow, how it interacts with sibling elements, and how its direct children are laid out internally.
In this lesson, we explore the primary display values: block, inline, inline-block, none, flex, grid, contents, and compare display: none with visibility: hidden.
┌────────────────────────────────────────────────────────────┐
│ Comparison of Core Display Values │
├──────────────┬──────────────┬──────────────┬───────────────┤
│ Display Value│ Starts Line? │ Width/Height │ Margin/Pad │
├──────────────┼──────────────┼──────────────┼───────────────┤
│ `block` │ Yes (New Ln) │ Fully obeyed │ Fully obeyed │
│ `inline` │ No (Flows) │ Ignored! │ Horiz. only │
│ `inline-block`│ No (Flows) │ Fully obeyed │ Fully obeyed │
│ `none` │ Removed DOM │ N/A │ N/A │
│ `contents` │ Container gone│ Children flow│ Ignored │
└──────────────┴─────────────────────────────┴───────────────┘
1. block vs inline vs inline-block
display: block: The element starts on a new line and expands horizontally to fill 100% of its parent container.width,height, and all four margins/paddings are fully respected.display: inline: The element flows horizontally with surrounding text without breaking onto a new line. Crucial Rule:width,height, and verticalmargin-top/margin-bottomare completely ignored on inline elements!display: inline-block: The best of both worlds. The element flows horizontally inline like text, but fully obeys declaredwidth,height, and vertical padding/margin. Perfect for interactive button links and pill badges:
/* Button link styled as inline-block */
a.btn {
display: inline-block;
padding: 0.75rem 1.5rem;
margin-top: 1rem; /* Obeyed because display is inline-block! */
background-color: #2563eb;
color: white;
text-decoration: none;
border-radius: 0.375rem;
}
2. Formatting Context Engines: flex & grid
display: flex: Establishes a 1-dimensional Flex formatting context for laying out child elements along a row or column axis.display: grid: Establishes a 2-dimensional Grid formatting context for aligning elements across simultaneous rows and columns.
3. The contents Value
display: contents removes the parent element's visual box from the layout tree completely, promoting its direct child elements to participate directly in the outer formatting context:
<div class="grid-parent" style="display: grid; grid-template-columns: 1fr 1fr;">
<!-- This wrapper div disappears visually, so Child 1 & Child 2 become direct grid items! -->
<div class="wrapper" style="display: contents;">
<div class="child-1">Child 1</div>
<div class="child-2">Child 2</div>
</div>
</div>
4. Hiding Elements: display: none vs visibility: hidden vs opacity: 0
display: none: The element is completely removed from the Render Tree. It occupies zero physical space, and screen readers ignore it entirely.visibility: hidden: The element is invisible, but remains in the Render Tree, preserving its physical width, height, and layout box on screen.opacity: 0: The element is transparent, retains its layout box, and remains interactive to mouse clicks and keyboard focus.
/* Accessible visually-hidden utility for screen reader only text */
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border-width: 0;
}
Summary
display: blockcreates full-width line-breaking boxes.display: inlinewraps text and ignores declaredwidth/heightand vertical margins.display: inline-blockflows inline while respecting full box-model sizing and padding.display: contentsmakes the container box transparent to layout calculations.display: noneremoves elements from the Render Tree;visibility: hiddenretains spatial geometry.
Best Practices
- Use
inline-blockorinline-flexfor Interactive Badges: Ensure vertical padding is respected. - Never Use
display: nonefor Content Screen Readers Should Hear: Use.sr-onlyclipping instead. - Use
display: contentsto Avoid Extra DOM Wrapper Layout Issues: Perfect for framework wrapper components. - Use
display: flexfor 1D Toolbars anddisplay: gridfor 2D Layouts: Match the formatting model to the use case.