Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make scroll bar take no space / prevent layout shift

Tags:

html

css

layout

I am making an application with html and am needing to have a scroll bar. But whenever I add one it shifts everything over to make the necessary room needed for the scroll bar. This screws up the layout of the application.

So I need a way to make the scroll bar simply overlay on top of the page, meaning the page is still there behind it. I already know how to style the scroll bar to make the back transparent, I just need the scroll bar to not take up any space.

Thanks In Advance!

like image 234
Porter Morgan Avatar asked Jan 26 '14 18:01

Porter Morgan


People also ask

How do I get rid of unnecessary scroll bar?

A quick fix is to add overflow: hidden to the CSS for your #footer . Note: A scrollbar will still appear if your #body content flows out of the the viewport. Show activity on this post. It will remove unnecessary scroll bars.

How can we make sure that a page doesn't Display a horizontal scroll bar when it loads?

Step 1: Add width: 100vw to a wrapper element. This makes it as wide as the viewport, ignoring the appearance of a scrollbar. Step 2: Add overflow-x: hidden to the content element. This will remove the horizontal scrollbar (created when vertical scrollbar appears, to allow the user to "look under" it).

How do I fix the scroll bar in CSS?

The easy fix is to use width: 100% instead. Percentages don't include the width of the scrollbar, so will automatically fit. If you can't do that, or you're setting the width on another element, add overflow-x: hidden or overflow: hidden to the surrounding element to prevent the scrollbar.

How do I turn off the overflow auto scroll bar?

To hide the vertical scrollbar and prevent vertical scrolling, use overflow-y: hidden like so: HTML.


2 Answers

Update - overlay was webkit-only and is now deprecated.


You can use the overflow overlay property instead of scroll:

html {
    overflow-y: overlay;
}
like image 139
Sos Sargsyan Avatar answered Sep 18 '22 03:09

Sos Sargsyan


There's no way to make a scrollbar appear on top of your content without using shady hacks, and it's not the best idea anyways since it will affect the usability of your app. If the scroll bar is over the content, it's likely to hide links/text/etc.

Your page ideally should be styled in such a way that it adapts to different browser/page widths without breaking. Especially with just a little scroll bar.

Anyways, here's a workaround that may help:

html {
    overflow-y: scroll;
}

Adding this style will make sure the scroll bar track is always present, and will also help avoid "jumpy" pages when a scrollbar appears/disappears. Again, the real solution is to use a flexible layout.

As a side note, styling the scrollbar is generally not recommended. It doesn't work cross-browser and will usually be ignored completely.

like image 35
Wesley Murch Avatar answered Sep 20 '22 03:09

Wesley Murch