The visibility property in CSS has two different functions. It hides rows and columns of a tableand it also hides an element without changing the layout.
p {
visibility: hidden;
}
tr {
visibility: collapse;
}
visibility has four valid values: visible, hidden, collapseand inherit. We’ll go through each of them to learn more.
visible
Just like it sounds, visible makes things visible. Nothing is hidden by defaultso this value does nothing unless you have set hidden on this or a parent of this element.
hidden
The hidden value hides things. This is different than using display: nonebecause hidden only visually hides elements. The element is still thereand still takes up space on the pagebut you can’t see it anymore (kind of like turning the opacity to 0). Interestinglythis property does not inherit by default. That means thatunlike the display or opacity propertiesyou can make an element hiddenand still have one of its children as visiblelike this:
Notice thatwhile hiddenthe parent element doesn’t trigger the :hover.
collapse
This one only effects table rows (<tr>)row groups (like <thead>)columns (<col>)column groups (<colgroup>)or elements made to be that way via display).
Unlike hiddenthis value hides the table sub-elementwithout leaving the space where it was. If used anywhere but on a table sub-elementit acts like visibility: hidden.
There are so many quirks with this it’s hard to know where to begin. Just as an appetizer:
- Chrome/Safari will collapse a rowbut the space it occupied will remain. And if the table cells inside had a borderthat will collapse into a single border along the top edge.
- Chrome/Safari will not collapse a column or column group.
- Safari collapse a table cell (wrong) but Chrome will not (right).
- In any browserif a cell is in a column that is collapsed (whether or not it actually collapses) the text in that cell will not be displayed.
- Opera (pre WebKit) will collapse the crap out of everythingexcept a table cell (which is correct).
There is morebut basically: don’t use this ever.
inherit
The default value. This simply causes the element to inherit the value of its parent.
Flexbox
visibility: collapse; is used in Flexbox as welland more well defined.
Related Properties
Other Resources
Browser Support
The basicsnot considering all the quirks with collapse:
how to make the image responsive in picture tag using media queries for all browser including IE7
Sorrybut…
Probably visibility: responsive? ;P
Hello,
I think you have 2 things to clarify in the beginning of your nice article (maybe article is outdated):
1- visibility has 6 possible values instead of only 4: visiblehiddencollapseinitialinherit and unset
2- visibility is inherited by default. If you set a parent element’s visibility to hiddenthen the child will have visibility: hiddenunless you explicitly set the child’s visibility to “visible”
Thanks
How do you add the visibility attribute to these rules so that the caption shows up when
the user hovers over the gallery item