Overflow and Scroll Containers
What happens when content is bigger than its box, how hidden, clip, auto and scroll differ, and why overflow quietly changes layout and breaks position sticky.
Overflow and Scroll Containers
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- By default content that is too big for its box overflows visibly. CSS does not clip or scroll it unless you set `overflow`.
- `overflow: hidden`, `auto` and `scroll` all turn the element into a scroll container, even `hidden`, which can still be scrolled with JavaScript or by focusing an element inside. `overflow: clip` clips without creating a scroll container.
- Setting `overflow` on one axis to anything but `visible` makes a `visible` value on the other axis compute to `auto`. You cannot clip one axis and let the other spill.
- A non-`visible` overflow value also creates a block formatting context, which can contain floats and stop margin collapsing, and it clips box shadows and focus outlines at the edge.
- `overscroll-behavior: contain` stops scrolling from chaining to the page behind a modal or panel, and `scrollbar-gutter: stable` reserves space so layout does not jump when a scrollbar appears.
Content does not care about your box
You set height: 100px on a card and put three paragraphs in it. The box stays 100px tall; the text carries on drawing below the bottom edge, over whatever comes next. CSS does not shrink the content or grow the box for you when you force a size. The overspill is called overflow, and the overflow property is how you tell the browser what to do with it.
The values
visible(default): overflow is painted outside the box.hidden: anything outside the padding box is clipped and not reachable by scrollbar.clip: the same clipping, with fewer side effects, below.scroll: always shows scrollbars (on classic-scrollbar systems), scrolling when needed.auto: scrollbars only when the content actually overflows. This is the one you usually want for a scrollable region.
The property is two properties: overflow-x and overflow-y. overflow: hidden auto sets x then y.
A scroll container is a real thing
Any element with overflow of hidden, scroll or auto becomes a scroll container. That is not just about scrollbars, it changes behavior: the element has its own scroll position, and descendants are positioned and stuck relative to it.
That includes hidden. People assume hidden means “no scrolling”. It means “no user-visible scrollbar”, and the content can still scroll when the browser scrolls a focused element into view, or when you set scrollTop in JavaScript. I set scrollTop = 100 on an overflow: hidden box and an overflow: clip box in Chromium: the hidden one moved to 100, the clipped one stayed at 0.
The side effects of hidden
Because hidden makes a scroll container, it brings baggage:
- It breaks
position: stickyfor descendants. A sticky header inside anoverflow: hiddenwrapper never sticks, since the wrapper is its scroll container and does not scroll. In my test the header scrolled away with the page; withoverflow: clipon the same wrapper it stayed pinned. See positioning and sticky. - It creates a block formatting context, which can contain floats and stop margin collapse, as in margin collapsing. That is why
overflow: hiddenbecame a common fix for float containment, often unintentionally. - It clips shadows and outlines that extend past the edge, including focus rings on children, which is an accessibility problem.
- The other axis changes.
overflow-x: hiddenwithoverflow-y: visibleis not allowed. Thevisibleis forced toauto. I confirmed this: Chromium reportshiddenandauto. If you wanted to clip horizontally while letting content spill vertically, the standard cannot give you that.
overflow: clip
clip cuts off painting at the box edge and does nothing else. No scroll container, no scrolling by any means, no BFC, so sticky keeps working and the other axis is not altered. It can also take an offset with overflow-clip-margin to clip some distance beyond the edge. It is supported in Chrome 90, Firefox 81 and Safari 16, according to MDN’s compatibility data. The video’s claim, from its description, is that clip is “basically a better version of overflow: hidden”, and that contain: paint is another option for some cases, with extra limitations but potentially better performance.
Use clip when you want to cut things off, and auto when you want a scrollable region.
Making a scrollable region well
.panel {
max-height: 20rem;
overflow-y: auto;
overscroll-behavior: contain;
scrollbar-gutter: stable;
}
max-heightrather thanheight, so a short panel stays short.overscroll-behavior: containstops scroll chaining: when the panel hits its end, the wheel or touch gesture does not carry on scrolling the page behind it.scrollbar-gutter: stablereserves the scrollbar’s width even when no scrollbar is showing, so content does not jump sideways when one appears. It is supported in Chrome 94, Firefox 97 and Safari 18.2.
Keyboard users have to be able to scroll a scroll container. Older browsers needed tabindex="0" on a scrollable region with no focusable content inside it, and newer ones make such regions focusable themselves, so test in the browsers you support. The rest of the accessibility picture is in accessible CSS.
Wide content and the sideways-scroll bug
A table, code block or image wider than the screen makes the whole page scroll horizontally. Don’t fix it with overflow-x: hidden on body; that hides the symptom and breaks sticky. Constrain the culprit instead:
.table-wrap { overflow-x: auto; }
pre { overflow-x: auto; max-width: 100%; }
img { max-width: 100%; height: auto; }
Then only the wide thing scrolls, and the layout around it stays intact. If a flex or grid child refuses to shrink, min-width: 0 on that child is often the fix, which the Flexbox group returns to. Overflowing text has its own tools in text wrapping and truncation.
What the video covers
Kevin Powell’s “2 better alternatives to overflow: hidden” runs about 11 minutes and is from December 2023. From its description it explains the odd side effects of overflow: hidden, offers overflow: clip as the fix, and suggests contain: paint where its limits are acceptable. It links an article by Kilian from Polypane on overflow: clip, and is sponsored with a Polypane affiliate link. The behaviors and measurements above are my own tests; I could not read a transcript. overscroll-behavior and scrollbar-gutter are not covered in it by its description.
Where this fits
Overflow connects to nearly everything before it: it creates formatting contexts, it breaks sticky, and in an unconstrained flow it is why a box with fixed height misbehaves. After this, sizing images and replaced elements takes the last class of boxes whose size comes from outside the CSS.
Check yourself
What is the practical difference between `overflow: hidden` and `overflow: clip`?
Both clip painting at the box's edge. `hidden` also makes the element a scroll container, so it can be scrolled programmatically (`scrollTop` works), creates a block formatting context, and breaks `position: sticky` descendants. `clip` does none of that. I set `scrollTop = 100` on both and only the `hidden` one moved.
You write `overflow-x: hidden` and leave `overflow-y` alone. What do you get on the y axis?
`overflow-y` computes to `auto`, because `visible` cannot be combined with a non-visible value on the other axis. The element may show a vertical scrollbar when content is taller than the box. I checked this in Chromium.
A wide table breaks a mobile layout. What is the standard fix that keeps the page itself from scrolling sideways?
Wrap the table in an element with `overflow-x: auto` (and a constrained width, such as the parent's). Only the wrapper scrolls horizontally, and the rest of the page stays put.
A chat panel scrolls to its end and then the page behind it starts scrolling. Which property stops that?
`overscroll-behavior: contain` on the panel. It prevents scroll chaining, so scrolling that reaches the end of the panel does not continue into the parent or page.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.