CSS Display Property
The display property in CSS is one of the most fundamental properties used to control the layout and visibility of elements on a web page. It dictates how elements are rendered in the document flow, influencing their behavior, positioning, and interaction with other elements.
The
displayproperty defines how an HTML element is displayed in the browser. It determines whether an element acts as a block, inline, or flex/grid container, among other possibilities.
2. Why Use the Display Property?
- Layout Control – Manage element arrangement and structure.
- Visibility – Hide or reveal elements without removing them from the DOM.
- Responsive Design – Adjust layouts dynamically based on screen size.
- Consistency – Standardize how different elements appear across pages.
3. Common Display Values and Their Effects
| Display Value | Description | Example Elements |
|---|---|---|
block | Element takes full width, starts on a new line. | <div>, <p>, <section> |
inline | Element only takes as much width as necessary, no line break. | <span>, <a>, <strong> |
inline-block | Like inline, but allows width/height to be set. | <button>, <img> |
none | Element is hidden (not rendered). | Any |
flex | Enables flexible box layout. | Parent containers |
grid | Enables grid-based layout. | Parent containers |
inline-flex | Flex container but behaves like an inline element. | |
inline-grid | Grid container but behaves like an inline element. | |
table | Element behaves like a table. | <table>, <thead>, <tr> |
list-item | Makes an element behave like a list item (<li>). | <li> |
inherit | Inherits the display value from its parent. |
4. Block vs. Inline Elements
| Property | Block Elements | Inline Elements |
|---|---|---|
| Width | Takes full width by default. | Only as wide as content. |
| Height | Respects height settings. | Ignores height and line height. |
| Starts on New Line | Yes | No |
| Example | <div>, <h1>, <p> | <span>, <a>, <strong> |
Example:
Effect:
- The
<div>stretches across the page, while<span>fits inline with surrounding text.
5. Inline-Block (Best of Both Worlds)
- Acts like
inlinebut allows height, width, margin, and padding.
Example:
- Effect: Buttons are inline but respect width and height.
6. Display None (Hiding Elements)
display: none;removes the element from the layout, making it invisible and non-interactive.- Use Case: Modals, dropdowns, hiding sections dynamically.
Example:
7. Default Display Values (By Element Type)
Element | Default Display Value |
|---|---|
| |
| |
| |
| |
| |
| |
8. Flex and Grid for Advanced Layouts
display: flex;– Enables flexbox, allowing for easy alignment and distribution of elements.display: grid;– Creates grid-based layouts with rows and columns.
Flex Example:
Grid Example:
9. Changing Display Dynamically
You can toggle display using JavaScript for interactive elements:
10. Example: Full Page Layout with Display
- Use Block for Structure – For layout, use
blockorflex. - Inline for Text – Use
inlinefor text-level elements. - Inline-Block for Forms/Buttons – Allows alignment while controlling size.
- Flex and Grid for Layout – Modern, responsive designs leverage
flexandgrid. - Hide and Show with
none– Usedisplay: none;to manage dynamic content.
By mastering the display property, you gain precise control over your web layout, creating more structured, flexible, and responsive designs.