Introduction
When designing web pages, one of the most fundamental questions is: How should an element appear and interact within the layout? Should it stack vertically, flow inline with text, sit side by side, or disappear entirely?
CSS provides powerful tools to control these behaviors through the display property and the visibility property.
- The
displayproperty determines the layout role of an element—whether it behaves like a block, inline, inline-block, flex container, grid container, or disappears altogether. - The
visibilityproperty, on the other hand, controls whether an element is visible or hidden, without changing the layout flow.
Mastering these properties is crucial for page structure, spacing, responsiveness, and interactivity.
This guide will cover:
- The core
displayvalues (block,inline,inline-block,none) - A primer on
flexandgrid - The difference between
display: none;andvisibility: hidden; - Common use cases and pitfalls
- Best practices for modern layout design
Part 1: The display Property
The display property defines the box type of an element and how it participates in layout.
Syntax:
selector {
display: value;
}
Common Values
blockinlineinline-blocknoneflexgrid- (others include
inline-flex,inline-grid,list-item,table, etc.)
1.1 Block Elements
A block element takes up the full width of its parent container and starts on a new line.
Default block elements: <div>, <p>, <h1>–<h6>, <section>, <article>.
Example:
div {
display: block;
}
Characteristics
- Expands to fill the container’s width (unless
widthis set). - Respects margin and padding on all sides.
- Stacks vertically.
Visual (simplified):
[Block element 1 - 100% width]
[Block element 2 - 100% width]
1.2 Inline Elements
An inline element flows within a line of text and only takes up as much width as its content.
Default inline elements: <span>, <a>, <strong>, <em>.
Example:
span {
display: inline;
}
Characteristics
- Does not start on a new line.
- Ignores
widthandheightproperties. - Only horizontal padding/margins are respected.
- Useful for styling part of a text line.
Visual:
This is [inline element] inside text.
1.3 Inline-Block Elements
An inline-block element is a hybrid: it flows inline like text, but respects width, height, margins, and padding.
Example:
button {
display: inline-block;
width: 150px;
height: 50px;
}
Characteristics
- Flows inline with text or other elements.
- Can have explicit dimensions.
- Great for buttons, navigation items, and badges.
Visual:
[inline-block][inline-block][inline-block]
1.4 Display None
When an element is set to display: none;, it is completely removed from the layout.
Example:
.hidden {
display: none;
}
Characteristics
- The element is not visible.
- It does not occupy space.
- It is removed from the document flow.
Use Cases
- Toggling menus, modals, dropdowns.
- Conditionally showing content via JavaScript.
1.5 Flex and Grid (Preview)
Modern CSS layouts rely heavily on flexbox and grid. While these deserve their own deep dives, here’s a preview.
Flexbox
.container {
display: flex;
}
- One-dimensional (row or column).
- Great for navbars, cards, and centering.
Grid
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
}
- Two-dimensional (rows and columns).
- Perfect for dashboards, galleries, and page layouts.
Part 2: The visibility Property
The visibility property controls whether an element is visible, but unlike display: none, it still occupies space in the layout.
Syntax:
.invisible {
visibility: hidden;
}
Values
visible(default)hidden– element is invisible but still takes up space.collapse– for table rows/columns (removes them likenone).
Example
.visible {
visibility: visible;
}
.hidden {
visibility: hidden;
}
Visual Difference
display: none;→ element is gone, layout adjusts.visibility: hidden;→ element is transparent, layout unchanged.
Part 3: Comparing Display None vs Visibility Hidden
| Feature | display: none | visibility: hidden |
|---|---|---|
| Visibility | Not visible | Not visible |
| Layout space | Removed from layout | Still occupies space |
| Accessibility (screen readers) | Usually hidden completely | Often still read aloud |
| Use cases | Modals, dynamic UI toggling | Temporary hiding without reflow |
Example:
.hidden {
display: none;
}
.invisible {
visibility: hidden;
}
HTML:
<p>Paragraph 1</p>
<p class="hidden">Paragraph 2 (display: none)</p>
<p class="invisible">Paragraph 3 (visibility: hidden)</p>
<p>Paragraph 4</p>
Result:
- Paragraph 2 disappears completely.
- Paragraph 3 leaves a blank space.
Part 4: Practical Use Cases
Toggle a Menu
.menu {
display: none;
}
.menu.active {
display: block;
}
Make an Element Invisible But Preserve Layout
td.hidden-cell {
visibility: hidden;
}
Create Inline Navigation
nav a {
display: inline-block;
padding: 10px 15px;
}
Build a Simple Grid with Inline-Block
.card {
display: inline-block;
width: 30%;
margin: 1%;
}
Part 5: Common Pitfalls
- Confusing
inlinevsinline-block:
Inline cannot set width/height, inline-block can. - Forgetting
display: blockfor custom buttons or links:
By default<a>is inline; making it block/inline-block allows padding and sizing. - Using
visibility: hiddenfor interactive toggles:
It still leaves space, which can confuse layouts. Usedisplay: nonefor collapsible elements. - Misusing
display: flexwithout flex properties:
Flex alone doesn’t arrange content—you must also usejustify-content,align-items, etc.
Part 6: Best Practices
- Use semantic defaults:
<p>should remain block,<span>should remain inline, unless there’s a good reason. - Prefer
inline-blockfor horizontally aligned elements (like nav links). - Use
display: nonefor UI toggling (dropdowns, modals). - Use
visibility: hiddenwhen you need to hide content but preserve layout integrity. - For modern layouts, learn Flexbox and Grid instead of hacking with inline-blocks or floats.
Conclusion
The display and visibility properties are fundamental to CSS layout and design.
block,inline, andinline-blockdefine how elements flow in the document.display: none;completely removes elements from the flow.visibility: hidden;hides them while preserving their space.flexandgridintroduce powerful layout systems for complex designs.
By understanding these properties deeply, you gain precise control over your layouts, making your web pages both beautiful and functional.


Leave a Reply