Performance differences between visibility:hidden and display:none

前端 未结 8 1035
一生所求
一生所求 2020-11-27 04:34

I want to simplify things in my jQuery Backbone.js web application. One such simplification is the behavior of my menu and dialog widgets.

Previously I created the

相关标签:
8条回答
  • 2020-11-27 04:44

    If you are toggling between visible and invisible states via javascript then visibility:hidden should be the better performer. Seeing as it always takes up the same amount of space in both visible and hidden states it won't cause a reflow of the elements below it every time you make it appear of disappear. For display:none you are removing it from the flow of the document and then when you set it to display:block you are rerendering it and pushing everything below that element down, essentially laying all that stuff out again.

    But if you are doing something like toggling visible states on button presses then you really should be using what suits your needs rather than what performs better, as the performance differences are negligible in such cases. When you are animating with the dom at around 20 times per second THEN you can worry about the performance of visibility:hidden vs display:none.

    0 讨论(0)
  • 2020-11-27 04:45

    From personal experience having just tried both on a simple static page with a form located beneath a "hidden" button, visibility: hidden performs flawlessly whereas display: none causes clickable buttons to slightly jump upon clicking, as if it tries to show the hidden button for a millisecond.

    0 讨论(0)
  • 2020-11-27 04:51

    Well, the main performance difference between display: block and visibility: hidden is that if you have a list of, say, 100000 elements, the visibility: hidden won't save you from DOM hanging because it doesn't remove elements from DOM. visibility: hidden acts like opacity: 0 + pointer-events: none. display: none acts like Element.remove().

    Live example: https://jsfiddle.net/u2dou58r/10/

    0 讨论(0)
  • 2020-11-27 04:52

    I think this could be somehow related to this question: CSS Properties: Display vs. Visibility

    I'll just quote the interesting part:

    the element is NEVER removed from the DOM hierarchy. All block level display 'styles' are completely 'hidden' when using display:none, whereas with visibility:hidden; the element itself is hidden but it still occupies a visual space in the DOM.

    So there should be no real difference in regard to browser performance, because both versions are still in the DOM hierarchy. These properties only affect how an element is displayed in regards to the DOM.

    0 讨论(0)
  • 2020-11-27 04:59

    I'm not aware of any performance difference between display:none and visibility:hidden - even if there is, for as little as 10 elements it will be completely negligible. Your main concern should be, as you say, whether you want the elements to remain within the document flow, in which case visibility is a better option as it maintains the box model of the element.

    0 讨论(0)
  • 2020-11-27 05:00

    visibility: hidden does not cause a re-flow on the document, while display: none does.

    display: none: The HTML engine will completely ignore the element and its children. The engine will not ignore elements marked with visibility: hidden, it will do all the calculations to the element and its children, the exception is that the element will not be rendered to the viewport.

    If the values for position and dimensions properties are needed then visibility: hidden have to be used and you have to handle the white space in the viewport, usually by wrapping that element inside another one with 0 width and height and 'overflow: hidden'.

    display:none will remove the element from the document's normal flow and set the values for position/height/width to 0 on the element and its children. When the elements display property is changed to other value than none, it triggers a complete document re-flow, which can be a problem for big documents - and sometimes not-so-big documents being rendered on hardware with limited capabilities.

    display: none is the natural and logical solution to use when hiding elements on the viewport, visibility: hidden should be used as a fallback, where/when needed.

    EDIT: As pointed by @Juan, display: none is the choice to go when what you need is to add many elements to the DOM tree. visibility: hidden will trigger a re-flow for each element added to the tree, while display: none will not.

    0 讨论(0)
提交回复
热议问题