Updated after comment. If the width of the body does not exceed the browser window:
Scroll inside the element :
var elem = document.getElementById("container");
var elemWidth = elem.scrollWidth;
var elemVisibleWidth = elem.offsetWidth;
elem.scrollLeft = (elemWidth - elemVisibleWidth) / 2;
Use this code to vertically and horizontally center the page (see also this answer ): window.scrollTo(
(document.body.offsetWidth -window.innerWidth )/2,
(document.body.offsetHeight-window.innerHeight)/2
);
To center only horizontally, use:
window.scrollTo((document.body.offsetWidth -window.innerWidth )/2, 0);
source
share