Stories

How to Avoid Page Flickering When Scrollbars are Dynamically Shown or Hidden?

There are certain cases when you want to dynamically manipulate the page height which will result in flickering.

November 8, 2022

css scrollbar flicker

How to Avoid Page Flickering When Scrollbars are Dynamically Shown or Hidden?

When a website's height is larger than the viewport, which is almost always the case, a browser will automatically display a y-axis scrollbar on the right side of the screen. While there's nothing wrong with that, there are certain cases when you want to dynamically manipulate the page height which will result in flickering which looks like a bug. The flicker effect occurrs when the previously existing scrollbars suddenly dissapear making the site about 15 pixels wider, depending on the browser.

This is primarily a desktop issue, on mobile phones, scrollbar don't use extra space.

Let's say you're coding a modal box or a menu, and you want to prevent user from scrolling while the menu is displayed. To disable the page body from scrolling you might use position: fixed; which will automatically remove the scrollbars.

The easiest solution, if you don't care much cross-browser compatibility, would be to simply use overflow: overlay;.

/* body or parent element */ .parent { overflow: overlay; }

The overlay value of the overflow CSS property is a non-standard value to make scrollbars appear on top of content rather than take up space. This value is, however, deprecated and related functionality being standardized as the scrollbar-gutter property.

Next solution to solve this and avoid the flickering would be to not hide the scrollbars at all by adding overflow-y: scroll; to the body or the parent element.

/* body or parent element */ .parent { overflow-y: scroll; }

Related Content

Easy Way to Fix Anchor Links Scrolling Too Far

Stories

Easy Way to Fix Anchor Links Scrolling Too Far

Fixed navbar is a very popular way of displaying navigation when a page is scrolled, but you could run into a small detail that needs to be addressed.

Changing the Mouse Cursor in CSS With the cursor Property

Stories

Changing the Mouse Cursor in CSS With the cursor Property

Using CSS to change the cursor on your website is a great way to add a little bit of personality to your site.

How to Create a Pure CSS Gradient Background Animation

Stories

How to Create a Pure CSS Gradient Background Animation

CSS animations are a growing trend in web design, and they're also fairly easy to create.