Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

html - How to offset for scrollbar width with css?

I had to set these properties and make my scrollbar appear on top of the page because it was making my lightbox jitter when I clicked on an image.

html {
 overflow-y: scroll;
 overflow-y: overlay;
}

However on mobile this makes it seem like I have no margin on the right as it appears behind the scrollbar.

Is there a way that I can offset my content on the right to match the scrollbars width, while still keeping it on top of everything?

A screenshot of the issue

The container the images are inside has 4% margin on the left and right but the scrollbar is displaying over it on the right.

question from:https://stackoverflow.com/questions/65927820/how-to-offset-for-scrollbar-width-with-css

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I figured out a solution to my problem that did not involve knowing the width of the scroll bar.

First of all the question was poorly formulated for 2 reasons:

  1. On mobile devices the scrollbar is not the same as on large screens so it does not get in the way
  2. It did interfere with my lightbox plugin (fancybox) but the solution is much simpler

If anyone comes here because of a similar issue, the solution was to apply

overflow-y: scroll;
overflow-y: overlay;

to the <body> tag instead of html as the lightbox plugin could overwrite it that way once you click on an image.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...