Loading...
Loading...
By continuing to use the platform, you accept the terms of the Privacy Policy and the use of cookies.
visibility: hidden and display: none are two ways to hide an element in CSS, but they work differently and have different consequences for the page layout.
display: none and display: blockvisibility property<div class="container">
<div class="box">Box 1</div>
<div class="box hidden-display">Box 2 (display: none)</div>
<div class="box">Box 3</div>
</div>
<div class="container">
<div class="box">Box 1</div>
<div class="box hidden-visibility">Box 2 (visibility: hidden)</div>
<div class="box">Box 3</div>
</div>
.box {
width: 100px;
height: 100px;
background-color: #3498db;
margin: 10px;
}
.hidden-display {
display: none;
}
.hidden-visibility {
visibility: hidden;
}
Result:
display: none: Box 2 completely disappears, Box 3 shifts to Box 2's positionvisibility: hidden: Box 2 is invisible but its space remains empty, Box 3 does not shiftUse display: none when:
/* Modal window */
.modal {
display: none;
}
.modal.active {
display: block;
}
/* Mobile menu */
.mobile-menu {
display: none;
}
@media (max-width: 768px) {
.mobile-menu {
display: block;
}
}
Use visibility: hidden when:
/* Smooth appear/disappear */
.fade-element {
visibility: hidden;
opacity: 0;
transition: visibility 0.3s, opacity 0.3s;
}
.fade-element.show {
visibility: visible;
opacity: 1;
}
/* Preserving space for hidden content */
.tooltip {
visibility: hidden;
position: absolute;
}
.tooltip:hover {
visibility: visible;
}
.invisible-opacity {
opacity: 0;
/* Element is invisible but takes up space and remains interactive */
}
Difference:
opacity: 0 — element is invisible but remains interactive (can be clicked)visibility: hidden — element is invisible and not interactive.sr-only {
position: absolute;
left: -9999px;
/* Element is visually hidden but accessible to screen readers */
}
This method is used to hide elements from view but keep them accessible to screen readers.
| Property | Takes up space | Interactive | Accessible to screen readers | Can be animated |
|---|---|---|---|---|
display: none | No | No | No | No |
visibility: hidden | Yes | No | No | Yes |
opacity: 0 | Yes | Yes | Yes | Yes |
/* Card with smooth appearance */
.card {
visibility: hidden;
opacity: 0;
transform: translateY(20px);
transition: visibility 0.3s, opacity 0.3s, transform 0.3s;
}
.card.visible {
visibility: visible;
opacity: 1;
transform: translateY(0);
}
/* Completely hidden element (does not take up space) */
.hidden {
display: none;
}
Important:
Choose the hiding method based on your needs: use display: none for complete removal from layout, visibility: hidden to preserve space, and opacity: 0 if element should remain interactive.