Adrian Marinica Adrian Marinica - 6 days ago 9
HTML Question

Refresh Html layout to recalculate scrollbars

I have a

div
with a fixed size which displays
scrollbars
because the content is too large. This is ok. My problem occurs when I resize the
div
in order to hide the
scrollbars
, I have to increase the size more than necessary to hide the
scrollbars
. This is because the
scrollbars
themselves occupy some space in the
div
. Is there a way to ask the browser to recalculate the size of the
div
and its inner content and see if there really should be a scrollable content or not?

Answer

With jQuery for DOM handling:

$("div#yourdiv").attr("overflow", "hidden"); <-- remove scrollbars
setTimeout(function(){
   $("div#yourdiv").attr("overflow", "auto"); // auto | initial
},1); <-- 1 millisecond is enough to allow the DOM to update