Vertical Scrollbar Position Absolute
Asked Answered
D

7

14

Is there a way to have a cross browser vertical scrollbar with an absolute position?

My problem is that the scrollbar changes the width of my website when appears by giving some issues in my layout.

I do not want to remove it, I just want to make its width not disturbing my layout..

Thanks!

Danube answered 9/8, 2011 at 16:4 Comment(0)
F
10

To stop your layout from moving when the scrollbar appears, you can use the css below to always make your scrollbar visible.

html{
    overflow-y: scroll;
}
Freewheeling answered 9/8, 2011 at 19:26 Comment(1)
This should not be the accepted answer. It surely stops layout moving, but you break any css padding / margin symmetry. What if you want in the future to change all the margins of your app or the width of your scrollbar ?Sillabub
K
56

Only in Safari and Chrome (ie. Webkit), you can also use:

html{
    overflow-y: overlay;
}

It will add a scroll bar only when necessery, and put it above your content, which means it will not disturb your layout. Use with caution though, it's proprietary and undocumented.

Kathrynekathy answered 29/12, 2015 at 1:16 Comment(3)
awesome! I didn't know this existedTupi
overlay is now depracated developer.mozilla.org/en-US/docs/Web/CSS/overflowByrn
Great. This is the first time I know that the "overlay" property exists. This helped me a lot.Fabiola
L
18

Since nobody has mentioned it, if you want to use overlay when it's available (e.g., Chrome & Safari) and fallback to scroll when it's not, you can use the following.

html {
  overflow-y: scroll;
  overflow-y: overlay;
}
Logarithm answered 16/7, 2020 at 21:32 Comment(0)
F
10

To stop your layout from moving when the scrollbar appears, you can use the css below to always make your scrollbar visible.

html{
    overflow-y: scroll;
}
Freewheeling answered 9/8, 2011 at 19:26 Comment(1)
This should not be the accepted answer. It surely stops layout moving, but you break any css padding / margin symmetry. What if you want in the future to change all the margins of your app or the width of your scrollbar ?Sillabub
D
3

always display it to not disturb your layout. it's what Google do ;)

Donation answered 9/8, 2011 at 16:6 Comment(0)
B
1

You should use below style.

scrollbar-gutter: stable;
overflow: auto;
Bergerac answered 27/9, 2023 at 13:58 Comment(0)
C
0

As @Jan said overflow: overlay; is depracated.

My problem is that the scrollbar changes the width of my website when appears by giving some issues in my layout.

So, I think we can scrollbar-gutter. (Maybe we have to change margins or padding)

scrollbar-gutter: stable;
/* or /
scrollbar-gutter: stable both-edges;
Contraction answered 30/6, 2023 at 4:4 Comment(0)
P
0

"The keyword value overlay is a legacy value alias for auto." states MDN.

However, when using overflow in conjunction with scrollbar-gutter, it creates the effect that the scrollbar floats, if the content has the width of the page:

body {
  overflow: auto;
  scrollbar-gutter: stable both-edges;
}

.content {
  width: 100vw; /* metrics like 100% should also work */
}
Pupil answered 11/1 at 22:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.