--- url: "https://web.dev/learn/css/overflow" titulek: "Overflow | web.dev" h1: "Overflow Stay organized with collections Save and categorize content based on your preferences." popis: "Overflow is how you deal with content that doesn't fit in a set parent size. In this module, you'll think outside the box, and learn how to style overflowing content." jazyk: "en" kanonicka: "https://web.dev/learn/css/overflow" drobecky: ["web.dev", "Resources", "CSS"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 01:49:27" znaku: 12802 slov: 1879 kodovani: "iso-8859-2" sha256: "58a0cd4fb3b195dd60b26296761d994bb116534b42a5c7eed079c3ebe0b02d9d" --- Overflow Stay organized with collections Save and categorize content based on your preferences. [The CSS Podcast - 034: Overflow](https://thecsspodcast.libsyn.com/034-overflow) When content extends beyond its parent, there are many options for how you can handle it. You can scroll to add additional space, clip the overflowing edges, indicate the cut-off with an ellipsis, and so much more. Overflow is especially important to consider when developing for phone applications and multiple screen sizes. There are two different clipping options in CSS; `text-overflow` will help with individual lines of text, and the `overflow` properties will help control overflow in the box model. Single line overflow with `text-overflow` Use the [`text-overflow`](https://developer.mozilla.org/docs/Web/CSS/text-overflow) property on any element that contains text node(s), for example a paragraph, `

`. It specifies how the text appears when it doesn't fit in the available space of the element. All viewable HTML text on a page is in [text nodes](https://developer.mozilla.org/docs/Web/API/Text). To use `text-overflow` you need a single unwrapped line of text, so you must also set `overflow` to `hidden` and have a `white-space` value that prevents wrapping. `p { text-overflow: ellipsis; overflow: hidden; white-space: nowrap; }` Using overflow properties Overflow properties are set on an element to control what happens when its children need more space than it has available. This can be intentional, like in an interactive map like Google Maps, where a user pans around a large image clipped to a specific size. It can also be unintentional like in a chat application where the user types a lengthy message that doesn't fit in the text bubble. You can think of the overflow in two parts. The parent element has a firmly constrained space that won't change. You can think of this as a window. The child elements are content that want more space from the parent. You can think of this as what you are looking through the window at. Managing overflow will help guide how the window frames this content. Scrolling on the vertical and horizontal axis The `overflow-y` property controls physical overflow along the vertical axis of the device viewport, therefore scrolling up and down. The `overflow-x` property controls overflow along the horizontal axis of the device viewport, therefore scrolling left and right. Logical properties for scroll direction Browser Support 135 135 69 26 [Source](https://developer.mozilla.org/docs/Web/CSS/Reference/Properties/overflow-inline) The [`overflow-inline`](https://developer.mozilla.org/docs/Web/CSS/overflow-inline) and [`overflow-block`](https://developer.mozilla.org/docs/Web/CSS/overflow-block) properties set the overflow based on the document direction and writing mode. The `overflow` shorthand The [`overflow`](https://developer.mozilla.org/docs/Web/CSS/overflow) shorthand sets both `overflow-x` and `overflow-y` styles in one line: `overflow: hidden scroll;` If two keywords are specified, the first applies to `overflow-x` and the second to `overflow-y`. Otherwise, both `overflow-x` and `overflow-y` use the same value. Values Take a closer look at the [values and keywords](https://developer.mozilla.org/docs/Web/CSS/overflow#values) available for the `overflow` properties. `overflow: visible` (default) Without setting the property, `overflow: visible` is the default value for the web. This ensures that content is never unintentionally hidden and follows the core tenets of "never hide content" or "safe layouts of precise layouts". `overflow: hidden` With `overflow: hidden` content is clipped in the specified direction, and no scrollbars are provided to show it. `overflow: scroll` `overflow: scroll` enables scrollbars to allow users to scroll through content. Even if content isn't overflowing, scrollbars will be present. This is a great way to reduce future layout shift if a container may be scrollable in the future based, for example, on resize, and visually prepare the user for scrollable areas. `overflow: clip` Like `overflow: hidden`, the content with `overflow: clip` is clipped to the element's padding box. The difference between `clip` and `hidden` is that the `clip` keyword also forbids all scrolling, including programmatic scrolling. `overflow: auto` Finally, the value most commonly used, `overflow: auto`. This respects the user's preferences and shows scrollbars if needed, but hides them by default, and gives responsibility for scrolling to the user and browser. Scrolling and overflow Many of these `overflow` behaviors introduce a scrollbar, but there's a few specific scroll behaviors and properties that can help you control scrolling on your overflow container. Scrolling and accessibility It's important to make sure that the scrollable area can accept focus so that a keyboard user can tab to the box, then use the arrow keys to scroll. To allow a scrolling box to accept focus add `tabindex="0"`, a name with the [`aria-labelledby`](https://developer.mozilla.org/docs/Web/Accessibility/ARIA/Attributes/aria-labelledby) attribute, and an appropriate `role` attribute. For example: `

content
` CSS can then be used to indicate that the box has focus, using the `outline` property to give a visual clue that it will now be scrollable. In [Using CSS to Enforce Accessibility](https://adrianroselli.com/2021/06/using-css-to-enforce-accessibility.html) Adrian Roselli demonstrates how CSS can help prevent accessibility regressions. For example, to only turn on scrolling and add the focus indicator if the correct attributes are used. The following rules will only make the box scrollable if it has a `tabindex`, `aria-labelledby`, and `role` attribute. `[role][aria-labelledby][tabindex] { overflow: auto; } [role][aria-labelledby][tabindex]:focus { outline: .1em solid blue; }` Scrollbar positioning within the box model Scrollbars take up space within the padding box and can compete for space if `inline` and not `overlaid`. The [box model module](/learn/css/box-model#the_areas_of_the_box_model) expands more on this potential source of layout shift. root-scroller versus implicit-scroller You may notice that some scrollers have a pull-to-refresh behavior and other special behaviors, especially when developing for mobile and hybrid applications. This scroll behavior happens on the root scroller. There is only ever one root scroller on a page. By default, the [documentElement](https://developer.mozilla.org/docs/Web/API/Document/documentElement) is the page's root scroller, however, by changing which element is the root scroller, the special behaviors can be applied to scrollers other than the documentElement, we call this new scroller the implicit root scroller. To create a root scroller, you can use something called *scroller promotion* by positioning a container as fixed, ensuring it covers the entire viewport and is z-index on top with a scroller. Experience a root scroller versus a nested implicit scroller [here](https://cdpn.io/web-dot-dev/debug/dyzPzwz). The video shows a root scroller with bounce behavior and new styling features, compared to scrolling an implicit scroller with no enhanced scroll behavior. Styling your scrollbar You can style your scrollbar to incorporate it into your site's design. `scrollbar-color` sets the color for the scrollbar's thumb and gutter. To change the width of the scrollbar, use `scrollbar-width`. You can't set this to an arbitrary length, but you can specify that you want a `thin` scrollbar or `none`. scroll-behavior Browser Support 61 79 36 15.4 [Source](https://developer.mozilla.org/docs/Web/CSS/Reference/Properties/scroll-behavior) `scroll-behavior` lets you opt into browser-controlled scrolling to elements. This lets you specify how in-page navigation like `.scrollTo()` or links are handled. This is especially useful when used with [prefers-reduced-motion](https://developer.mozilla.org/docs/Web/CSS/@media/prefers-reduced-motion) to specify scroll behavior based on user preference. `@media (prefers-reduced-motion: no-preference) { .scroll-view { scroll-behavior: auto; } }` overscroll-behavior Browser Support 144 18 150 16 [Source](https://developer.mozilla.org/docs/Web/CSS/Reference/Properties/overscroll-behavior) If you've ever reached the end of a modal overlay, then continued scrolling and had the page behind the overlay move, this is the scroll chaining, or bubbling up to the parent scroll container. The [`overscroll-behavior`](https://developer.mozilla.org/docs/Web/CSS/overscroll-behavior) property lets you prevent overflow scrolling leaking into a parent container (called scroll chaining). Scroll snapping Scrolling generally is smooth, allowing you to position the content within the [scrollport](https://developer.mozilla.org/docs/Glossary/Scroll_container#scrollport) wherever you want. For some designs, like image galleries or content that emulates pages or slides, you may want to make the content snap to the scrollport. Setting up the scroll container To enable scroll snapping, add the `scroll-snap-type` to the scroll container. You'll first define which axis the scroll snapping will happen on. This can be a logical property (`block` or `inline`), a physical property (`x` or `y`), or `both`. You can also define how strict scroll snapping should be. The default strictness is `proximity`, which means that the scroll container will try to snap if possible. You can also set the strictness to `mandatory` to make sure that the scroll container will always snap. `.scroll-container { scroll-snap-type: block mandatory; }` Scroll snapping aligns an element within the full bounds of the scroll container, but what happens if part of the scroll container is not visible? For example, you might have a fixed header overlaying part of the scroll container. To help align the snapped elements to the visible portion of the scroll container, you can set `scroll-padding`. Controlling the snappable elements To make an element snappable, set the `scroll-snap-align` property to `start`, `end` or `center`. If the scroll snap direction is `both`, you can set two values. This sets whether an edge of the element will align with the scrollport's edge, or if it will be centered. You can adjust the spacing around the edges of the snapped element with `scroll-margin`: `scroll-margin` also is used to set the padding when scrolling to an element: To make the scrolling more sticky, you can add `scroll-snap-stop: always` to an item in a scroll container. It does not prevent you from scrolling past multiple items in a single scroll. If you end a scrolling motion in a way that the scroll would continue with inertia, the scroll will end at the next snap position, instead of continuing past. Check your understanding Text overflow and element overflow are the same? true Text overflow is special when compared to element overflow. false Text overflow is generally about inline overflow, where element overflow is about block overflow. The `overflow` property accepts 2 keywords, the first keyword is for which axis? horizontal 🎉 vertical Almost always, when passing two shorthand values, the first is horizontal. Which space in the box model do scrollbars consume when showing and inline? content box Try again! padding box Scrollbars in `overlay` mode will overlap the padding, and when in `inline` mode will add to the padding. border box Try again! margin box Try again! To trap extra momentum from scrolling in a nested implicit scroller, which property would you use? `scroll-behavior` Try again! `scroll-hint` Try again! `overscroll-behavior` Setting this property to `contain` will trap scrolling. `scroll-padding` Try again! `overscroll-effect` Try again! What value declares that a scroll container must stop at a snapped element if possible? `required` Incorrect. `mandatory` Correct! `0px` Incorrect. `proximity` Incorrect. What are valid values for `scrollbar-width`? `5px` Incorrect. `thin` Correct! `medium` Incorrect. `none` Correct! Resources [Overflow And Data Loss In CSS from Smashing Magazine](https://www.smashingmagazine.com/2019/09/overflow-data-loss-css/) [Previous arrow_back View Transitions for SPAs](/learn/css/view-transitions-spas) [Next Backgrounds arrow_forward](/learn/css/backgrounds) Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2025-08-21 UTC.