While working with a piece of JS code yesterday, I stumbled upon something peculiar. There was a div
element that was initially hidden using display:none
, and I was utilizing its height in some JavaScript calculations. Everything was functioning properly until I added a "hidden" class to the element, which had display:none !important
.
Unexpectedly, the reported height always became 0
. The only alteration made was adding the !important
rule to the display property.
After conducting further investigation, I have identified an intriguing finding:
#b { display: none; } /* height is 36 when reported */
#c { display: none !important; } /* height is 0 when reported */
To isolate this issue, I created a simple JSFiddle. This demo uses vanilla JavaScript to retrieve the height, displaying the expected behavior.
It appears that jQuery may inaccurately report the height of invisible DIVs, while the !important
declaration functions correctly.
Could this potentially be a bug within jQuery?