CSS display Property
The display property determines how an element generates boxes and participates in the layout of a web page.
It controls whether an element behaves as a block or inline box, establishes a flex or grid formatting context, participates in table layout, or generates no box at all.
The property supports traditional single-keyword values such as block, inline, flex, and grid, along with modern multi-keyword values that separately describe an element's outer and inner display behavior.
display Syntax
The display property accepts keywords that define an element's layout behavior.
Basic Syntax
selector {
display: block;
}
Common Values
.element {
display: block;
}
.element {
display: inline;
}
.element {
display: inline-block;
}
.element {
display: flex;
}
.element {
display: grid;
}
.element {
display: none;
}
Multi-Keyword Syntax
.element {
display: block flow;
}
.element {
display: inline flow;
}
.element {
display: block flex;
}
.element {
display: inline grid;
}
Internal Display Values
.element {
display: table-cell;
}
.element {
display: table-row;
}
CSS-Wide Keywords
.element {
display: inherit;
}
.element {
display: initial;
}
.element {
display: unset;
}
.element {
display: revert;
}
.element {
display: revert-layer;
}
The initial value is inline. However, browser stylesheets assign different default display values to HTML elements, such as block for paragraphs and list-item for list items.
Property Values
The display property supports several categories of values.
Common Display Values
| Value | Description |
|---|---|
block |
Creates a block-level box that normally begins on a new line. |
inline |
Creates an inline-level box that participates in a line of text. |
inline-block |
Creates an inline-level box that can be sized like a block container. |
flex |
Creates a block-level flex container. |
inline-flex |
Creates an inline-level flex container. |
grid |
Creates a block-level grid container. |
inline-grid |
Creates an inline-level grid container. |
flow-root |
Creates a block-level box that establishes a new block formatting context. |
contents |
Removes the element's own principal box while generally allowing its children to participate in layout. |
none |
Prevents the element and its descendants from generating boxes in the normal rendering tree. |
list-item |
Creates a list-item box, including a marker box when applicable. |
table |
Creates a block-level table formatting context. |
inline-table |
Creates an inline-level table formatting context. |
ruby |
Creates a ruby formatting context for annotation layout. |
Additional Display Values
| Value | Description |
|---|---|
table-row |
Creates an internal table row box. |
table-cell |
Creates an internal table cell box. |
table-row-group |
Creates a table row group. |
table-header-group |
Creates a table header group. |
table-footer-group |
Creates a table footer group. |
table-column |
Creates an internal table column box. |
table-column-group |
Creates an internal table column group. |
table-caption |
Creates a table caption box. |
ruby-base |
Creates a ruby base box. |
ruby-text |
Creates a ruby annotation box. |
ruby-base-container |
Creates a ruby base container box. |
ruby-text-container |
Creates a ruby annotation container box. |
CSS-Wide Values
| Value | Description |
|---|---|
inherit |
Uses the computed display value of the parent element. |
initial |
Restores the initial value, inline. |
unset |
Uses the initial value because display is not inherited. |
revert |
Reverts to the value established by an earlier cascade origin. |
revert-layer |
Reverts to the value established by an earlier cascade layer. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | inline |
| Inherited | No |
| Applies to | All elements |
| Computed value | As specified, with certain display values adjusted by layout rules |
| Animation type | Discrete |
| Percentages | Not applicable |
Some display values are automatically transformed by layout rules. For example, an element that becomes a flex item is blockified, meaning its outer display behavior becomes block-level.
How It Works
Every rendered element participates in a formatting context. The display property helps determine how its boxes are generated and how its contents are arranged.
Step 1: Create Elements
<div class="container">
<span>Item One</span>
<span>Item Two</span>
<span>Item Three</span>
</div>
Step 2: Choose a Layout
.container {
display: flex;
}
Step 3: Arrange the Children
The container establishes a flex formatting context. Its direct in-flow children become flex items and are arranged according to the flex layout rules.
Change the Layout
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
Changing the container to grid establishes a grid formatting context instead.
Important Distinction
The display property determines the type of layout context. Properties such as flex-direction, grid-template-columns, and gap control how items are arranged within that context.
Outer and Inner Display
Modern CSS distinguishes between an element's outer display type and its inner display type.
Outer Display
The outer display type describes how the element participates in its parent's formatting context.
Inner Display
The inner display type determines how the element's own contents are laid out.
Multi-Keyword Examples
.block-flow {
display: block flow;
}
.inline-flow {
display: inline flow;
}
.block-flex {
display: block flex;
}
.inline-flex-container {
display: inline flex;
}
.block-grid {
display: block grid;
}
.inline-grid-container {
display: inline grid;
}
Legacy and Modern Equivalents
| Single Keyword | Multi-Keyword Equivalent |
|---|---|
block |
block flow |
inline |
inline flow |
inline-block |
inline flow-root |
flex |
block flex |
inline-flex |
inline flex |
grid |
block grid |
inline-grid |
inline grid |
flow-root |
block flow-root |
Recommended Approach
The familiar single-keyword values remain valid and widely supported. Multi-keyword syntax makes the outer and inner display behaviors more explicit.
display: block
The block value creates a block-level box that normally begins on a new line and participates in block layout.
Basic Example
.item {
display: block;
}
HTML Example
<span class="block-item">First item</span>
<span class="block-item">Second item</span>
.block-item {
display: block;
padding: 10px;
background-color: #e8f1f8;
}
Block-Level Behavior
In normal block layout, block boxes are generally stacked vertically. Their width and height can be controlled using sizing properties.
Common Elements
Elements such as <div>, <p>, and headings normally have block-level display behavior through browser default styles.
Important Consideration
Block-level behavior does not guarantee a full-width box in every formatting context. The final size depends on available space, sizing properties, and layout rules.
display: inline
The inline value creates an inline-level box that participates in a line of text rather than starting a new block.
Basic Example
.item {
display: inline;
}
HTML Example
<p>
Learn <span class="highlight">HTML</span> and CSS.
</p>
.highlight {
display: inline;
background-color: #fff2b3;
}
Inline Behavior
Inline boxes flow alongside text and can wrap across multiple lines.
Sizing Limitations
For ordinary non-replaced inline elements, width and height do not control the box in the same way they do for block or inline-block elements.
Common Elements
Elements such as <span>, <strong>, and <em> normally display inline.
Important Consideration
Use inline when content should remain part of a line of text. Use inline-block when the element needs explicit width and height while remaining inline-level.
display: inline-block
The inline-block value creates an inline-level box that supports block-like sizing and padding.
Basic Example
.item {
display: inline-block;
}
Sized Inline Elements
.badge {
display: inline-block;
min-width: 80px;
padding: 8px 12px;
background-color: #336699;
color: #ffffff;
text-align: center;
}
HTML Example
<span class="badge">HTML</span>
<span class="badge">CSS</span>
Inline-Block Behavior
Inline-block boxes participate in inline layout but establish an independent formatting context for their contents.
Whitespace Between Items
Whitespace in the HTML source between inline-block elements may produce visible gaps.
Practical Uses
Inline-block is useful for badges, labels, small controls, and other elements that need sizing without occupying an entire line.
display: flex
The flex value creates a block-level flex container that arranges its direct in-flow children as flex items.
Basic Example
.flex-container {
display: flex;
}
HTML Example
<div class="flex-container">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
Flexbox with Spacing
.flex-container {
display: flex;
gap: 15px;
}
Vertical Flex Layout
.flex-container {
display: flex;
flex-direction: column;
gap: 10px;
}
Centered Flex Items
.flex-container {
display: flex;
justify-content: center;
align-items: center;
}
Important Consideration
Flexbox is designed primarily for one-dimensional layouts, arranging items along a main axis with alignment along a cross axis.
display: inline-flex
The inline-flex value creates an inline-level flex container.
Basic Example
.inline-flex-container {
display: inline-flex;
}
Inline Flex Group
<p>
Choose:
<span class="inline-flex-container">
<span>HTML</span>
<span>CSS</span>
</span>
</p>
.inline-flex-container {
display: inline-flex;
gap: 8px;
padding: 5px;
background-color: #e8f1f8;
}
Comparison with flex
| Value | Outer Display | Inner Display |
|---|---|---|
flex |
Block-level | Flex |
inline-flex |
Inline-level | Flex |
Practical Uses
Inline-flex is useful for compact icon-and-label groups, badges, and inline controls.
display: grid
The grid value creates a block-level grid container for arranging content into rows and columns.
Basic Example
.grid-container {
display: grid;
}
Three-Column Grid
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 15px;
}
Responsive Grid
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 15px;
}
HTML Example
<div class="grid-container">
<div>Column 1</div>
<div>Column 2</div>
<div>Column 3</div>
</div>
Grid Behavior
The direct in-flow children of the grid container become grid items. Their placement can be controlled through grid tracks, lines, and areas.
Important Consideration
Grid is especially useful for two-dimensional layouts where rows and columns need coordinated sizing and alignment.
display: inline-grid
The inline-grid value creates an inline-level grid container.
Basic Example
.inline-grid-container {
display: inline-grid;
}
Compact Grid
.inline-grid-container {
display: inline-grid;
grid-template-columns: repeat(2, 40px);
gap: 5px;
}
HTML Example
<div class="inline-grid-container">
<span>A</span>
<span>B</span>
<span>C</span>
<span>D</span>
</div>
Comparison with grid
| Value | Outer Display | Inner Display |
|---|---|---|
grid |
Block-level | Grid |
inline-grid |
Inline-level | Grid |
Practical Uses
Inline-grid works well for compact structured groups that need grid alignment while remaining inline-level.
display: flow-root
The flow-root value creates a block-level box that establishes a new block formatting context.
Basic Example
.container {
display: flow-root;
}
Containing Floats
<div class="article-box">
<img src="example.jpg" alt="Example graphic">
<p>Text wraps around the floated image.</p>
</div>
.article-box {
display: flow-root;
border: 1px solid #cccccc;
padding: 15px;
}
.article-box img {
float: left;
width: 100px;
margin-right: 15px;
}
Why It Works
A new block formatting context contains its internal floats when calculating the container's height.
Margin Behavior
A flow-root container also prevents its children's vertical margins from collapsing through the container.
Practical Uses
Flow-root is useful for containing floats and isolating block formatting behavior without using overflow clipping as a workaround.
display: contents
The contents value removes an element's own principal box from layout while generally allowing its children to participate as though the wrapper's box were absent.
Basic Example
.wrapper {
display: contents;
}
HTML Example
<div class="grid-container">
<div class="wrapper">
<div>Item 1</div>
<div>Item 2</div>
</div>
<div>Item 3</div>
</div>
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
.wrapper {
display: contents;
}
How It Works
The wrapper's own principal box is not generated, allowing its child boxes to participate directly in the surrounding grid layout.
Backgrounds and Borders
Because the wrapper does not generate its own principal box, backgrounds, borders, and padding applied to that box are not displayed in the usual way.
Accessibility Considerations
Some browser implementations have historically handled accessibility semantics incorrectly when display: contents is applied to certain elements. Test semantic structures and interactive content carefully.
Important Consideration
The behavior of display: contents has special rules for some elements, including replaced elements and certain table-related elements. It should not be treated as a universal way to remove every wrapper without consequences.
display: none
The none value prevents an element and its descendants from generating boxes in the rendered layout.
Basic Example
.hidden {
display: none;
}
HTML Example
<p>This paragraph is visible.</p>
<p class="hidden">This paragraph is hidden.</p>
Remove an Element from Layout
.optional-panel {
display: none;
}
The hidden panel occupies no layout space.
Show the Element Again
.optional-panel.is-visible {
display: block;
}
Responsive Example
.desktop-only {
display: block;
}
@media (max-width: 767px) {
.desktop-only {
display: none;
}
}
Accessibility Consideration
Content hidden with display: none is generally removed from the accessibility tree and cannot receive keyboard focus while hidden.
Important Consideration
Use display: none when content should be absent from the current rendered interface, not when it must remain available to assistive technology.
List Item Display
The list-item value creates a list-item box that can generate a marker.
Basic Example
.item {
display: list-item;
}
HTML Example
<div class="custom-list">
<div class="item">HTML</div>
<div class="item">CSS</div>
<div class="item">JavaScript</div>
</div>
.custom-list {
padding-left: 25px;
}
.custom-list .item {
display: list-item;
list-style-type: square;
}
List Marker Styling
.custom-list .item::marker {
color: #336699;
}
Multi-Keyword Syntax
.item {
display: block flow list-item;
}
Important Consideration
For actual lists, use semantic HTML elements such as <ul>, <ol>, and <li>. Applying display: list-item to a generic element does not automatically give it list semantics.
Table Display Values
CSS provides display values that allow elements to participate in table formatting.
Table Container
.table {
display: table;
}
Table Rows
.table-row {
display: table-row;
}
Table Cells
.table-cell {
display: table-cell;
padding: 10px;
border: 1px solid #cccccc;
}
Complete Example
<div class="css-table">
<div class="css-row">
<div class="css-cell">HTML</div>
<div class="css-cell">Markup</div>
</div>
<div class="css-row">
<div class="css-cell">CSS</div>
<div class="css-cell">Styling</div>
</div>
</div>
.css-table {
display: table;
border-collapse: collapse;
}
.css-row {
display: table-row;
}
.css-cell {
display: table-cell;
padding: 10px;
border: 1px solid #cccccc;
}
Important Consideration
CSS table display values create table-like layout behavior, but they do not automatically provide semantic table relationships to assistive technology. Use actual HTML table elements for tabular data.
Ruby Display Values
Ruby display values support the layout of small annotations associated with base text, commonly used in East Asian typography.
Ruby Container
.ruby-container {
display: ruby;
}
Ruby Base
.ruby-base {
display: ruby-base;
}
Ruby Annotation
.ruby-text {
display: ruby-text;
}
Semantic HTML Example
<ruby>
漢 <rt>かん</rt>
字 <rt>じ</rt>
</ruby>
Important Consideration
Use semantic HTML <ruby> and <rt> elements for actual ruby annotations. Browser support for manually applying individual ruby display values may vary.
display vs. visibility
The display and visibility properties can both hide elements, but they affect layout differently.
display: none
.hidden {
display: none;
}
The element generates no box and occupies no layout space.
visibility: hidden
.hidden {
visibility: hidden;
}
The element is not painted, but its box normally continues to occupy layout space.
Comparison
| Behavior | display: none | visibility: hidden |
|---|---|---|
| Element visible | No | No |
| Occupies layout space | No | Usually yes |
| Generates a layout box | No | Yes |
| Keyboard focus | Not available while hidden | Not available while hidden |
Recommended Approach
Use display: none to remove an element from layout. Use visibility: hidden when its layout space should remain reserved.
Display and Positioning
The display property interacts with positioning, floats, and formatting contexts.
Absolute Positioning
.positioned-element {
display: inline;
position: absolute;
}
Absolutely positioned elements are blockified, so their computed outer display behavior is adjusted from inline-level to block-level.
Floated Elements
.floated-element {
display: inline;
float: left;
}
Floating also causes blockification of the element's outer display type.
Flex Items
.flex-container {
display: flex;
}
.flex-container .item {
display: inline;
}
A direct in-flow child of a flex container becomes a flex item, and its outer display type is blockified.
Grid Items
.grid-container {
display: grid;
}
.grid-container .item {
display: inline;
}
Direct in-flow grid children are similarly blockified when they become grid items.
Important Consideration
Specified display values are not always identical to their computed or used behavior because other layout rules may transform them.
Display and Sizing
Different display values affect how an element responds to width, height, margins, and padding.
Block Sizing
.block-box {
display: block;
width: 250px;
padding: 15px;
}
Inline Sizing
.inline-box {
display: inline;
width: 250px;
height: 80px;
}
For ordinary non-replaced inline elements, the specified width and height do not determine the inline box dimensions.
Inline-Block Sizing
.inline-block-box {
display: inline-block;
width: 250px;
height: 80px;
}
Flex Sizing
.flex-container {
display: flex;
}
.flex-container .item {
flex: 1;
}
Grid Sizing
.grid-container {
display: grid;
grid-template-columns: 1fr 2fr;
}
Important Consideration
The effect of sizing properties depends on the element's formatting context, intrinsic dimensions, and any minimum or maximum size constraints.
Display and Animation
The display property uses discrete animation behavior rather than continuous interpolation.
Modern browsers can support transitions involving display: none, but entry and exit transitions require additional rules and browser compatibility checks.
Basic Display Change
.panel {
display: none;
}
.panel.is-open {
display: block;
}
Without a supported transition setup, the panel appears or disappears immediately.
Entry and Exit Transition
.panel {
display: none;
opacity: 0;
transition: opacity 0.3s ease, display 0.3s allow-discrete;
}
.panel.is-open {
display: block;
opacity: 1;
}
@starting-style {
.panel.is-open {
opacity: 0;
}
}
How It Works
Where supported, transition-behavior: allow-discrete or the allow-discrete transition shorthand keyword enables a discrete display transition.
For transitions between none and a visible display value, the browser keeps the element visible during the opacity transition and switches the display value at the appropriate endpoint.
Fallback Behavior
Browsers without support for discrete display transitions still apply the display change, but the intended fade may not occur.
Reduced Motion
@media (prefers-reduced-motion: reduce) {
.panel {
transition: none;
}
}
Important Consideration
Test entry and exit transitions across target browsers. The display property does not interpolate gradually between layout modes such as block and grid.
display Example
This example compares block, inline, inline-block, flex, and grid layouts using the same basic item styles.
Each group demonstrates a different way to arrange three elements.
<div class="display-demo">
<div class="demo-group">
<h3>Block</h3>
<div class="demo-block">
<span>One</span>
<span>Two</span>
<span>Three</span>
</div>
</div>
<div class="demo-group">
<h3>Inline</h3>
<div class="demo-inline">
<span>One</span>
<span>Two</span>
<span>Three</span>
</div>
</div>
<div class="demo-group">
<h3>Inline-Block</h3>
<div class="demo-inline-block">
<span>One</span>
<span>Two</span>
<span>Three</span>
</div>
</div>
<div class="demo-group">
<h3>Flex</h3>
<div class="demo-flex">
<span>One</span>
<span>Two</span>
<span>Three</span>
</div>
</div>
<div class="demo-group">
<h3>Grid</h3>
<div class="demo-grid">
<span>One</span>
<span>Two</span>
<span>Three</span>
</div>
</div>
</div>
.display-demo {
display: grid;
gap: 15px;
}
.display-demo .demo-group {
padding: 15px;
border: 1px solid #cccccc;
background-color: #f8f9fa;
}
.display-demo h3 {
margin: 0 0 10px;
font-size: 1rem;
}
.display-demo span {
padding: 8px 12px;
background-color: #336699;
color: #ffffff;
}
.display-demo .demo-block span {
display: block;
margin-bottom: 5px;
}
.display-demo .demo-inline span {
display: inline;
}
.display-demo .demo-inline-block span {
display: inline-block;
margin-right: 5px;
}
.display-demo .demo-flex {
display: flex;
gap: 5px;
}
.display-demo .demo-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 5px;
}
Block
Inline
Inline-Block
Flex
Grid
The block items appear on separate lines. Inline items participate in text flow, while inline-block items retain inline placement with block-like sizing.
The flex container arranges items along its main axis. The grid container divides the available width into three equal columns.
This example illustrates why the display property is one of the fundamental tools for controlling CSS layout.
Accessibility
The display property affects visual layout and can also influence how content is exposed to assistive technology.
Use Semantic HTML
Choose HTML elements according to their meaning. CSS display values do not automatically provide equivalent semantic roles.
Hidden Content
Content hidden with display: none is generally excluded from the accessibility tree and keyboard navigation.
display: contents
Test display: contents carefully when it is applied to semantic or interactive elements because some implementations have had accessibility issues.
Visual Reordering
Flexbox and Grid can visually rearrange items without changing the underlying document order. Ensure that the reading and keyboard focus order remains meaningful.
Responsive Layouts
Responsive display changes should preserve access to essential content and controls across screen sizes.
Tables and Lists
Use semantic list and table markup for meaningful lists and tabular information rather than relying on CSS display values alone.
Animation and Motion
When display changes are animated, consider reduced-motion preferences and ensure that interactive elements are not focusable while hidden.
Important Considerations
Keep the following points in mind when using display:
- Purpose: Controls box generation and layout participation.
- Initial value:
inline. - Inheritance: The property is not inherited.
- Block layout:
blockcreates block-level behavior. - Inline layout:
inlineparticipates in text flow. - Inline-block: Supports block-like sizing within inline layout.
- Flexbox:
flexandinline-flexestablish flex formatting contexts. - Grid:
gridandinline-gridestablish grid formatting contexts. - Flow-root: Creates a new block formatting context.
- Contents: Removes an element's principal box while generally preserving its children's layout participation.
- None: Prevents the element and its descendants from generating rendered boxes.
- List items:
list-itemsupports list marker generation. - Tables: Table-related display values create table formatting behavior.
- Ruby: Ruby display values support annotation layout.
- Multi-keyword syntax: Separates outer and inner display behavior.
- Blockification: Some layout rules automatically adjust outer display types.
- Animation: Display changes use discrete animation behavior.
- Accessibility: Visual display changes should not compromise semantic meaning or keyboard access.
Choose the display value that matches the intended layout. Use Flexbox for primarily one-dimensional arrangements, Grid for two-dimensional layouts, and normal flow when specialized layout behavior is unnecessary.
Browser Support
The CSS display property is widely supported across modern browsers, including Chrome, Edge, Firefox, and Safari.
Core values such as block, inline, inline-block, and none have longstanding support. Flexbox, Grid, and flow-root are also widely supported in modern browsers.
Commonly Supported Values
.block {
display: block;
}
.flex {
display: flex;
}
.grid {
display: grid;
}
.hidden {
display: none;
}
Features Requiring Additional Testing
Compatibility varies for some newer or specialized behaviors, including multi-keyword syntax in older browsers, ruby display values, display: contents on special elements, and discrete transitions involving display.
Feature Detection
@supports (display: grid) {
.container {
display: grid;
}
}
Multi-Keyword Feature Detection
@supports (display: block flex) {
.container {
display: block flex;
}
}
For current compatibility information, visit Can I Use? and search for the specific display feature when necessary.
Specifications
The display property is defined primarily in CSS Display Module Level 3.
The specification defines outer and inner display types, box generation, internal display values, and interactions with formatting contexts.
Related layout behavior is further defined by the CSS Flexible Box Layout, CSS Grid Layout, CSS Tables, and CSS Ruby Layout specifications.
