This bug (specific to Firefox) occurs even when not setting a fixed width.
For instance, if you have a vertically scrollable container div (overflow: auto;
) inside a flexible wrapper div (display: inline-block;
), then when you resize the window to be smaller than the content can wrap, first, a horizontal scrollbar will appear in your container div, and only after that, the flexible wrapper div will grow or eventually a secondary horizontal scrollbar will appear in your window.
The result is a useless horizontal scrollbar, that only can scroll the width of the vertical scrollbar:
In order to get rid of this issue, you could use the javascript-code from this example (tested in Firefox and Chromium):
<!DOCTYPE html>
<html>
<body>
<style type="text/css">
.page {height: 200px;width: 400px;overflow: auto;background-color: #ccc;border: 5px solid #000;margin: 5px;}
.wrapper {display: inline-block;min-width: 100%;margin: 20px;}
.scroller {overflow: auto;max-height: 100px;background-color: #f00;}
.content {min-height: 500px;min-width: 400px;background-color: #cfc;}
</style>
<div class="page">
<div class="wrapper">
<div class="scroller">
<div class="content">
The wrapper-div should expand to fit the scroller content.
Reduce the browser window width, and a useless horizontal scrollbar appears.
</div>
</div>
</div>
</div>
<div class="page">
<div class="wrapper">
<div class="scroller ensure-scrollbar-width-padding">
<div class="content">
But with the javascript-function, this is now fixed.
There is no horizontal scrollbar in the wrapper-div.
</div>
</div>
</div>
</div>
<script>
var ensureScrollbarWidthPadding = function(elem)
{
if(!parseInt(elem.scrollWidth) || !parseInt(elem.clientWidth) || !parseInt(elem.offsetWidth))
{
return; // no browser-support for this trick
}
var update = function()
{
// reset to as if not having any right-padding
elem.style.paddingRight = '0px';
// check if horizontal scrollbar appeared only because of the vertical scrollbar
if(elem.scrollWidth !== elem.clientWidth)
{
elem.style.paddingRight = (elem.offsetWidth - elem.clientWidth) + 'px';
}
else
{
elem.style.paddingRight = '0px';
}
};
window.addEventListener('resize', update, false);
window.addEventListener('load', update, false);
update();
return update;
};
(function()
{
var elems = document.getElementsByClassName('ensure-scrollbar-width-padding');
for(var i=0;i<elems.length;++i)
{
ensureScrollbarWidthPadding(elems[i]);
}
})();
</script>
</body>
</html>
The javascript function ensureScrollbarWidthPadding dynamically adds a padding-right
to the vertically scrollable container, to ensure that the horizontal scrollbar will never appear.
overflow-y: auto
instead. This will only put scrollbars vertically. – Turmeric