Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent page content from shift when scroll appears? [duplicate]

Tags:

html

css

scroll

I am currently working on project about car travelling http://wayfi.ru and

I have encountered a problem recently - page content shifts to the left after vertical scroll appears. How to prevent this?

As I know, one could always display disabled scroll and make it enable if there is necessary.

Do you know better technique to accomplish this?

like image 278
makaroni4 Avatar asked Aug 10 '11 08:08

makaroni4


People also ask

How does the scrollbar affect the layout?

If you move from one page of a site without a scrollbar to another with a scrollbar, you'll see a slight layout shift as things squeeze inward a bit to make room for the scrollbar.

How do you fix a page without a scrollbar?

If you move from one page of a site without a scrollbar to another with a scrollbar, you’ll see a slight layout shift as things squeeze inward a bit to make room for the scrollbar. A classic fix was html { overflow-y: scroll; } to force the scrollbar all the time.

How do I prevent an element from scrolling?

There’s an easy way to prevent an element from scrolling its parent. This means that if you have an element with a vertical or horizontal overflow and the user has reached the end of the scroll, the browser will start scrolling the parent element (which is mostly the body) - this is called ‘scroll chaining.’

Why does my scroll position change when images load?

This happens when new DOM elements are added to the page, images load or dimensions of elements change. In this article, we will share techniques to minimize this content shifting. Have you ever opened a website, started reading and, after some time had passed and all assets had finished loading, you found that you’ve lost your scroll position?


1 Answers

I'm afraid I do not know of any other way to do this apart from always showing the scrollbar, active or not.

The simplest way I think is:

html {
    overflow-y: scroll; 
}

This way, even on pages without scrolling there is space reserved for the scrollbar and the content won't jump around between pages.

like image 172
Caroline Elisa Avatar answered Sep 30 '22 12:09

Caroline Elisa