The difference between "visibility: collapse" and "display: no"

What is the difference between visibility:collapse and display:none ?

+61
html css
Sep 12 '10 at 17:37
source share
4 answers

Short version:

The first is used to completely hide table elements. The latter is used to completely hide everything else.

Long version :

visibility: collapse completely hides the element (so that it does not occupy any place in the layout), but only when the element is an element of the table .

If used for elements other than table elements, visibility: collapse will act as visibility: hidden . This makes the element invisible, but it will still take up space in the layout.

display: none completely hides the element, so it does not occupy any place in the layout, but it cannot be used in table elements.

W3C Link

+68
Sep 12 '10 at 17:40
source share

visibility:collapse should be used only in tables. On other elements, it will act as visibility:hidden .

visibility:hidden hide the element, but still take the space of the element, while display:none will not even save space.




Resources:

In the same topic:

  • What is the difference between visibility: hidden and display: none
  • CSS Properties: Display and Visibility
  • CSS rendering: none and visibility: hidden
  • Does opacity: 0 has the same effect as visibility: hidden
+16
Sep 12 '10 at 17:40
source share

visibility: collapse behaves exactly the same way as visibility: hidden in most formatting contexts: the space required by the element is “reserved” in the layout, but the element itself is not displayed, leaving an empty space where it would be.

There are three exceptions that I know of: table-rows, table-columns, and flex items, in which visibility: collapse behaves like display: none , but with one significant difference: "strut". You can think of a layout as a zero placeholder size, which does not claim to have any place in the layout process, but nevertheless is still part of the formatting structure and is involved in calculations of a certain size.

A collapsed row table, for example, will not occupy a single vertical space in the table, but the columns of the table will still have an “as if” collapsed row size, and its contents were actually visible. This is to prevent columns from “wobbling” when rows are switched and exited. Likewise, the rolled up flex element does not occupy any space along the main axis, but still contributes to the cross-dimension of the flex line.

“Do not use display: none with tables” is a valuable rule of thumb, but it does not tell the whole story.

  • Use display: none if you do not want your hidden elements to participate in any way in the layout of the table (or the flexible line).
  • Use visibility: collapse if you want to dynamically display and hide elements without destabilizing the layout of the table (or flex line).

Here is a code snippet showing the difference between display: none and visibility: collapse for a table row:

 .show-right-border { border-right: 1px black solid; } 
 <h3>visibility: collapse</h3> <table class="show-right-border"> <tr> <td>Short text.</td> <td style="visibility: collapse;">Loooooooooong text.</td> </tr> </table> <h3>display: none</h3> <table class="show-right-border"> <tr> <td>Short text.</td> <td style="display: none;">Loooooooooong text.</td> </tr> </table> 
+13
Mar 08 '16 at 23:53 on
source share

visibility:collapse has display:none behavior for table elements only. On other elements, it should appear as hidden .

+2
Sep 12 '10 at 17:41
source share



All Articles