CSS overflow Property
The CSS overflow property controls how content is displayed when it extends beyond an element's padding box.
It determines whether overflowing content remains visible, is clipped, or can be accessed through scrolling. The property is a shorthand for overflow-x and overflow-y, allowing horizontal and vertical overflow to be controlled together or separately.
overflow Syntax
The overflow property accepts one or two overflow keywords. A single value applies to both axes. With two values, the first controls horizontal overflow and the second controls vertical overflow.
Basic Syntax
selector {
overflow: auto;
}
Single-Value Syntax
.box {
overflow: visible;
}
.box {
overflow: hidden;
}
.box {
overflow: clip;
}
.box {
overflow: scroll;
}
.box {
overflow: auto;
}
Two-Value Syntax
.box {
overflow: hidden auto;
}
The first value controls the horizontal axis, and the second controls the vertical axis.
CSS-Wide Keywords
.box {
overflow: initial;
}
.box {
overflow: inherit;
}
.box {
overflow: unset;
}
.box {
overflow: revert;
}
.box {
overflow: revert-layer;
}
The initial value is visible for both axes, allowing content to extend beyond the element's boundaries.
Property Values
| Value | Description |
|---|---|
visible |
Overflowing content is not clipped and may extend beyond the element's padding box. This is the initial value. |
hidden |
Overflowing content is clipped. User scrolling is not available, but programmatic scrolling may still be possible. |
clip |
Overflowing content is clipped at the overflow clip edge. Scrolling is not supported for that clipped axis. |
scroll |
Overflowing content is clipped, and scrolling is enabled. Scrollbar presentation depends on the browser and operating system. |
auto |
Allows scrolling when necessary. Scrollbars are generally displayed only when needed, depending on the platform. |
initial |
Resets both overflow axes to visible. |
inherit |
Uses the computed overflow values of the parent element. |
unset |
Resets both axes to visible because overflow is not inherited. |
revert |
Reverts the property according to cascade origin rules. |
revert-layer |
Reverts the property according to cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | visible for both axes |
| Inherited | No |
| Applies to | Block containers, flex containers, and grid containers |
| Computed value | Specified keywords, with adjustments for certain combinations of axis values |
| Animation type | Discrete |
| Shorthand for | overflow-x and overflow-y |
Valid Values
.box {
overflow: visible;
}
.box {
overflow: hidden;
}
.box {
overflow: clip;
}
.box {
overflow: scroll;
}
.box {
overflow: auto;
}
.box {
overflow: hidden auto;
}
.box {
overflow: auto scroll;
}
Invalid Values
.box {
overflow: none;
}
.box {
overflow: 20px;
}
.box {
overflow: hidden auto scroll;
}
The property accepts supported overflow keywords, not arbitrary lengths or more than two axis values.
How It Works
Overflow occurs when content extends beyond the space available inside an element. This may happen because of fixed dimensions, long text, wide images, or nested elements.
Creating an Overflowing Container
.box {
width: 250px;
height: 100px;
padding: 15px;
border: 2px solid #64748b;
}
If the content requires more than the available height, it extends beyond the container when the overflow value remains visible.
Controlling Overflow
.box {
width: 250px;
height: 100px;
overflow: auto;
}
With auto, the browser provides scrolling when content exceeds the available space.
Overflow and Container Dimensions
For vertical overflow to produce a useful scrollable region, the container generally needs a constrained height or block size. Horizontal overflow typically requires a constrained width or inline size.
Overflow Is Not Always Caused by Text
Images, tables, code blocks, and flex or grid children can also extend beyond their containers.
Important Consideration
Setting overflow does not automatically solve every layout problem. The element's dimensions and its children's sizing rules also affect whether content overflows.
visible
The visible value allows content to extend beyond the element's padding box without clipping.
Basic Example
.box {
width: 250px;
height: 100px;
overflow: visible;
}
Content that exceeds the box remains visible outside its boundaries.
Default Behavior
.box {
overflow: visible;
}
This is the initial behavior for both overflow axes.
When visible Is Useful
- Allowing decorative elements to extend beyond a container.
- Displaying content without introducing an internal scrolling region.
- Preserving overflowing content when clipping is unnecessary.
Important Consideration
Overflowing content may overlap adjacent elements. A visible value on one axis can also compute differently when the other axis uses a scrolling or hidden overflow value.
hidden
The hidden value clips content that extends beyond the padding box. It does not provide a user-operated scrollbar, but the element can still be scrolled programmatically.
Basic Example
.box {
width: 250px;
height: 100px;
overflow: hidden;
}
Clipping Wide Content
.box {
width: 250px;
overflow: hidden;
}
Content extending horizontally beyond the box is clipped.
Programmatic Scrolling
const box = document.querySelector(".box");
box.scrollTop = 100;
An element using overflow: hidden can still be scrolled through JavaScript when it contains scrollable overflow.
Common Uses
- Clipping content within a fixed-size component.
- Concealing overflow during certain interface effects.
- Creating a scroll container without user-operated scrollbars.
Important Consideration
Hidden content may become inaccessible to users if there is no way to reveal it. Avoid clipping important text or controls without providing an appropriate alternative.
clip
The clip value clips overflowing content without creating a scroll container. Unlike hidden, it does not permit programmatic scrolling along the clipped axis.
Basic Example
.box {
width: 250px;
height: 100px;
overflow: clip;
}
Controlling the Clip Edge
.box {
overflow: clip;
overflow-clip-margin: 10px;
}
The overflow-clip-margin property can extend the clipping edge beyond the padding box for applicable elements.
clip vs. hidden
| Behavior | hidden | clip |
|---|---|---|
| Clips overflowing content | Yes | Yes |
| Provides user scrollbars | No | No |
| Supports programmatic scrolling on the clipped axis | Yes | No |
| Creates a scroll container by itself | Yes | No |
| Establishes a new formatting context by itself | Yes | No |
Using clip with flow-root
.box {
display: flow-root;
overflow: clip;
}
This combination clips overflow while establishing a new block formatting context.
Important Consideration
When clip is combined with a scrolling or hidden value on the other axis, its computed behavior can change to hidden.
scroll
The scroll value clips overflowing content and establishes a scroll container. Traditional scrollbars may remain visible even when the content fits, while overlay scrollbar systems may display them differently.
Basic Example
.box {
width: 250px;
height: 120px;
overflow: scroll;
}
Vertical Scrolling
.box {
max-height: 200px;
overflow-y: scroll;
}
Horizontal Scrolling
.box {
width: 300px;
overflow-x: scroll;
}
Common Uses
- Scrollable code or data panels.
- Containers that need a persistent scrolling mechanism.
- Interfaces where consistent scrollbar allocation is useful.
Important Consideration
Scrollbar appearance depends on the browser and operating system. Do not assume that overflow: scroll always produces permanently visible scrollbar tracks.
auto
The auto value allows the browser to provide scrolling when content exceeds the available space. It is a common choice for scrollable panels and containers.
Basic Example
.box {
width: 250px;
max-height: 200px;
overflow: auto;
}
Scrollable Code Block
.code-panel {
max-width: 100%;
overflow-x: auto;
}
Scrollable Content Panel
.content-panel {
height: 250px;
overflow: auto;
}
auto vs. scroll
| Feature | auto | scroll |
|---|---|---|
| Creates a scroll container | Yes | Yes |
| Allows scrolling when content overflows | Yes | Yes |
| Traditional scrollbar display | Generally when needed | May reserve space even when not needed |
| Common use | Conditional scrolling | Explicit scrollable regions |
Important Consideration
Use auto when scrolling should become available only as needed. The container still requires appropriate size constraints for overflow to occur.
Two-Value Syntax
The overflow shorthand can accept two values to control horizontal and vertical overflow independently.
Syntax
selector {
overflow: horizontal vertical;
}
The words horizontal and vertical above indicate value positions, not literal CSS keywords.
Hidden Horizontal, Automatic Vertical
.box {
overflow: hidden auto;
}
Automatic Horizontal, Hidden Vertical
.box {
overflow: auto hidden;
}
Scrollable Horizontal and Vertical
.box {
overflow: scroll auto;
}
Equivalent Longhand Declarations
.box {
overflow-x: hidden;
overflow-y: auto;
}
This is equivalent to overflow: hidden auto.
Computed Value Adjustments
When one axis uses hidden, scroll, or auto, a specified visible on the other axis computes to auto, while a specified clip computes to hidden.
.box {
overflow: visible auto;
}
In this example, the horizontal visible value computes to auto.
Important Consideration
These computed value rules mean that certain combinations do not behave as independent visible and scrolling axes. Check the computed styles when using mixed values.
Horizontal and Vertical Overflow
The overflow-x and overflow-y properties provide direct control over overflow along the horizontal and vertical axes.
Horizontal Overflow
.table-wrapper {
max-width: 100%;
overflow-x: auto;
}
This pattern is useful for wide tables and other content that cannot fit within a narrow viewport.
Vertical Overflow
.panel {
max-height: 300px;
overflow-y: auto;
}
The panel becomes vertically scrollable when its content exceeds the maximum height.
Both Axes
.panel {
width: 300px;
height: 200px;
overflow: auto;
}
Using Logical Overflow Properties
.panel {
overflow-inline: auto;
overflow-block: hidden;
}
The logical properties overflow-inline and overflow-block follow the element's writing mode rather than always referring to physical horizontal and vertical directions.
Important Consideration
Horizontal overflow can make content difficult to use on smaller screens. Prefer responsive sizing where practical, and provide scrolling when wide content must remain intact.
Scroll Containers
An element becomes a scroll container when its computed overflow behavior supports scrolling, such as auto, scroll, or hidden.
Creating a Scroll Container
.scroll-panel {
height: 250px;
overflow-y: auto;
}
Scrolling to an Element
const item = document.querySelector("#target-item");
item.scrollIntoView({ behavior: "smooth", block: "nearest" });
When applicable, the browser scrolls relevant scroll containers to bring the target into view.
Programmatic Scrolling
const panel = document.querySelector(".scroll-panel");
panel.scrollTop = 150;
Scroll Container vs. Clipped Container
overflow: hidden creates a scroll container that can be scrolled programmatically. overflow: clip does not create a scroll container by itself and does not allow scrolling along the clipped axis.
Important Consideration
Nested scroll containers can make navigation more difficult. Use them when the layout requires independent scrolling regions rather than as a default for every component.
Scrollbars
Scrollbar visibility depends on the overflow value, content dimensions, browser, and operating system.
Automatic Scrollbars
.box {
overflow: auto;
}
Explicit Scrollbars
.box {
overflow: scroll;
}
Controlling Scrollbar Space
.box {
overflow: auto;
scrollbar-gutter: stable;
}
The scrollbar-gutter property can reserve space for classic scrollbars and help reduce layout changes when they appear.
Scrollbar Styling
.box {
overflow: auto;
scrollbar-width: thin;
scrollbar-color: #64748b #e2e8f0;
}
Scrollbar styling properties can customize supported scrollbar appearances, although platform rendering may vary.
Important Consideration
Do not rely on scrollbar visibility alone to communicate that content can be scrolled. Some systems use overlay scrollbars that appear only during interaction.
Text Overflow
The overflow property controls clipping and scrolling, but it does not automatically add an ellipsis to truncated text.
Clipping Text
.text-box {
width: 200px;
overflow: hidden;
white-space: nowrap;
}
Adding an Ellipsis
.text-box {
width: 200px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
The text-overflow property controls how clipped inline text is represented. The combination above is commonly used for a single-line ellipsis.
Preserving Long Text
.text-box {
overflow-wrap: anywhere;
}
Allowing long words or strings to wrap can prevent unwanted horizontal overflow.
Important Consideration
Truncation may hide meaningful information. Consider providing the complete text through an accessible interface when the content is important.
Layout Behavior
Overflow can influence formatting contexts, scrolling behavior, and the way elements interact with surrounding content.
Block Formatting Context
.container {
overflow: auto;
}
For block containers, overflow values other than visible and clip generally establish a new block formatting context. This can affect float containment and margin behavior.
Using flow-root Instead
.container {
display: flow-root;
}
If the goal is only to establish a block formatting context, display: flow-root is often clearer than using overflow as a layout workaround.
Overflow in Flex Layouts
.flex-item {
min-width: 0;
overflow: auto;
}
Flex items have automatic minimum sizing behavior that can prevent them from shrinking as expected. Adjusting minimum sizes may be necessary when handling overflowing content.
Overflow in Grid Layouts
.grid-container {
display: grid;
grid-template-columns: minmax(0, 1fr);
}
.grid-item {
min-width: 0;
overflow-x: auto;
}
This pattern helps a grid item contain wide content without forcing the grid track to expand beyond its intended size.
Clipping and Positioned Content
Overflow clipping can affect positioned descendants, focus outlines, shadows, and decorative elements. The exact clipping behavior depends on the element's positioning and containing block relationships.
Important Consideration
Before using overflow: hidden to fix unwanted scrolling, identify what is causing the overflow. Correcting the underlying sizing problem is often preferable to clipping content.
Accessibility Considerations
Scrollable and clipped content should remain usable with keyboards, touch input, assistive technologies, and different viewport sizes.
Keyboard-Accessible Scroll Regions
<div class="scroll-region" tabindex="0" role="region" aria-label="Scrollable examples">
<p>Scrollable content goes here.</p>
</div>
.scroll-region {
max-height: 200px;
overflow: auto;
}
.scroll-region:focus-visible {
outline: 3px solid #2563eb;
outline-offset: 2px;
}
A focusable scroll region can help keyboard users reach and scroll content. Use an appropriate accessible name, and avoid adding unnecessary tab stops when the region is already keyboard-accessible.
Avoid Hiding Important Content
Do not use overflow: hidden or overflow: clip to conceal essential information or interactive controls without an accessible way to reach them.
Support Zoom and Reflow
Test scrollable containers at increased browser zoom and on narrow screens. Content should remain readable and operable without unnecessary two-dimensional scrolling.
Preserve Focus Visibility
Overflow clipping can hide focus indicators that extend outside a container. Verify that keyboard focus remains visible throughout the interface.
Important Consideration
Scrollable regions should have clear boundaries and appropriate dimensions. Avoid creating many nested scrolling areas that make navigation difficult.
overflow Example
This example compares the five main overflow values using containers with fixed dimensions and content that exceeds the available height.
HTML Example
<div class="overflow-demo">
<div class="overflow-item">
<h3>visible</h3>
<div class="overflow-box overflow-visible">
<p>First paragraph of content.</p>
<p>Second paragraph of content.</p>
<p>Third paragraph of content.</p>
</div>
</div>
<div class="overflow-item">
<h3>hidden</h3>
<div class="overflow-box overflow-hidden">
<p>First paragraph of content.</p>
<p>Second paragraph of content.</p>
<p>Third paragraph of content.</p>
</div>
</div>
<div class="overflow-item">
<h3>clip</h3>
<div class="overflow-box overflow-clip">
<p>First paragraph of content.</p>
<p>Second paragraph of content.</p>
<p>Third paragraph of content.</p>
</div>
</div>
<div class="overflow-item">
<h3>scroll</h3>
<div class="overflow-box overflow-scroll">
<p>First paragraph of content.</p>
<p>Second paragraph of content.</p>
<p>Third paragraph of content.</p>
</div>
</div>
<div class="overflow-item">
<h3>auto</h3>
<div class="overflow-box overflow-auto">
<p>First paragraph of content.</p>
<p>Second paragraph of content.</p>
<p>Third paragraph of content.</p>
</div>
</div>
</div>
CSS Example
.overflow-demo {
display: flex;
flex-wrap: wrap;
gap: 65px 24px;
padding: 20px;
background-color: #f1f5f9;
}
.overflow-item {
width: 170px;
}
.overflow-item h3 {
margin: 0 0 10px;
font-size: 16px;
}
.overflow-box {
box-sizing: border-box;
height: 100px;
padding: 10px;
border: 2px solid #64748b;
background-color: #fff;
color: #334155;
font-size: 14px;
}
.overflow-box p {
margin: 0 0 12px;
}
.overflow-visible {
overflow: visible;
}
.overflow-hidden {
overflow: hidden;
}
.overflow-clip {
overflow: clip;
}
.overflow-scroll {
overflow: scroll;
}
.overflow-auto {
overflow: auto;
}
visible
First paragraph of content.
Second paragraph of content.
Third paragraph of content.
hidden
First paragraph of content.
Second paragraph of content.
Third paragraph of content.
clip
First paragraph of content.
Second paragraph of content.
Third paragraph of content.
scroll
First paragraph of content.
Second paragraph of content.
Third paragraph of content.
auto
First paragraph of content.
Second paragraph of content.
Third paragraph of content.
The visible box allows content to extend beyond its border. The hidden and clip boxes conceal excess content. The scroll and auto boxes allow the content to be scrolled.
Horizontal Overflow Example
This example creates a horizontally scrollable container for content wider than its available space.
<div class="horizontal-scroll">
<div class="wide-content">This content is wider than its container.</div>
</div>
.horizontal-scroll {
max-width: 300px;
overflow-x: auto;
border: 2px solid #64748b;
}
.wide-content {
width: 500px;
padding: 15px;
background-color: #dbeafe;
}
Scroll horizontally to view the content that extends beyond the container's width.
Common Mistakes
Expecting Scrolling Without Size Constraints
.box {
overflow: auto;
}
If the box expands to fit its content, there may be no overflow to scroll. Apply suitable width, height, or maximum-size constraints.
Using an Invalid Value
.box {
overflow: none;
}
The none keyword is not a valid overflow value.
Confusing hidden and clip
hidden allows programmatic scrolling, while clip prevents scrolling on the clipped axis.
Expecting visible to Remain Visible in Every Combination
.box {
overflow-x: visible;
overflow-y: auto;
}
The horizontal visible value computes to auto because the other axis uses a scrolling overflow value.
Using hidden to Conceal Layout Problems
Clipping overflow can hide the symptoms of incorrect widths or minimum sizes without correcting the underlying issue.
Expecting overflow to Add an Ellipsis
Use text-overflow: ellipsis with appropriate clipping and text wrapping rules when an ellipsis is required.
Creating Unnecessary Nested Scroll Areas
Multiple independent scroll containers can make keyboard and touch navigation more difficult.
Clipping Focus Indicators
Overflow clipping can conceal outlines or shadows used to identify focused elements.
Assuming Scrollbars Always Look the Same
Scrollbar visibility and appearance vary across browsers and operating systems.
Important Considerations
Keep the following points in mind when using overflow:
- The property controls content that extends beyond an element's padding box.
- It is a shorthand for
overflow-xandoverflow-y. - The initial value is
visibleon both axes. - The property is not inherited.
- A single keyword applies to both axes.
- Two keywords control horizontal and vertical overflow separately.
visibleallows content to extend beyond the container.hiddenclips content but permits programmatic scrolling.clipclips content without creating a scroll container by itself.scrollestablishes a scroll container and enables scrolling.autoenables scrolling when needed.- Some combinations of axis values produce adjusted computed values.
- Scroll containers generally need appropriate size constraints.
- Scrollbar appearance depends on the browser and operating system.
- Overflow can affect formatting contexts and float containment.
- Flex and grid minimum sizing rules can influence overflow.
overflow: hiddenis not always the best solution for unwanted page scrolling.- Clipping may conceal important content or focus indicators.
text-overflowcontrols the presentation of clipped inline text.scrollbar-guttercan help manage scrollbar space.overflow-clip-margincan adjust clipping for applicable elements.- Logical overflow properties provide writing-mode-aware control.
- Scrollable regions should remain accessible to keyboard users.
- Test overflow behavior at different viewport widths and zoom levels.
Browser Support
The CSS overflow property is Baseline - Widely available and has been supported in major browsers for many years.
Modern versions of Chrome, Edge, Firefox, Safari, and other major browsers support the standard visible, hidden, scroll, and auto values.
The clip value and certain newer overflow-related features have more recent browser support histories. Check compatibility when supporting older browsers.
Scrollbar appearance and behavior may vary by operating system and browser settings.
For current compatibility information, visit Can I Use? and search for overflow.
Specifications
The overflow property is defined in CSS Overflow Module Level 3.
CSS Overflow Module Level 3: Overflow Control
The specification defines overflow values, clipping, scrolling, and the relationship between the horizontal and vertical overflow axes.
For additional documentation and examples, consult MDN Web Docs.
