Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › CSS Snippets › Reference › display

CSS Reference

display

Controls how an element participates in the layout. The single most important CSS property for structure.

Last updated: October 2026

Definition: The display property controls how an element is laid out — as a block, inline, flex container, grid container, or hidden. Every element has a default display value, and changing it changes the entire layout behavior.

🎬 Live Example

Five display values, side by side. Notice how each one affects flow, width, and spacing.

display: block
Block element 1
Block element 2
display: inline
Inline 1 Inline 2 Inline 3
display: inline-block
Card 1 Card 2 Card 3
display: flex
Flex 1
Flex 2
Flex 3
display: grid
1
2
3

💡 Each display value changes the layout rules for the element and its children.

📝

Basic Syntax

/* Block element — full width, new line */
p { display: block; }

/* Inline element — flows with text */
span { display: inline; }

/* Hybrid — inline flow + block properties */
.button { display: inline-block; }

/* Flex container — one-dimensional layout */
.container { display: flex; }

/* Grid container — two-dimensional layout */
.grid { display: grid; }

/* Hidden — removed from layout entirely */
.hidden { display: none; }

🎯 Every display Value

Value Behavior Default for
blockStarts new line, full width<div>, <p>, <h1>–<h6>
inlineFlows with text, content width<span>, <a>, <strong>
inline-blockInline flow, accepts box model—
flexFlex container, 1D layout—
inline-flexInline-level flex container—
gridGrid container, 2D layout—
inline-gridInline-level grid container—
noneRemoved from layout—
contentsElement's box disappears, children remain—
tableBehaves like a table element<table>
1

block vs inline

Feature block inline
Line breakYesNo
Default width100% of parentContent width
Accepts width/heightYesNo
Vertical marginsYesNo
Horizontal marginsYesYes
2

Making a Link a Block Button

Anchors default to inline. Add display to make them look like buttons.

.btn {
    display: inline-block;
    padding: 0.75rem 1.5rem;
    background: #3b82f6;
    color: white;
    border-radius: 0.5rem;
    text-decoration: none;
}

.btn-block {
    display: block;
    width: 100%;
    text-align: center;
}
3

Flexbox and Grid Containers

/* Flex — one-dimensional */
.nav {
    display: flex;
    gap: 1.5rem;
    align-items: center;
    justify-content: space-between;
}

/* Grid — two-dimensional */
.card-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 1rem;
}

Setting display: flex or display: grid on a parent makes it a flex or grid container. All direct children become flex or grid items.

4

display: none — Hiding Elements

.hidden {
    display: none;  /* Removed from layout entirely */
}

/* Responsive: hide on mobile, show on desktop */
.sidebar {
    display: none;
}

@media (min-width: 1024px) {
    .sidebar {
        display: block;
    }
}

Warning: display: none also removes the element from screen readers. To hide visually but keep accessible, use a visually-hidden class that clips content with CSS instead.

5

display: contents — Invisible Wrapper

The element's box disappears, but its children remain in the layout.

.wrapper {
    display: contents;
}

/* Before: children are wrapped in a div (flex items become children of wrapper)
   After: children become direct flex items of the grandparent */

Useful for semantic wrappers that you don't want to affect layout. Note that display: contents can break accessibility in some browsers, so use it carefully.

🎯 Common Responsive Pattern

/* Mobile-first: everything stacks */
.container {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

/* Tablet: side-by-side */
@media (min-width: 768px) {
    .container {
        flex-direction: row;
    }
}

/* Desktop: 3-column grid */
@media (min-width: 1024px) {
    .container {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
    }
}

Changing display at breakpoints is the standard way to build responsive layouts.

⚠️ Common Mistakes

♿ Accessibility Notes

❓ Frequently Asked Questions

What is the CSS display property?

The display property controls how an element participates in the layout. It determines whether the element is block, inline, flex, grid, or hidden. It is one of the most important CSS properties.

What is the difference between display block and inline?

Block elements start on a new line, take full width by default, and respect width/height/margin/padding on all sides. Inline elements flow with text, take only as much width as their content, and only respect horizontal margins/padding.

What is display inline-block?

inline-block is a hybrid. The element flows inline (like a word in text) but accepts width, height, margin, and padding on all sides (like a block). It is often used for buttons and inline cards.

What is the difference between display flex and grid?

Flex is one-dimensional — arrange items in a row OR a column. Grid is two-dimensional — arrange items in rows AND columns simultaneously. Use Flex for component layouts, Grid for page-level layouts.

What does display none do?

display: none removes the element from the layout entirely. It takes up no space, is invisible, and is not announced by screen readers. To hide visually but keep for screen readers, use a visually-hidden CSS class instead.

🎯 What's Next?

← All CSS Reference