I discovered something weird yesterday when working with a piece of JS code. I had a div
that was hidden (display:none
), and I was using the height of it in some calculations in JS. This was all working fine, until I added my "hidden" class (which has display:none !important
).
Suddenly the height was always 0
. There were no other changes than !important
on the display.
After some digging I've narrowed the problem down to something I find rather weird:
#b { display:none; } /* reported height is 36 */
#c { display:none !important; } /* reported height is 0 */
I've created a very basic JSFiddle to isolate this. It also uses vanilla JS to get height, which seems to work just fine / as expected.
It seems like jQuery incorrectly reports height on invisible DIVs, and that !important
behaves correctly.
Is this a bug in jQuery?
Display none don't reduce the size of the dom, just make the element not visible, like visible hidden, without occupies the visual space. Display none don't improve the performance because the goal of virtual scrolling is reduce the number of the elements into the dom.
display:none means that the tag in question will not appear on the page at all (although you can still interact with it through the dom). There will be no space allocated for it between the other tags. visibility:hidden means that unlike display:none, the tag is not visible, but space is allocated for it on the page.
display:none removes the element from the document. It does not take up any space.
The Best Answer is display: none doesn't have a literal opposite like visibility:hidden does. The visibility property decides whether an element is visible or not. It therefore has two states ( visible and hidden ), which are opposite to each other.
I dont think this is a bug in jQuery, jQuery sets display
to block
for a fraction of a second to calculate height
, when you set !important
even that is ovverriden, thats all.
I guess we need some more explanation
There is basically no way to get the height
of an element from DOM if it doesn't have any height. So when display
is none
, the height
is nonexistent or zero
.
In jQuery if display
is none
, we can just set display
to block
for a fraction of a second to get the height
, during this inline
style is altered as usually done by jQuery.
<div id="something" style="display:block">/div>
And then height
is taken, but at this time if you have set display:none!important
in css this inline style wont work, and height
calculated becomes zero
.
In my personal opinion its always better not to use !important as it makes your code/presentation hard to read. Css usually has multitude of ways to override styles.
If you still want to proceed, an inline !important
might override your style, and calculate the height by yourself using the jQuery show for a second technique, or just override the jQuery function to calculate height :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With