By the end of this lesson, you will be able to choose CSS display values to control whether elements stack, sit side by side, form flexible rows, create grid layouts, or disappear from layout.
What it is
display tells the browser what kind of box an element should generate and how its children should be laid out. Think of it as the element's layout mode. A block element takes full width and starts on a new line. An inline element flows inside text and only takes the width of its content. flex and grid turn an element into a layout container, making its direct children flex items or grid items. none removes the element from the layout entirely.
Related terms include box model, flex container, grid container, inline-level, block-level, and accessibility tree.
Why it matters
- It controls whether components stack vertically or sit side by side.
- It lets you build responsive rows and columns without floats or tables.
- It determines whether width, height, margins, and padding behave predictably.
- It can hide content from layout and screen readers when needed.
- It provides the foundation for alignment tools like
gap,justify-content, andalign-items.
Syntax or steps
- Select the element that should control layout.
- Set
displaytoblock,inline,flex,grid, ornone. - If using
flexorgrid, style the direct children as items. - Add layout properties such as
gap,flex-direction, orgrid-template-columns.
Example
<div class="row">
<div class="card">One</div>
<div class="card">Two</div>
<div class="card">Three</div>
</div>
<div class="hidden">This is removed from layout.</div>
<style>
.row {
display: flex;
gap: 16px;
}
.card {
flex: 1;
padding: 12px;
border: 1px solid #ccc;
}
.hidden {
display: none;
}
</style>
The .row element becomes a flex container. Its three .card children become flex items and sit side by side. gap: 16px adds space between them. flex: 1 lets each card grow to share available width. The .hidden element is not rendered, so it takes no space and is not announced by assistive technology.
Common mistakes
- Setting width or height on inline elements. Inline elements ignore most box sizing. Use
display: inline-blockordisplay: blockif you need a sized inline-like element. - Applying flex properties to the wrong element.
display: flexgoes on the parent;flex: 1goes on the child. - Using
display: nonewhen content should remain accessible. If you only want to hide something visually, use a screen-reader-only technique instead. - Expecting transitions to animate
display.displayis not smoothly transitioned; animateopacityorvisibilityinstead.
When to use it
Use flex for one-dimensional rows or columns, and grid for two-dimensional layouts. Use none only when the element should be completely absent.
| Goal | Use | Why |
|---|---|---|
| Stack sections | display: block | Default for many elements; full width and line breaks. |
| Inline text emphasis | display: inline | Flows with text and does not break lines. |
| Card row | display: flex | Easy alignment and spacing in one direction. |
| Page layout | display: grid | Controls rows and columns together. |
| Hide from layout | display: none | Removes element and its space. |
Practice
Guided exercise: Create three div elements inside a parent. Set the parent to display: flex and add gap: 12px. Expected output: three boxes appear side by side with equal spacing.
Challenge: Make the same row wrap on small screens. Hint: add flex-wrap: wrap to the parent and give each child a minimum width such as min-width: 150px.
Quick check
Question: What is the difference between display: none and visibility: hidden?
Answer: display: none removes the element from layout and accessibility tree, while visibility: hidden keeps its space and may still affect layout.
Summary
display is the switch that decides how an element participates in layout. Choose block or inline for basic flow, flex or grid for structured containers, and none when content should disappear.