Hiding and showing elements should be done with visibility: hidden/visible or with display: none/block
(or inline-block, flex, etc.). One of the two can be used, but there are cases in which one is better than the other. In most cases using display is the better solution for hiding elements because the document will be rendered as if those elements didn't exist in it. In cases where animation is applied to an element, visibility/display should be applied after the animation ends.
In this section we will emphasize on the comparison between the two and answer which and why should be used in a given case.
visibility: hidden
causes a bigger Paint then display:none because it doesn't exclude the elements from the document, just hides them visually.display: none
for hiding entire screens/scenes. Bigger elements have a bigger chance to go over other elements and cause redraws as mentioned earlier.display: none
- there is a very brief Recalculate Style done.Displaying many elements back on the screen will cause drop in performance than when using visibility: visible because the elements should take back its place on the screen and recalculate their styles (background color, geometry, etc.). A workaround is to display elements on portions. For example - firstly displaying buttons/icons, then minimap and then a list of items. Making elements to appear in portions will not bottleneck frames.
visibility: hidden
to hide entire scenes/screens or very big elements. Even though an element is not visible, it is still in the document flow. Furthermore it still affects other elements and even if it is in its own layer, it can still have diminishing effects. If one element is hidden and another is placed on top of it, this will cause heavier Paints if there is some interaction on it or an animation is applied to any one of them.hidden
, the animation will still run which will have an impact on the performance. Use visibility: hidden
only if the animation should stay active.visibility: hidden
. More elements in the document flow means less performance.When choosing between display or visibility property, it must be taken into consideration their intended purpose. In short - visibility should be used to hide and show an element which needs to stay in the document flow at all times. Hiding an element will allow an animation to continue running and it can be paused to preserve its current state. Display: none should be used to hide/disable no longer needed elements in the context of the scene and increase the responsiveness of the UI.