The display property in CSS determines how an element is rendered in the document flow — specifically, how it’s visually structured, how it interacts with surrounding elements, and whether it’s visible or hidden. It defines the element’s layout behavior.
Different display values change:
width and height.Common display values include:
block — Takes up the full available width, starts on a new line, and respects width and height.inline — Sits in line with surrounding text, ignores width and height.inline-block — Behaves like inline elements but allows setting width and height.flex — Turns the element into a flex container for flexible layouts.grid — Turns the element into a grid container for grid-based layouts.none — Hides the element completely and removes it from the layout.Example:
/* A block element */
div {
display: block;
}
/* An inline element */
span {
display: inline;
}
/* A hidden element */
.hidden {
display: none;
}<div>)<span>)div {
display: block;
}
span {
display: inline;
}Block elements start on a new line and take full width. Inline elements appear side-by-side and ignore width/height.
The inline-block value for the display property is useful when you want elements to appear next to each other on the same line (like inline elements) but still be able to setwidth, height, margin, padding, and other box-model properties (like block elements).
Unlike inline, which ignores width and height, inline-block respects these properties. This makes it perfect for creating horizontally aligned buttons, cards, or navigation items.
Example: Two buttons will appear side-by-side, each with a fixed width and height.
button {
display: inline-block;
width: 120px;
height: 40px;
padding: 10px;
background-color: #4CAF50;
color: white;
text-align: center;
}<button>Button 1</button>
<button>Button 2</button>There are two common ways to hide elements in CSS: display: none and visibility: hidden.
display: none; — The element is completely removed from the layout. It won’t take up any space on the page and behaves as if it’s not in the DOM (though it still exists in the code).visibility: hidden; — The element is invisible but still takes up the same space in the layout. This can be useful when you want to hide content without shifting other elements.Examples:
.hidden {
display: none;
}
.invisible {
visibility: hidden;
}<p>This paragraph is visible.</p>
<p class="hidden">This paragraph is hidden and does not take space.</p>
<p class="invisible">This paragraph is hidden but still takes up space.</p>Create three <div> elements:
display: blockdisplay: inlinedisplay: inline-block and set width<div style={{ display: "block", backgroundColor: "lightblue", marginBottom: "10px" }}>
Block Element
</div>
<div style={{ display: "inline", backgroundColor: "lightgreen", padding: "5px" }}>
Inline Element
</div>
<div style={{ display: "inline-block", backgroundColor: "lightcoral", width: "150px", height: "50px", marginLeft: "10px" }}>
Inline-Block Element
</div>You can copy and paste this code in your IDE to run!
Ask the AI if you need help understanding or want to dive deeper in any topic