CSS overflow controls what happens when content exceeds an element’s box, letting you hide it, clip it, or make it scrollable. You set it with the overflow property, which accepts values like visible, hidden, scroll, and auto, and you can fine-tune the horizontal and vertical axes separately with overflow-x and overflow-y. This article explains how each value behaves, how to choose the right one, and how to avoid the common pitfalls that trip up both beginners and experienced developers.
What Does the Overflow Property Do?
Every HTML element is a rectangular box. When its content—text, images, or child elements—is larger than the box’s specified width or height, that content “overflows” the box. By default, the overflow is visible, meaning the content spills out and is rendered outside the element’s boundaries. This is often fine for inline text, but it can break layouts when you have fixed-size containers like cards, modals, or table cells.
The overflow property gives you four main choices:
visible– content is not clipped and may render outside the box.hidden– content is clipped to the box and no scrollbar is provided.scroll– content is clipped, but a scrollbar is always shown (even if not needed).auto– content is clipped, and a scrollbar appears only when content overflows.
There’s also clip (similar to hidden but prevents programmatic scrolling) and overlay (deprecated, behaved like auto but with overlay scrollbars). For practical purposes, you’ll use the four main values.
How to Apply Overflow to an Element
You apply overflow directly to the container that has the fixed size. For example, to make a <div> with a set height scroll vertically:
.scroll-box {
height: 200px;
overflow-y: auto;
}
If you set both overflow-x and overflow-y to different values, be aware that if one axis is set to scroll or auto and the other is visible, the browser automatically computes the visible axis as auto. This is a common surprise: you can’t have one axis visible and the other scrollable—the visible one becomes auto, which may create an unexpected scrollbar.
Choosing Between hidden, scroll, and auto
The choice depends on the user experience you want:
- Use
hiddenwhen you want to completely cut off content that doesn’t fit—for example, a carousel slide that should only show the first image, or a text preview that ends with an ellipsis (thoughtext-overflowis better for single-line text). - Use
scrollwhen you want a consistent layout and always want the scrollbar visible, even if the content fits. This is rare but useful for custom scrollbar styling or when you want to prevent layout shift when content changes length. - Use
autofor the vast majority of cases—it shows a scrollbar only when needed, which is the most user-friendly default.
A common pattern is to set overflow: auto on a modal’s body so that long content scrolls while the modal header and footer stay fixed. Another is overflow: hidden on a hero section to prevent a large background image from causing a page-wide horizontal scrollbar.
Common Problems and Misconceptions
One frequent misconception is that overflow: hidden on the <body> or <html> element will prevent all scrolling. It does prevent scrolling on that element, but if a child element has its own scroll (e.g., a div with overflow: auto), the user can still scroll inside that child. To lock the whole page, you need to set overflow: hidden on both html and body, and even then, some mobile browsers may ignore it.
Another issue: overflow: hidden on a container does not always clip absolutely positioned children that are outside the container’s padding box—unless the container also has a position other than static (like relative). This is a subtle but important detail: if you have a dropdown menu that you want to clip, ensure the parent has position: relative.
Also, when you set overflow on a flex or grid item, the item’s content may shrink instead of overflow, because the default min-width: auto on flex items can be overridden. To force a flex child to overflow, set min-width: 0 or min-height: 0 on it.
How Overflow Interacts with Other CSS Properties
Overflow is closely tied to white-space, text-overflow, and word-wrap. For a single line of text that should be truncated with an ellipsis, you need three properties together:
.truncate {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
Without overflow: hidden, the ellipsis won’t appear. Without white-space: nowrap, the text will wrap and never overflow horizontally.
For multi-line truncation, you need display: -webkit-box and -webkit-line-clamp in addition to overflow: hidden. This is a non-standard but widely supported technique.
Also, setting overflow: hidden or auto on an element creates a new block formatting context (BFC). This can be useful to contain floats or prevent margin collapsing, but it can also affect how child elements behave—for example, a child with position: sticky may not stick if an ancestor has overflow: hidden.
Scrollbar Styling and Customization
When you use overflow: scroll or auto, the browser renders a scrollbar. You can style it with the ::-webkit-scrollbar pseudo-elements in Chrome, Safari, and Edge, and with the scrollbar-color and scrollbar-width properties in Firefox. For example, to make a thin dark scrollbar:
.custom-scroll {
scrollbar-width: thin;
scrollbar-color: #888 #f1f1f1;
}
.custom-scroll::-webkit-scrollbar {
width: 8px;
}
.custom-scroll::-webkit-scrollbar-thumb {
background: #888;
border-radius: 4px;
}
Remember that custom scrollbars can hurt usability if they’re too small or low-contrast, so always test with keyboard navigation and touch devices.
Overflow and Touch Devices
On touch devices, scrollbars are often hidden by default and only appear while scrolling. This means overflow: auto works fine, but users may not realize a container is scrollable. To hint at scrollability, you can add a fade-out gradient at the bottom or a visible scrollbar via CSS. Also, be aware that overflow: hidden on a touch device prevents the user from scrolling that container, but the page itself may still scroll if the content extends beyond the viewport.
Another touch-specific issue: overflow: scroll on a container does not prevent the page from scrolling when the user touches the container and drags—this is called scroll chaining. To prevent it, you can use overscroll-behavior: contain on the container. This property is widely supported and stops the scroll from propagating to the parent.
Frequently Asked Questions
Why is my overflow: hidden not working?
The most common reason is that the element does not have a constrained size. overflow: hidden only clips content that exceeds the element’s content box. If the element’s height is auto (the default), it will grow to fit the content, and there is nothing to clip. Set an explicit height, max-height, or width on the element. Also check that the element is not a flex item with min-height: auto—add min-height: 0 to allow it to shrink.
What’s the difference between overflow: auto and overflow: scroll?
auto shows a scrollbar only when the content overflows; scroll always shows the scrollbar (or a disabled one) even when the content fits. In practice, auto is almost always the better choice because it avoids unnecessary scrollbars that take up space and confuse users.
How do I prevent horizontal scrolling on my page?
Horizontal scrolling usually happens when an element is wider than the viewport—for example, a long unbroken string of text, a wide image, or a flex container with flex-wrap: nowrap. To fix it, set overflow-x: hidden on the body or html as a quick patch, but the real fix is to find and correct the offending element. Use your browser’s dev tools to identify which element extends beyond the viewport width.
Can I have overflow-x: hidden and overflow-y: visible?
No. If you set one axis to hidden or scroll and the other to visible, the browser will automatically change the visible axis to auto. This is per the CSS specification. If you need to hide horizontal overflow while allowing vertical overflow, you’ll need to use a wrapper element or accept that the vertical axis will become auto.
Does overflow: hidden affect SEO or accessibility?
No, search engines can still read hidden content, and screen readers can still access it because it remains in the DOM. However, if you hide content that is important for users to interact with (like a button that is clipped), that’s a usability problem. Use overflow: hidden only for decorative or non-essential content.
How do I make a div scrollable only when needed?
Set overflow: auto on the div and give it a fixed height or max-height. For example, max-height: 300px; overflow-y: auto; will show a vertical scrollbar only when the content exceeds 300 pixels. This is a common pattern for comment sections, dropdown menus, and chat boxes.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Fix Windows 10 Wi-Fi Not Connecting: Step-by-Step
- Scaling Headless Chrome: A Practical Guide
- Fix Windows Upgrade Error 0x80090011
- Fix Microsoft Store Apps Not Updating
- v0 Alternatives: Which Fits Your Web App Build?9.26 yd³
- Fix Hard Disk Error 0x80071ac3