CSS overflow-x Property

The CSS overflow-x property controls how an element handles content that extends beyond its horizontal boundaries.

It determines whether horizontal overflow remains visible, is clipped, or can be accessed through scrolling. Common uses include wide tables, code examples, image galleries, and other content that may exceed the available width.

overflow-x Syntax

The overflow-x property accepts one keyword that determines how horizontal overflow is handled.

Basic Syntax

selector {
  overflow-x: auto;
}

Keyword Values

.box {
  overflow-x: visible;
}
.box {
  overflow-x: hidden;
}
.box {
  overflow-x: clip;
}
.box {
  overflow-x: scroll;
}
.box {
  overflow-x: auto;
}

CSS-Wide Keywords

.box {
  overflow-x: initial;
}
.box {
  overflow-x: inherit;
}
.box {
  overflow-x: unset;
}
.box {
  overflow-x: revert;
}
.box {
  overflow-x: revert-layer;
}

The initial value is visible. However, its computed value can change depending on the value of overflow-y.

Property Values

Value Description
visible Allows content to extend beyond the horizontal boundary without clipping, subject to the other overflow axis.
hidden Clips horizontal overflow without displaying a scrollbar. Programmatic scrolling remains possible.
clip Clips horizontal overflow at the overflow clip edge and prevents scrolling along that axis.
scroll Clips horizontal overflow and creates a scrollable region, whether or not content currently overflows.
auto Allows horizontal scrolling when content exceeds the available width.
initial Resets the property to visible.
inherit Uses the computed value of the parent element.
unset Resets the property to visible because it 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
Inherited No
Applies to Block containers, flex containers, and grid containers
Computed value Specified keyword, with adjustments based on the other overflow axis
Animation type Discrete
Physical axis Horizontal (x-axis)
Percentage values Not accepted
Length values Not accepted

Valid Values

.box {
  overflow-x: visible;
}
.box {
  overflow-x: hidden;
}
.box {
  overflow-x: clip;
}
.box {
  overflow-x: scroll;
}
.box {
  overflow-x: auto;
}

Invalid Values

.box {
  overflow-x: none;
}
.box {
  overflow-x: 20px;
}
.box {
  overflow-x: hidden auto;
}

The property accepts one overflow keyword, not a length or multiple overflow values.

How It Works

Horizontal overflow occurs when content extends beyond the left or right boundary of an element's available content area.

The overflow-x property determines whether that content remains visible, is clipped, or can be reached through scrolling.

Creating Horizontal Overflow

.container {
  width: 250px;
  overflow-x: auto;
}
.content {
  width: 500px;
}

The child is wider than its container, so horizontal scrolling becomes available.

Clipping Horizontal Overflow

.container {
  width: 250px;
  overflow-x: hidden;
}
.content {
  width: 500px;
}

The portion of the child extending beyond the container's horizontal boundary is clipped.

Allowing Visible Overflow

.container {
  width: 250px;
  overflow-x: visible;
}
.content {
  width: 500px;
}

The overflowing content remains visible when the vertical overflow setting does not cause the horizontal value to compute differently.

Important Consideration

Horizontal scrolling requires content to extend beyond the available width. Setting overflow-x: auto does not create a visible scrollbar when everything fits.

Overflow Values

Each overflow keyword provides different behavior for content extending beyond the horizontal boundaries.

visible

.box {
  overflow-x: visible;
}

Horizontal overflow remains visible. This is the initial value.

hidden

.box {
  overflow-x: hidden;
}

Horizontal overflow is clipped. Users cannot scroll along that axis using a scrollbar, but scripts and certain browser navigation behaviors may still change the scroll position.

clip

.box {
  overflow-x: clip;
}

Horizontal overflow is clipped at the overflow clip edge. The clipped axis does not permit programmatic scrolling.

scroll

.box {
  overflow-x: scroll;
}

Horizontal scrolling is enabled even when the content currently fits. The scrollbar may be persistent or automatically hidden depending on the browser and operating system.

auto

.box {
  overflow-x: auto;
}

Horizontal scrolling becomes available when content exceeds the container's width.

Choosing a Value

Use auto when overflowing content must remain accessible, visible when content should extend beyond the box, and clipping values only when hiding content is intentional.

overflow-x vs. overflow-y

The overflow-x and overflow-y properties control overflow along separate physical axes.

Property Direction Typical Use
overflow-x Horizontal Wide tables, code blocks, and horizontal galleries
overflow-y Vertical Scrollable panels, menus, and long content areas

Horizontal Scrolling

.box {
  overflow-x: auto;
  overflow-y: hidden;
}

Vertical Scrolling

.box {
  overflow-x: hidden;
  overflow-y: auto;
}

Scrolling Along Both Axes

.box {
  overflow-x: auto;
  overflow-y: auto;
}

Using the overflow Shorthand

.box {
  overflow: auto hidden;
}

The first value sets overflow-x, and the second sets overflow-y.

Important Consideration

The two overflow axes interact. Certain combinations of values are adjusted when the browser computes the final overflow behavior.

Computed Value Adjustments

Some overflow values change at computed-value time depending on the setting of the opposite axis.

visible with auto

.box {
  overflow-x: visible;
  overflow-y: auto;
}

The specified overflow-x: visible computes to auto because the vertical axis uses auto.

clip with scroll

.box {
  overflow-x: clip;
  overflow-y: scroll;
}

The specified overflow-x: clip computes to hidden because the vertical axis uses scroll.

Both Axes Using visible

.box {
  overflow-x: visible;
  overflow-y: visible;
}

Both values remain visible.

Both Axes Using clip

.box {
  overflow-x: clip;
  overflow-y: clip;
}

Both values remain clip.

Computed Value Rules

  • visible computes to auto when the opposite axis is neither visible nor clip.
  • clip computes to hidden under the same condition.
  • When both axes use visible or clip, those values remain unchanged.

These rules are important when attempting to allow overflow along one axis while clipping or scrolling along the other.

Horizontal Scrolling

Horizontal scrolling is useful when content has a natural width that should not be reduced to fit a narrow container.

Basic Scroll Container

.horizontal-scroll {
  max-width: 100%;
  overflow-x: auto;
}
.horizontal-content {
  width: 700px;
}

Preventing Flex Items from Wrapping

.horizontal-list {
  display: flex;
  flex-wrap: nowrap;
  gap: 12px;
  overflow-x: auto;
}
.horizontal-list > * {
  flex: 0 0 180px;
}

The items remain in one row and can be reached by scrolling horizontally.

Scroll Snapping

.horizontal-list {
  display: flex;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
}
.horizontal-list > * {
  flex: 0 0 200px;
  scroll-snap-align: start;
}

Scroll snapping can align items at defined positions as the user scrolls.

Scrollbar Appearance

The browser and operating system determine whether a scrollbar is always visible, appears only during scrolling, or uses an overlay style.

Important Consideration

A scroll container should provide enough visual context for users to recognize that additional content is available horizontally.

Responsive Tables

Wide data tables can become difficult to display on narrow screens. Wrapping a table in a horizontally scrollable container preserves its columns without forcing the entire page to overflow.

HTML Example

<div class="table-scroll" tabindex="0" role="region" aria-label="Scrollable data table">
  <table>
    <thead>
      <tr>
        <th>Property</th>
        <th>Initial Value</th>
        <th>Inherited</th>
        <th>Animation Type</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>overflow-x</td>
        <td>visible</td>
        <td>No</td>
        <td>Discrete</td>
      </tr>
    </tbody>
  </table>
</div>

CSS Example

.table-scroll {
  max-width: 100%;
  overflow-x: auto;
}
.table-scroll table {
  width: 100%;
  min-width: 600px;
  border-collapse: collapse;
}
.table-scroll th,
.table-scroll td {
  padding: 10px;
  border: 1px solid #cbd5e1;
  text-align: left;
}
.table-scroll:focus-visible {
  outline: 3px solid #2563eb;
  outline-offset: 2px;
}

The wrapper scrolls horizontally when the table's minimum width exceeds the available space.

Important Consideration

Keep table headers associated with their data cells and provide a descriptive label for keyboard-accessible scroll regions.

Scrollable Code Blocks

Code examples often contain long lines that should preserve their formatting. Horizontal scrolling allows users to inspect those lines without wrapping them.

CSS Example

pre {
  max-width: 100%;
  overflow-x: auto;
  white-space: pre;
}

Example Markup

<pre><code>const message = "A long line of JavaScript code that extends beyond the available width.";</code></pre>

Why Use Horizontal Scrolling?

Preserving line breaks and indentation can make code easier to read, particularly when line wrapping would split expressions or obscure formatting.

Important Consideration

Make sure scrollable code blocks can be reached with the intended input methods and that long lines remain selectable and copyable.

Physical vs. Logical Overflow

The overflow-x property always controls the physical horizontal axis, regardless of writing mode.

The logical overflow-inline and overflow-block properties adapt to the element's writing mode.

Property Axis
overflow-x Physical horizontal axis
overflow-y Physical vertical axis
overflow-inline Logical inline axis
overflow-block Logical block axis

Horizontal Writing Mode

.box {
  writing-mode: horizontal-tb;
  overflow-x: auto;
}

In horizontal writing, the physical x-axis corresponds to the inline axis.

Vertical Writing Mode

.box {
  writing-mode: vertical-rl;
  overflow-x: auto;
}

In vertical writing, the physical x-axis corresponds to the block axis.

Important Consideration

Use overflow-x when the desired direction is always horizontal. Use logical overflow properties when the direction should adapt to writing mode, while accounting for their browser support.

overflow-x Example

This example compares the five overflow values using a fixed-width container and a wider child element.

HTML Example

<div class="overflow-demo">
  <div class="overflow-item">
    <h3>visible</h3>
    <div class="overflow-box overflow-visible">
      <div class="overflow-content">Wide Content - Scroll or Clip Test</div>
    </div>
  </div>
  <div class="overflow-item">
    <h3>hidden</h3>
    <div class="overflow-box overflow-hidden">
      <div class="overflow-content">Wide Content - Scroll or Clip Test</div>
    </div>
  </div>
  <div class="overflow-item">
    <h3>clip</h3>
    <div class="overflow-box overflow-clip">
      <div class="overflow-content">Wide Content - Scroll or Clip Test</div>
    </div>
  </div>
  <div class="overflow-item">
    <h3>scroll</h3>
    <div class="overflow-box overflow-scroll" tabindex="0">
      <div class="overflow-content">Wide Content - Scroll or Clip Test</div>
    </div>
  </div>
  <div class="overflow-item">
    <h3>auto</h3>
    <div class="overflow-box overflow-auto" tabindex="0">
      <div class="overflow-content">Wide Content - Scroll or Clip Test</div>
    </div>
  </div>
</div>

CSS Example

.overflow-demo {
  display: flex;
  flex-direction: column;
  gap: 22px;
  padding: 20px;
  background-color: #f1f5f9;
}
.overflow-item h3 {
  margin: 0 0 8px;
  font-size: 16px;
}
.overflow-box {
  box-sizing: border-box;
  width: 220px;
  height: 85px;
  padding: 10px;
  border: 2px solid #64748b;
  background-color: #fff;
}
.overflow-content {
  box-sizing: border-box;
  width: 380px;
  padding: 15px;
  background-color: #dbeafe;
  border: 1px solid #93c5fd;
  white-space: nowrap;
}
.overflow-visible {
  overflow-x: visible;
}
.overflow-hidden {
  overflow-x: hidden;
}
.overflow-clip {
  overflow-x: clip;
}
.overflow-scroll {
  overflow-x: scroll;
}
.overflow-auto {
  overflow-x: auto;
}
Browser Result

visible

Wide Content - Scroll or Clip Test

hidden

Wide Content - Scroll or Clip Test

clip

Wide Content - Scroll or Clip Test

scroll

Wide Content - Scroll or Clip Test

auto

Wide Content - Scroll or Clip Test

The visible example allows the child to extend beyond its container. The hidden and clip examples conceal the excess content, while scroll and auto provide horizontal scrolling.

Scrollable Cards Example

The following example uses a horizontal flex layout with scrolling enabled when the cards exceed the available width.

<div class="card-scroll" tabindex="0" role="region" aria-label="Scrollable cards">
  <div class="scroll-card">Card 1</div>
  <div class="scroll-card">Card 2</div>
  <div class="scroll-card">Card 3</div>
  <div class="scroll-card">Card 4</div>
</div>
.card-scroll {
  display: flex;
  gap: 12px;
  max-width: 100%;
  padding: 12px;
  overflow-x: auto;
  background-color: #f1f5f9;
}
.scroll-card {
  flex: 0 0 160px;
  padding: 24px;
  background-color: #dbeafe;
  border: 1px solid #93c5fd;
  text-align: center;
}
Browser Result
Card 1
Card 2
Card 3
Card 4

On narrow screens, users can scroll horizontally to reach cards that extend beyond the visible area.

Accessibility Considerations

Horizontal scrolling should be used carefully because it can make content harder to discover or navigate.

Keyboard Access

Ensure scrollable regions can be operated with a keyboard. A focusable container may be appropriate when its content cannot otherwise be reached conveniently.

<div class="horizontal-scroll" tabindex="0" role="region" aria-label="Scrollable content">
  <div class="wide-content">Content</div>
</div>

Visible Focus Indicators

.horizontal-scroll:focus-visible {
  outline: 3px solid #2563eb;
  outline-offset: 2px;
}

Avoid Unnecessary Page Scrolling

Prefer responsive layouts and text wrapping when possible. Reserve horizontal scrolling for content such as tables, code, and galleries where maintaining width is useful.

Content Discovery

Consider a visible scrollbar, partial next item, or brief instruction to indicate that more content is available.

Zoom and Reflow

Test horizontal scroll regions at increased zoom and narrow viewport widths to ensure all content remains accessible.

Common Mistakes

Using overflow-x Without Overflowing Content

.box {
  overflow-x: auto;
}

Scrolling will not be necessary if the content fits within the container.

Confusing hidden and clip

hidden permits programmatic scrolling, while clip prevents scrolling along the clipped axis.

Expecting visible to Remain visible

.box {
  overflow-x: visible;
  overflow-y: auto;
}

The horizontal value computes to auto because the vertical axis uses a scrolling overflow value.

Using Invalid Values

.box {
  overflow-x: none;
}

The none keyword is not supported.

Hiding Essential Content

Using hidden or clip may conceal controls or information that users need.

Applying Scrolling to the Entire Page

When only one component is too wide, place the overflow behavior on that component rather than allowing the entire page to scroll horizontally.

Ignoring Flex and Grid Sizing

Flex and grid items may require appropriate minimum sizing rules before an inner scroll container can shrink to the available width.

Important Considerations

Keep the following points in mind when using overflow-x:

  • The property controls overflow along the physical horizontal axis.
  • Its initial value is visible.
  • The property is not inherited.
  • It accepts visible, hidden, clip, scroll, and auto.
  • Only one overflow keyword is permitted.
  • visible allows content to extend beyond the container.
  • hidden clips overflow but permits programmatic scrolling.
  • clip clips overflow and prevents scrolling along the clipped axis.
  • scroll creates a scrollable region even when no overflow currently exists.
  • auto provides scrolling when needed.
  • Scrollbar visibility depends on the browser and operating system.
  • The value of overflow-y can affect the computed value of overflow-x.
  • overflow can set both physical axes in one declaration.
  • The property remains horizontal regardless of writing mode.
  • Logical overflow properties adapt to writing mode.
  • Horizontal scrolling is useful for wide tables and code blocks.
  • Scrollable components should remain keyboard accessible.
  • Clipping should not conceal essential content.
  • Flex and grid sizing rules can affect whether scrolling occurs.
  • Test scrolling with keyboard, mouse, touch, and increased zoom.

Browser Support

The CSS overflow-x property is Widely available across modern browsers.

The visible, hidden, scroll, and auto values have long-standing support. The clip value is supported in modern browsers but may not work in older versions.

Fallback Example

.box {
  overflow-x: hidden;
  overflow-x: clip;
}

Browsers that do not support clip can use hidden as a fallback. However, the two values differ in programmatic scrolling behavior.

For current compatibility information, visit Can I Use? and search for overflow-x.

Specifications

The overflow-x property is defined in CSS Overflow Module Level 3.

CSS Overflow Module Level 3: Overflow Control

The specification defines the overflow keywords, scrolling behavior, and interactions between the horizontal and vertical overflow axes.

For additional documentation and examples, consult MDN Web Docs.

MDN Web Docs: overflow-x