CSS Display
CSS Display property determines how an element is rendered in the document flow, with values like block, inline, inline-block, flex, grid, and none controlling layout behavior and box generation.
What does the CSS display property do?
display decides how an element generates boxes. block starts on a new line and fills the available width, inline flows within text and ignores width and height, inline-block flows inline but respects width and height, flex and grid turn the element into a layout container, and none removes it from the page entirely.
span.tag { display: inline-block; padding: 4px 8px; }
nav ul { display: flex; gap: 1rem; }
.gallery { display: grid; grid-template-columns: repeat(3, 1fr); }
.hidden { display: none; }Display Property Demo
Code Editor
<p>Inline: <span class="tag">span</span> <span class="tag">span</span></p>
<p>Inline-block: <span class="tag ib">span</span> <span class="tag ib">span</span></p>
<div class="tag">block div</div>
<div class="row"><div class="tag">flex</div><div class="tag">items</div></div>
<div class="tag hidden">display: none (not shown)</div>Preview
Understanding the Display Property
The CSS display property is one of the most important layout properties. It defines how an element is displayed in the document flow and determines its behavior. From basic block and inline elements to modern flexbox and grid layouts, mastering display is essential for creating responsive, well-structured designs.
Basic Values
block- Full width, new lineinline- Flows with textinline-block- Hybrid behaviornone- Hidden from layout
Modern Layouts
flex- Flexible box layoutgrid- Two-dimensional layoutinline-flex- Inline flexboxinline-grid- Inline grid
Block vs Inline vs Inline-Block
Block Elements
- • Take full available width
- • Start on new line
- • Accept width and height
- • All margins and padding work
- • Examples: div, p, h1, section
Inline Elements
- • Flow within text content
- • Width and height ignored
- • Only horizontal margins work
- • Vertical padding affects line height
- • Examples: span, a, strong, em
Inline-Block Elements
- • Flow inline but accept dimensions
- • Accept width and height
- • All margins and padding work
- • Can control vertical alignment
- • Perfect for buttons, badges
Modern Layout Systems
Flexbox Layout
When to Use
- • One-dimensional layouts
- • Navigation bars
- • Center content
- • Distribute space evenly
Key Properties
- • justify-content
- • align-items
- • flex-direction
- • flex-wrap
Grid Layout
When to Use
- • Two-dimensional layouts
- • Complex page layouts
- • Card grids
- • Responsive designs
Key Properties
- • grid-template-columns
- • grid-template-rows
- • gap
- • grid-area
Best Practices & Common Patterns
Best Practices
- • Use semantic HTML first, display second
- • Choose the right display type for the job
- • Consider mobile-first responsive design
- • Test with screen readers
- • Use flexbox for one-dimensional layouts
- • Use grid for two-dimensional layouts
Common Mistakes
- • Using display: table for layout
- • Overusing display: none for responsiveness
- • Not considering accessibility impact
- • Mixing layout systems unnecessarily
- • Ignoring browser support for newer values
- • Using floats instead of modern layouts
Performance & Accessibility Tips
Performance
- • Use transform for animations instead of display
- • Avoid frequent display changes
- • Consider visibility: hidden for temporary hiding
Accessibility
- • display: none removes from screen readers
- • Use aria-hidden for decorative hidden content
- • Provide focus management for dynamic content
CSS Display — step by step
block vs inline vs inline-block
block elements stack vertically and take full width. inline flows within text. inline-block combines both — flows inline but respects width and height.
🎯 Quick Check
Which display value keeps the element's space in layout but makes it invisible?
CSS Display: summary
Key points
- Block elements (
div,p,h1,section) stack vertically and take the full width by default. - Inline elements (
span,a,strong) sit in a line of text; vertical margins andwidth/heightdon't apply to them. inline-blockis useful for buttons and tags that sit in text but need padding and a size.display: noneremoves the element from layout and from screen readers.visibility: hiddenhides it but keeps its space.display: contentsmakes the element's box disappear so its children act as children of the grandparent.
Common mistakes
- Setting
widthorheighton an inline element such as aspanand seeing no change. - Hiding content with
display: nonewhen it should still be read by screen readers. Use a visually-hidden class instead.
Frequently asked questions
›What is the difference between display none and visibility hidden?
display: none removes the element completely, so it takes no space and nothing reflows around it. visibility: hidden makes it invisible but keeps the space it occupied.
›What is the difference between inline and inline-block?
Both sit in a line of text, but inline-block respects width, height and vertical padding/margin, while inline ignores width/height and vertical margins.
›What is the default display value of a div?
block. A span defaults to inline, and a button or img behaves as inline-block.