41
loading...
This website collects cookies to deliver better user experience
“overflow-x: hidden”
in the body
tag to fix it.When you set an element's width to 100vw, the element's width now will be the whole view width of the browser and the important part is 100vw does not include the vertical scrollbar's width into its calculation at all. Therefore, when there is a vertical scrollbar, the total width will be the sum of the element's width and the vertical scrollbar's width, which causes the overflow on the x-axis and thus the horizontal scrollbar.
Tepy Thai, Why 100vw causes horizontal scrollbaroverflow-x: hidden
from the body
. They were trying to build a feature that uses position: sticky
and as it turns out, there is a ticket reporting that position sticky inside overflow hidden doesn’t work.//remove small horizontal scrollbar when a block is full bleed
var scrollbarWidth = window.innerWidth - document.body.clientWidth;
var halfScrollbarWidth = scrollbarWidth / 2;
document.body.style.setProperty('--scrollbarWidth', `${scrollbarWidth}px`);
document.body.style.setProperty('--halfScrollbarWidth', `${halfScrollbarWidth}px`);
margin-left
I needed to take into account the possible existence of the scrollbar. After some poking, it looked like half of the width of the scrollbar would fix that.--viewportWidth: calc(100vw - var(--scrollbarWidth));
/* finalHalfScrollbar: value must be negative */
--finalHalfScrollbar: calc(var(--halfScrollbarWidth) * -1);
width: 100%;
width: calc(100vw - 15px);
width: var(--viewportWidth);
margin-left: 0;
margin-left: calc(50% - 50vw - var(--finalHalfScrollbar, -7px));