Display Property in CSS – Tutorial
Display Property in CSS
The display property in CSS is one of the most fundamental and powerful tools for controlling how elements appear and behave in the layout of a webpage. It determines how elements are visually represented and interact with surrounding elements.
The display property in CSS controls how an element is displayed on a webpage. Common values include block, inline, inline-block, flex, grid, and none. These values dictate whether an element starts on a new line.
display: value;
blockThe element takes up the full width available.
Starts on a new line.
Examples: <div>, <p>, <h1>
.display-block {
display: block;
}
inlineThe element only takes up as much width as necessary.
Does not start on a new line.
Examples: <span>, <a>, <strong>
.display-inline {
display: inline;
}
inline-blockBehaves like inline in terms of layout (does not break line), but allows width and height to be set.
.display-inline-block {
display: inline-block;
width: 100px;
height: 50px;
}
noneCompletely removes the element from the layout.
The space the element would have taken is also removed.
.hidden-element {
display: none;
}
flexEnables Flexbox layout for better alignment, spacing, and responsive design.
.flex-container {
display: flex;
justify-content: center;
align-items: center;
}
gridEnables CSS Grid layout for two-dimensional design.
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
Use block for structural elements.
Use inline for small, embedded elements like links.
Use inline-block when you need size control on inline elements.
Use flex or grid for layout purposes, especially in modern responsive designs.
Use none to hide elements conditionally (e.g., via JavaScript or media queries).
Combine display with position, width, and height for advanced layouts.
Use browser dev tools to inspect the computed display type of any element.
Modern frameworks (like Bootstrap, Tailwind) use utility classes based on display values.
Mastering the display property is essential for any web developer. Whether you’re building simple layouts or complex responsive interfaces, understanding how display works will help you control how elements behave on your page effectively.