Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Usage of CSS visibility property
The CSS visibility property controls whether an element is visible or hidden. Unlike display: none, hidden elements with visibility: hidden still occupy space in the layout.
Syntax
selector {
visibility: value;
}
Possible Values
| Value | Description |
|---|---|
visible |
The element and its contents are shown to the user (default) |
hidden |
The element and its content are invisible, but still affect the page layout |
collapse |
Used only with table rows and columns to remove them from display |
Example: Visible vs Hidden Elements
The following example demonstrates how visibility: hidden hides elements while preserving their layout space −
<!DOCTYPE html>
<html>
<head>
<style>
.container {
border: 2px solid #333;
padding: 20px;
margin: 10px 0;
}
.visible-box {
background-color: #4CAF50;
color: white;
padding: 15px;
margin: 10px 0;
visibility: visible;
}
.hidden-box {
background-color: #f44336;
color: white;
padding: 15px;
margin: 10px 0;
visibility: hidden;
}
</style>
</head>
<body>
<div class="container">
<div class="visible-box">This box is visible</div>
<div class="hidden-box">This box is hidden but takes up space</div>
<div class="visible-box">This box is also visible</div>
</div>
</body>
</html>
A bordered container with two visible green boxes appears. There's an empty space between them where the hidden red box still occupies layout space, even though it's not visible.
Example: Dynamic Visibility with Inline Styles
You can also control visibility using inline styles −
<!DOCTYPE html>
<html>
<head>
<style>
.text-block {
background-color: #e7f3ff;
border: 1px solid #b3d9ff;
padding: 10px;
margin: 5px 0;
}
</style>
</head>
<body>
<div class="text-block" style="visibility: visible;">
This paragraph is visible.
</div>
<div class="text-block" style="visibility: hidden;">
This paragraph is hidden but still affects layout.
</div>
<div class="text-block">
This paragraph is visible and appears after the hidden space.
</div>
</body>
</html>
Two visible blue text blocks appear with a gap between them where the hidden block occupies space but remains invisible.
Conclusion
The visibility property is useful for hiding elements while maintaining page layout. Use visibility: hidden when you want to hide content but preserve the space it occupies in the document flow.
Advertisements
