Fixing Horizontal Scroll Caused by width: 100vw in CSS
A horizontal scrollbar appears with an element set to width: 100vw because 100vw is the width of the screen including the width of the vertical scrollbar. When the page gets long enough to show a vertical scrollbar, the element overflows to the right by that width. This happens easily when you want a hero image on the top page, or a background band behind a heading, to span the full width of the screen. If the band sits directly under body, you can fix it by setting width: 100% or by removing the width setting.
The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and headless Chromium 153. I set the screen to 800×600px, opened a page with a 100vw band in Playwright, and measured the element width and scrollWidth. When Playwright launches headless, it passes --hide-scrollbars to Chromium, so I removed that argument to show regular scrollbars that take up space (15px wide in this environment). I did not test Firefox, Safari, Windows or macOS browsers, or real smartphones.
- 1. The horizontal overflow appears at the same moment the vertical scrollbar does
- 2. Finding the element that overflows
- 3. Fix 1: Use width: 100%, or remove the width setting
- 4. Fix 2: To extend from inside a narrow content column, use 100cqw
- 5. Fix 3: Set scrollbar-gutter: stable on html
- 6. To hide the overflow, use overflow-x: clip on an outer div instead of body
- 7. In a screen that imitates a smartphone, nothing overflowed
The horizontal overflow appears at the same moment the vertical scrollbar does
I placed a 100vw band on a page and kept adding paragraphs to make the page longer. Only the following GIF was recorded with the screen at 600×300px.

On the second click, the page height became 309px against a screen height of 300px, and a vertical scrollbar (15px wide) appeared. At the same moment, the horizontal overflow width (the value of scrollWidth minus clientWidth) changed from 0px to 15px, and a horizontal scrollbar appeared at the bottom of the screen.
On an 800×600px screen, the numbers for a short page and a long page are as follows.
| Page | Band width | body width |
scrollWidth |
Horizontal scrollbar |
|---|---|---|---|---|
| Short (no scrollbar) | 800px | 800px | 800px | Not shown |
| Long (scrollbar shown) | 800px | 785px | 800px | Shown (15px high) |
On the long page, the width available for the content dropped to 785px, but the 100vw band stayed at 800px. When I sent a horizontal wheel action with Playwright, the page moved 15px sideways.
According to the MDN page on length units, vw is a unit equal to 1% of the viewport width. The CSS Values and Units Module Level 4 specification states that viewport units are sized as if no scrollbar were present, except when the root element's overflow or scrollbar-gutter means a scrollbar (or its space) is always shown. For overflow: auto, where a scrollbar appears only when the content needs it, the scrollbar width is not subtracted.
I also tried 100svw, 100dvw, and 100lvw. All of them became 800px on the long page and overflowed to the right, just like 100vw.
Finding the element that overflows
When there is a horizontal scrollbar somewhere on the page and you don't know which element causes it, run the following code in the browser's developer tools console. It lists the elements whose right edge goes past the content width of the page. In my test, I ran the same code inside the page from Playwright.
const limit = document.documentElement.clientWidth;
[...document.querySelectorAll('body *')]
.filter((el) => el.getBoundingClientRect().right > limit);
On the test page, only the 100vw band (div.band) was found, and its right edge was at 800px. This code finds only the elements that overflow to the right.
Fix 1: Use width: 100%, or remove the width setting
If the band sits directly under body and body has no left or right margin, you do not need 100vw. Both width: 100% and removing the width setting made the band 785px wide, the same as body, and no horizontal scrollbar appeared. A block element like a div expands to the full width of its parent when no width is set.
.band {
width: 100%; /* or remove the width setting */
}
Fix 2: To extend from inside a narrow content column, use 100cqw
If the content column is narrowed with max-width and you want only the band inside it to reach the screen edges, 100% only gives you the width of the parent. One approach is to combine 100vw with a negative margin.
.post {
max-width: 400px;
margin-inline: auto;
}
.full {
width: 100vw;
margin-left: calc(50% - 50vw);
}
On the long page, the left edge of the band was at −7.5px and the right edge at 792.5px, and scrollWidth was 793px. The band still overflowed to the right.
When I made body a container and used cqw instead of vw, nothing overflowed. According to the MDN page on container queries, cqw is a unit equal to 1% of the width of the nearest ancestor with container-type set to inline-size or size (the query container).
body {
container-type: inline-size;
}
.full {
width: 100cqw;
margin-left: calc(50% - 50cqw);
}
The width of body is 785px after excluding the scrollbar, so the left edge of the band was at 0px and the right edge at 785px, and scrollWidth was also 785px. Setting container-type on body did not change the position of a position: fixed element on the same page. After scrolling 500px, it stayed at the top of the screen.
For a layout that uses CSS grid columns to make an image span the full width while the text stays centered, see the article "How to Make Only an Image Span the Full Screen with CSS Grid While the Text Stays Centered" (in Japanese).
Fix 3: Set scrollbar-gutter: stable on html
The scrollbar-gutter section of the CSS Overflow Module Level 3 specification states that when stable is specified, space for a scrollbar that takes up room (the traditional type) is reserved in advance, whether or not the content overflows. Under the rule in the CSS Values and Units specification mentioned above, setting this on html also makes 100vw smaller by that amount. In the test environment, the 100vw band became 785px and did not overflow to the right.
html {
scrollbar-gutter: stable;
}
For other effects of scrollbar-gutter, see the article "How to Keep the Layout from Shifting with and without a Scrollbar in CSS" (in Japanese).
To hide the overflow, use overflow-x: clip on an outer div instead of body
You can also keep the band's width as it is and cut off the part that overflows. A common way is body { overflow-x: hidden; }, but the result depended on where the rule was set. In every case, the band stayed 800px wide.
| Where it is set | Horizontal scrollbar | Moves sideways with wheel | Moves sideways with scrollTo(30, 0) |
Sticky header |
|---|---|---|---|---|
| Not set | Shown | Moves 15px | Moves 15px | Stays at the top edge |
overflow-x: hidden on body |
Not shown | Does not move | Moves 15px | Stays at the top edge |
overflow-x: clip on body |
Not shown | Does not move | Moves 15px | Stays at the top edge |
overflow-x: hidden on both html and body |
Not shown | Does not move | Does not move | Scrolls away with the page |
overflow-x: hidden on an outer div |
Not shown | Does not move | Does not move | Scrolls away with the page |
overflow-x: clip on an outer div |
Not shown | Does not move | Does not move | Stays at the top edge |
When only body had the setting, the scrollbar disappeared, but the page width stayed at 800px, and calling JavaScript's scrollTo() moved the page 15px sideways. According to the CSS Overflow Module Level 3 specification, when the overflow of html is visible, the overflow of body is moved to the viewport (the browser's display area). A clip moved to the viewport is treated as hidden. This is why setting hidden or clip on body gave the same result. The MDN page on overflow also says that an element with hidden can still be scrolled by scrollTo() and similar methods, even without a visible scrollbar.
When both html and body had overflow-x: hidden, or when an outer div wrapping the whole page had overflow-x: hidden, the page no longer moved sideways. However, a header fixed at the top with position: sticky moved up with the page: after scrolling 500px, it was 500px above the top of the screen (top was −500px). In both cases, the computed value of overflow-y became auto, so the element wrapping the header became a box with its own scrolling mechanism. The MDN page on position says that a sticky element sticks to its nearest ancestor that has a scrolling mechanism, even when that ancestor is not actually scrolled.
Only when the outer div wrapping the whole page had overflow-x: clip did the page not move sideways, and the header stayed at the top edge. The computed value of overflow-y remained visible.
<body>
<div class="page">
<header>…</header>
<div class="band">…</div>
…
</div>
</body>
.page {
overflow-x: clip;
}
overflow-x: hidden and clip only cut off the overflowing part so it is not visible, and the band stays 800px wide. If you want the band's width itself to match the width of the page content, use Fixes 1 to 3.
In a screen that imitates a smartphone, nothing overflowed
I used Playwright's devices["Pixel 7"] to imitate a smartphone screen and opened the same long page. The screen width was 412px, and clientWidth, the band width, and scrollWidth were all 412px, so nothing overflowed. In this screen, the vertical scrollbar did not take up any of the page width.
The overflow from 100vw happens on screens where a scrollbar that takes up space is shown. So to check whether a fix works, open a page longer than the screen in that kind of browser.





