display PropertyThe display property in CSS controls how an element is rendered on the page. It decides whether an element behaves like a block, inline item, flexible container, grid container, and more.
It is one of the most important layout properties and directly affects how elements line up, wrap, and take up space in a layout.
<div>).<span>).Basic syntax of the display property:
/* Basic syntax of the display property */
selector {
display: value;
}
block – element acts as a block-level box and takes full width.inline – element flows with text and does not force a line break.inline-block – inline element that can have width and height.flex – element becomes a flex container, and its children become flex items.
/* Comparing different display values */
.block-box {
display: block;
background: lightblue;
padding: 8px;
margin-bottom: 4px;
}
.inline-box {
display: inline;
background: lightgreen;
padding: 4px;
}
.inline-block-box {
display: inline-block;
background: lightcoral;
padding: 8px;
margin: 4px;
width: 120px;
text-align: center;
}
<!-- Example HTML using the classes above -->
<div class="block-box">I am block</div>
Some text
<span class="inline-box">inline 1</span>
<span class="inline-box">inline 2</span>
continues on the same line...
<div class="inline-block-box">inline-block A</div>
<div class="inline-block-box">inline-block B</div>
display: flex
/* Parent uses flex, children become flex items */
.flex-container {
display: flex;
gap: 8px;
justify-content: center;
align-items: center;
}
.flex-item {
padding: 10px 16px;
background: steelblue;
color: white;
border-radius: 6px;
}
<!-- Flexbox HTML structure -->
<div class="flex-container">
<div class="flex-item">Item 1</div>
<div class="flex-item">Item 2</div>
<div class="flex-item">Item 3</div>
</div>
Here is a tiny inline example to visualize block vs inline-block vs flex using inline styles:
Block: each box goes on a new line and stretches horizontally.
Inline-block: boxes sit on the same line if there is enough space and can have width/height.
Flex: the parent decides how to distribute space between items (e.g., centered row of items).
Remember: the display value is always applied to the element itself, and it affects how that element and its children participate in the layout.
display: flex; when you need to align items horizontally or vertically in a responsive way.display: inline; for small pieces of content that should flow with text (like icons or badges).display: inline-block; when you want inline items but need to control their width/height.display: flex; applied.<div> is block, <span> is inline) before overriding.<div> elements and style them as block, inline, and inline-block. Observe how they behave on the page.display: inline-block; for menu items.justify-content and align-items values.display value.