My page layout looks something like this:
<style type="text/css">
#content-wrap
{
margin: 0 auto;
width: 800px;
}
</style>
<div id="content-wrap">
</div>
You'll notice that the content-wrap div shifts its position a tad bit when the vertical scrollbar appears. One scenario is when the browser starts to progressively render the page without displaying the vertical scrollbar, then determines that a scrollbar is needed because the content is taller than the "fold". This shifts the div about 10px towards left.
What is the best way to tackle this problem without forcing the browser to always display the scrollbar?
I'm afraid the best way to solve this is to force the scroll bar to be visible at all times with
html {overflow-y: scroll;}
. The problem you have is that the "available area" shrinks with say 10 px when the scroll bar appear. This cause the calculated margin on your left side to shrink with half the width of the scroll bar, thus shifting the centered content somewhat to the left.A possible solution might be to calculate the margin with JavaScript instead of using
margin: 0 auto;
and somehow compensate for the "lost" pixels when the scroll bar appear, but I'm afraid it will be messy and the content will probably move a little bit anyway while you calculate and apply the new margin.First I would recommend optimizing the HTML so that it won't take so long to load/render. If load/render is fast the scrollbar won't appear "too late". What is it that takes long to load/render? Check the network tab in chrome debug tools (F12). Do an audit in Chrome debug tools.
There are multiple things that could make the document "reflow", and the scrollbar appear even though the browser could have known the necessary measurements right from the start. Are you using tables for layout - don't! They may need multiple passes of rendering. Do you have images without width/height specified? Then the document will need to be rerendered when each image loads. Specify
<img ... style="width: ..px; height: ..px">
. Is the CSS sane and efficient?If you can't get load/rendering speed down I think your best bet is to not use the browser's scrollbar if javascript is enabled. That way you can control it and place it absolutely positioned so that it won't affect horizontal positioning.
Let your slider start of with
display: none
. Monitor dom ready event as well as image load events as well as window resize events. When the page has been loaded, images have been loaded and when window gets resized just run the same function every time. It would determine if the scrollbar is needed and either display it or hide it.You could use JQuery UI Slider for example and set it's maxValue to
$(document).height() - $(window).height()
, monitor the slider change event and then scroll the body to the value of the slider and so forth.If javascript is disabled the fallback will be the regular scrollbar and there's nothing you can do about the slight horizontal shift then.
But really I think the problem of the horizontal shift is too small to spend time fixing with a custom scrollbar, and check that it actually works well on all platforms etc. Do HTML/CSS optimizations first.
If your site is "responsive" (reacts to width):
Step 1: Add
width: 100vw
to a wrapper div. This makes it as wide as the viewport, ignoring the appearance of a scrollbar.Step 2: Add
overflow-x: hidden
to the body.This will remove the horizontal scrollbar (created when vertical scrollbar appears, to allow the user to "look under" it)."wrapper div" is referring to another div around your
#content-wrap
Will work for your case too, tested:
Make sure nothing on your page is wide enough to get caught under the scrollbar.
If your site is fixed width + centered (your case):
This will add a left margin equal in width to the scrollbar when it appears. Which is 0 when it does not. Taken from here, but tested.
Use jquery and put this in the start of your tag:
and call the function checkheight(); in the end of your tag plus wherever you have onclick (or other) events that make the page longer or shorter in height.
If you can use Javascript, you can set the width of the
content-wrap
to the inner width of the window minus the standard width of a scrollbar.You will run into some problems though.
So if you can live with all that, you can do something like this (in pseudo code)
You must use: