Display Property
Learn how to use the CSS display property to control how elements are rendered in the layout.
CSS Display Property Overview
The CSS display property is one of the most important CSS properties for controlling layout. It determines how an element is displayed on the web page.
Common Display Values:
- block: Full width, starts on new line
- inline: Flows with text, no width/height
- inline-block: Flows inline but respects box model
- none: Completely removed from layout
- flex: Flexible box layout
- grid: Grid layout system
Layout Impact:
- Controls element positioning
- Affects box model behavior
- Determines flow of content
- Influences responsiveness
- Key to modern CSS layouts
Key Concepts:
- Every element has a default display value
- Display affects how elements interact with each other
- Modern layouts use flex and grid extensively
- Understanding display is crucial for responsive design
CSS Display Values
CSS provides numerous display values that control how elements behave in the layout.
| Display Value | Description | Box Model | Common Use Cases |
|---|---|---|---|
block |
Full width, starts on new line | Respects full box model | Headings, paragraphs, sections |
inline |
Flows with text content | Limited box model | Links, spans, emphasis |
inline-block |
Inline flow with box model | Respects full box model | Buttons, form elements |
none |
Completely hidden | No space allocated | Dynamic content, toggles |
flex |
Flexible box layout | Respects full box model | Modern layouts, navigation |
grid |
Grid layout system | Respects full box model | Complex layouts, dashboards |
table |
Table-like behavior | Table box model | Tabular data display |
Default Display Values:
/* Common HTML elements and their default display values */
div, p, h1-h6, section, article, header, footer, main, nav, form {
display: block; /* Default for most structural elements */
}
span, a, strong, em, i, b, mark, small, sub, sup, code {
display: inline; /* Default for text-level elements */
}
li {
display: list-item; /* Default for list items */
}
img, input, textarea, select, button {
display: inline-block; /* Default for replaced elements */
}
table {
display: table; /* Default for tables */
}
tr {
display: table-row; /* Default for table rows */
}
td, th {
display: table-cell; /* Default for table cells */
}
Block Elements
Block-level elements take up the full available width and start on a new line. They respect the full box model.
Block Element Characteristics:
- Always start on a new line
- Take up the full available width
- Respect width, height, margin, and padding properties
- Can contain other block and inline elements
- Stack vertically by default
Block Element Examples:
Block Element 2 (paragraph)
Block Element Behavior:
Default Block Behavior
With Fixed Width
/* Block element styling */
.block-element {
display: block; /* Explicitly set (though usually default) */
width: 100%; /* Default for block elements */
margin: 10px 0; /* Vertical margins work normally */
padding: 15px;
background: #4e73df;
color: white;
}
/* Block elements with custom width */
.custom-width {
display: block;
width: 300px; /* Fixed width */
margin: 10px auto; /* Center with auto margins */
padding: 20px;
}
/* Nested block elements */
.container {
display: block;
width: 80%;
margin: 0 auto;
padding: 20px;
}
.container .content {
display: block;
margin: 10px 0;
padding: 15px;
}
Inline Elements
Inline elements flow within text content and only take up as much width as necessary. They have limited box model support.
Inline Element Characteristics:
- Flow within text content
- Only take up necessary width
- Do not respect width and height properties
- Only horizontal margins and padding work
- Cannot contain block-level elements
- Line breaks can occur within inline elements
Inline Element Examples:
This is a paragraph with an inline span element and a strong element and a link element flowing within the text content.
Inline Element Limitations:
Inline Elements
Note: Width, height, and vertical margins don't work
Inline with Box Model Attempt
Note: Width and height are ignored, vertical margins don't create space
/* Inline element styling */
.inline-element {
display: inline; /* Explicitly set (though usually default for spans, links, etc.) */
/* width and height are IGNORED */
margin: 0 10px; /* Only horizontal margins work */
padding: 5px 15px; /* Padding works but may cause overlapping */
background: #4e73df;
color: white;
}
/* Text-level inline elements */
strong, em, span, a, code {
display: inline; /* Default value */
}
/* Inline elements in text flow */
.paragraph {
font-size: 16px;
line-height: 1.6;
}
.paragraph .highlight {
background: yellow;
padding: 2px 4px; /* Works fine */
/* margin-top: 10px; Would not work */
/* width: 100px; Would be ignored */
}
Inline-Block Elements
Inline-block elements combine the best of both inline and block behavior. They flow inline like text but respect the full box model.
Inline-Block Characteristics:
- Flow within text content like inline elements
- Respect width, height, margin, and padding like block elements
- Do not start on new lines
- Can be aligned vertically using vertical-align
- Perfect for buttons, form elements, and navigation items
Inline-Block Examples:
Inline-Block vs Other Displays:
Inline (Limited)
Inline-Block (Full Control)
/* Inline-block element styling */
.inline-block-element {
display: inline-block; /* Key property */
width: 150px; /* Now works! */
height: 50px; /* Now works! */
margin: 10px; /* All margins work! */
padding: 15px 25px;
background: #4e73df;
color: white;
text-align: center;
vertical-align: middle; /* Control vertical alignment */
}
/* Navigation with inline-block */
.nav-item {
display: inline-block;
padding: 12px 20px;
margin: 0 5px;
background: #333;
color: white;
text-decoration: none;
border-radius: 4px;
}
.nav-item:hover {
background: #4e73df;
}
/* Button group */
.button-group {
text-align: center;
padding: 20px;
}
.button {
display: inline-block;
padding: 10px 20px;
margin: 0 10px;
background: #4e73df;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
Inline-Block Tips:
- Use for elements that need to flow inline but require box model control
- Perfect for navigation menus, button groups, and form elements
- Watch for whitespace between elements (can cause gaps)
- Use
vertical-alignto control alignment
Flex Layout (display: flex)
The flex layout provides a more efficient way to lay out, align, and distribute space among items in a container, even when their size is unknown or dynamic.
Flex Container Characteristics:
- Creates a flex formatting context
- Children become flex items
- One-dimensional layout (row or column)
- Powerful alignment and distribution controls
- Responsive and flexible by design
Flex Layout Examples:
Flex Row (Default)
Flex Column
Flex with Wrapping
/* Flex container */
.flex-container {
display: flex; /* Creates flex context */
flex-direction: row; /* row | row-reverse | column | column-reverse */
justify-content: space-between; /* Main axis alignment */
align-items: center; /* Cross axis alignment */
flex-wrap: wrap; /* nowrap | wrap | wrap-reverse */
gap: 20px; /* Space between items */
padding: 20px;
background: #f8f9fc;
}
/* Flex items */
.flex-item {
flex: 1; /* Grow, shrink, basis */
padding: 20px;
background: #4e73df;
color: white;
text-align: center;
}
/* Specific flex item sizing */
.flex-item-large {
flex: 2; /* Takes twice the space */
}
.flex-item-fixed {
flex: 0 0 200px; /* Don't grow, don't shrink, fixed 200px basis */
}
/* Flex navigation */
.nav-flex {
display: flex;
justify-content: space-around;
align-items: center;
background: #333;
padding: 0 20px;
}
.nav-flex a {
padding: 15px 20px;
color: white;
text-decoration: none;
transition: background 0.3s;
}
.nav-flex a:hover {
background: #4e73df;
}
Grid Layout (display: grid)
CSS Grid Layout is a two-dimensional layout system that handles both rows and columns, making it perfect for complex web layouts.
Grid Container Characteristics:
- Creates a grid formatting context
- Two-dimensional layout (rows and columns)
- Precise control over item placement
- Powerful responsive capabilities
- Ideal for complex page layouts
Grid Layout Examples:
Basic 3×3 Grid
Complex Grid Layout
/* Grid container */
.grid-container {
display: grid; /* Creates grid context */
grid-template-columns: 1fr 1fr 1fr; /* Three equal columns */
grid-template-rows: auto; /* Automatic row sizing */
gap: 20px; /* Space between grid items */
padding: 20px;
background: #f8f9fc;
}
/* Grid items */
.grid-item {
padding: 30px;
background: #4e73df;
color: white;
text-align: center;
}
/* Complex grid layout */
.page-layout {
display: grid;
grid-template-columns: 250px 1fr; /* Sidebar and main content */
grid-template-rows: 80px 1fr 60px; /* Header, content, footer */
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
min-height: 100vh;
}
.header { grid-area: header; background: #333; color: white; padding: 20px; }
.sidebar { grid-area: sidebar; background: #f8f9fc; padding: 20px; }
.main { grid-area: main; background: white; padding: 20px; }
.footer { grid-area: footer; background: #333; color: white; padding: 20px; }
/* Responsive grid */
.responsive-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
padding: 20px;
}
Grid vs Flexbox:
- Use Grid for two-dimensional layouts (rows AND columns)
- Use Flexbox for one-dimensional layouts (row OR column)
- Grid for overall page structure
- Flexbox for components and content within grid areas
- They work great together!
Interactive Display Property Demo
Experiment with different display values to see how they affect element layout and behavior:
Generated CSS:
display: block;
width: 150px;
height: 80px;
What's Next?
Now that you understand the CSS Display Property, you can explore more advanced topics:
- CSS Positioning: Static, relative, absolute, fixed, and sticky positioning
- CSS Floats & Clears: Traditional layout techniques
- CSS Flexbox Deep Dive: Advanced flexbox properties and techniques
- CSS Grid Deep Dive: Advanced grid layout patterns
- CSS Responsive Design: Media queries and responsive units