Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

!important and display:none and .height() weirdness

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?

like image 532
Eirik Hoem Avatar asked Feb 25 '14 08:02

Eirik Hoem


People also ask

Does display none improve performance?

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.

What is the difference between display none and visibility?

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.

What happens when display none?

display:none removes the element from the document. It does not take up any space.

What is opposite of display none?

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.


1 Answers

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 :)

like image 170
sabithpocker Avatar answered Sep 18 '22 12:09

sabithpocker