Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centered div changes position when scroll shows up

I have a div container which is centered (by margin-left:auto and margin-right:auto) and page design looks fine when loaded... Below this div there is another hidden one, which shows up on user request. But when this happens browser scroll shows up and mess up my design because centered div also moves few pixels to the left (so it can again be in the center).

Can this behavior be stopped?

Alternative solution is adding overflow-y:scroll but I found that overflow-y is not supported by all browsers and I can't find by which browsers... Can somebody post a link where I can see browser support list for css3 functions?

like image 501
domagojk Avatar asked Aug 23 '26 00:08

domagojk


2 Answers

With scroll always being shown, maybe be not good for layout.

Try to limit body width with css3

body {
    width: calc(100vw - 34px);
}

vw is width of viewport (see this link for some explanation)
calc calculate in css3
34px stands for double scrollbar width (see this for fixed or this to calculate if you don't trust fixed sizes)

like image 84
Moesio Avatar answered Aug 25 '26 16:08

Moesio


This css will always show vertical scroll on your page.

body {
    overflow-y: scroll;
}

By default it's overflow: auto;

like image 42
Teneff Avatar answered Aug 25 '26 17:08

Teneff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!