The display Property
The display property is one of the most important CSS properties—it controls how an element generates a box and participates in the layout. Different values (block, inline, inline-block, flex, grid, none) fundamentally change element behavior. Understanding display is essential to CSS layout.
Main display values
Value | Behavior | Width | Stacking | Use Case |
|---|---|---|---|---|
block | Full width box | 100% | Vertical | Paragraphs, divs, headers |
inline | Flow with text | Content | Horizontal | Links, spans, emphasis |
inline-block | Inline + box model | Can set | Horizontal | Buttons, inputs, images |
flex | Flexible layout | Flexible | Flex axis | Navigation, toolbars |
grid | Grid layout | Flexible | Grid cells | Page layouts, dashboards |
none | Hidden | N/A | None | Hidden content |
CSS
/* display: block (default for div, p, h1-h6) */
.block-element {
display: block;
width: 100%;
height: 100px;
margin: 10px;
padding: 10px;
}
/* Takes full width, stacks vertically */
/* display: inline (default for span, a, strong) */
.inline-element {
display: inline;
width: auto;
height: auto;
margin: 10px 0;
padding: 5px 10px;
}
/* Flows with text, ignores height/width */
/* display: inline-block (hybrid) */
.inline-block-element {
display: inline-block;
width: 200px;
height: 100px;
margin: 10px;
padding: 10px;
}
/* Flows inline but respects box model */
/* display: flex (layout mode) */
.flex-container {
display: flex;
gap: 20px;
align-items: center;
}
/* Children become flex items */
/* display: grid (layout mode) */
.grid-container {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
}
/* Children become grid items */
/* display: none (hidden) */
.hidden {
display: none;
}Changing element behavior with display
CSS
/* Make a span behave like a block */
span {
display: block;
width: 100%;
margin: 10px 0;
}
/* Now span stacks vertically and has width */
/* Make a div flow inline with text */
div {
display: inline;
margin: 0;
}
/* Now div flows horizontally like text */
/* Make a button behave like an inline element (unusual) */
button {
display: inline;
}
/* Common: make list items display inline for navigation */
ul {
list-style: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
/* Navigation: ul becomes flex container */
nav ul {
display: flex;
gap: 20px;
list-style: none;
padding: 0;
}
nav li {
display: block;
}
/* Gallery: items become inline-block for wrapping */
.gallery {
font-size: 0;
}
.gallery-item {
display: inline-block;
width: 200px;
margin: 10px;
font-size: 16px;
}Special display values
CSS
/* display: none vs visibility: hidden */
/* display: none: element removed from layout entirely */
.hidden-none {
display: none;
}
/* visibility: hidden: element invisible but takes space */
.hidden-visibility {
visibility: hidden;
}
/* display: contents (newer, powerful) */
.container {
display: contents;
}
/* display: flow-root (new) */
.bfc {
display: flow-root;
}
/* display: list-item (keeps list behavior) */
li {
display: list-item;
}
/* display: table variants (less common) */
table {
display: table;
}
tr {
display: table-row;
}
td {
display: table-cell;
}display with layout properties
CSS
/* display: flex opens up new properties */
.flex-container {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
gap: 20px;
}
/* display: grid opens up grid properties */
.grid-container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-gap: 20px;
}
/* display: block with positioning */
.positioned {
display: block;
position: absolute;
top: 0;
left: 0;
}
/* display: none ignores everything else */
.hidden {
display: none;
width: 100px;
height: 100px;
flex: 1;
position: absolute;
}
/* display: inline-block with position */
.popup {
display: inline-block;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}Best practices for display
CSS
/* 1. Use semantic HTML first */
/* Default display is semantic */
/* 2. Modern layouts: flex or grid, not inline-block */
/* Old way: inline-block for navigation */
li {
display: inline-block;
margin: 0 10px;
}
/* Modern way: flex */
ul {
display: flex;
gap: 10px;
list-style: none;
padding: 0;
}
/* 3. Use display: none for hidden content, not visibility: hidden */
.hidden {
display: none;
}
/* 4. Don't fight natural display behavior */
/* Good: work with defaults */
button {
padding: 10px 20px;
}
/* 5. Use display: contents sparingly */
.wrapper {
display: contents;
}
/* 6. Don't mix conflicting display values */
/* Bad: flex + inline-block fighting */
.flex-container {
display: flex;
}
.child {
display: inline-block;
}
/* Good: let flex parent handle layout */
.flex-container {
display: flex;
gap: 20px;
}
.child {
flex: 1;
}Note
The `display` property fundamentally controls how elements layout. Common values: `block` (full width, stacks), `inline` (flows with text, ignores sizing), `inline-block` (inline flow + box model), `flex` (flexible layout), `grid` (2D layout), `none` (hidden). Modern CSS favors flex and grid over inline-block. Use semantic HTML and default display behavior when possible.
Next
Flexbox layout: [Flexbox Introduction](/css/flexbox-intro).