Home 〉 CSS Tutorials 〉 CSS Visibility Property Explained with Examples | Learn How Visibility Works in CSS
CSS Visibility Property Explained with Examples | Learn How Visibility Works in CSS !
Want to hide an element but keep its space? The `visibility` property helps you show or hide elements without removing them from the layout. Learn the difference between `visibility: hidden` and `display: none`, and when to use each. It's useful for menus, modals, and tooltips.
In CSS, the visibility property is a versatile way to control whether an element is visible or hidden on the page. Unlike the 'display' property, which removes an element from the layout entirely, the visibility property will hide the element but preserve the space it occupies.
Let's dive into the details of the 'visibility' property and explore how you can use it effectively!
The syntax for 'visibility' is simple:
Syntax ✍
selector {
visibility: visible | hidden | collapse;
}
The default value, meaning the element is fully visible.
<!DOCTYPE html>
<html>
<body>
<p style="visibility: visible;">This text is visible.</p>
<p>This is a paragraph.</p>
</body>
</html>
output 📌
This text is visible.
This is a paragraph.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Output: The text appears as usual.
Hides the element but still keeps its space in the layout.
<!DOCTYPE html>
<html>
<body>
<p>This is a paragraph.</p>
<p style="visibility: hidden;">This text is hidden but still takes up space.</p>
<p>This is a paragraph.</p>
</body>
</html>
output 📌
This is a paragraph.
This is a paragraph.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Output: The text is hidden, but the space where it was will remain.
Try the above code with 'display: none;' and see the output.
Mostly used for table rows or columns, where it removes the element but doesn't affect the rest of the layout.
<!DOCTYPE html>
<html>
<body>
<table>
<tr style="visibility: collapse;">
<td>Collapsed Row</td>
</tr>
<tr>
<td>Visible Row</td>
</tr>
</table>
</body>
</html>
output 📌
Collapsed Row |
Visible Row |
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Output: The first row disappears but the second row remains unaffected.
The visibility property in CSS is a powerful tool for controlling the visibility of elements while keeping their space in the layout. Use 'visible', 'hidden', and 'collapse' wisely to create cleaner, more dynamic layouts.
'visibility: hidden' hides an element but keeps it in the layout. 'display: none' removes the element entirely, including its space.
Yes, you can animate the 'visibility' property, but it's not as smooth as animating properties like 'opacity'.
'visibility: collapse' is typically used in tables, where it collapses rows or columns without affecting the table structure.
Yes, search engines can still crawl and index content with 'visibility: hidden'. However, it's best for hiding elements temporarily.
While 'visibility' itself can be animated, it is not as smooth as animating other properties like 'opacity'. Consider using 'opacity' for more fluid transitions.