admin管理员组

文章数量:1353470

I wonder if it is possible to center the horizontal scroll bar of my site using JavaScript or jQuery. A scroll bar appears for users who use resolutions like 1024 x 768, and therefore, would like her position always stay in the center.

Example:

.png

I wonder if it is possible to center the horizontal scroll bar of my site using JavaScript or jQuery. A scroll bar appears for users who use resolutions like 1024 x 768, and therefore, would like her position always stay in the center.

Example:

https://i.sstatic/YK8Xe.png

Share Improve this question asked Mar 6, 2014 at 16:37 user2297392user2297392 1
  • Even if you could, I don't think it would be good practice. If you don't want part of your site hidden for specific screen resolutions, you should make the site responsive. But if you really want to do this, you could add margin: 0 auto to center the page. – apohl Commented Mar 6, 2014 at 16:39
Add a ment  | 

1 Answer 1

Reset to default 10

Given that the scrollable element in question is document.body you could use the following JavaScript to centre the horizontal scrollbar:

document.body.scrollLeft = (document.body.scrollWidth - document.body.clientWidth) / 2

本文标签: javascriptCenter the horizontal scroll bar of my siteStack Overflow