Skip to content

CSS Overflow

The CSS overflow property controls what happens when content is larger than its container. It helps manage scrolling, hidden content, long text, images, code blocks, tables, and responsive layouts.

What Is CSS Overflow?

Overflow occurs when content extends beyond the width or height of its container. The CSS overflow property determines whether the extra content is visible, hidden, clipped, or scrollable.

.container {
  width: 300px;
  height: 150px;
  overflow: auto;
}

Without proper overflow handling, content can overlap other elements, break layouts, or become inaccessible to users.

Why CSS Overflow Is Important

  • Prevents content from breaking layouts.
  • Enables scrolling inside containers.
  • Improves mobile responsiveness.
  • Handles long text and large images.
  • Supports data tables and code examples.
  • Creates scrollable sidebars and panels.
  • Improves user experience and accessibility.

CSS Overflow Cheatsheet

Value Description Typical Usage
visible Default. Content can extend outside container. Normal content flow.
hidden Extra content is clipped. Image cropping and UI containers.
scroll Always shows scrollbars. Fixed-size panels.
auto Shows scrollbars only when needed. Most common overflow setting.
clip Clips content without scrollbars. Modern UI components.
overflow-x Controls horizontal overflow. Wide tables and code blocks.
overflow-y Controls vertical overflow. Scrollable panels.

overflow: visible

This is the default overflow behavior. Content is allowed to extend beyond the container boundaries.

.box {
  overflow: visible;
}

While useful for some designs, visible overflow can cause layout issues when content becomes too large.

overflow: hidden

The hidden value clips content that exceeds the container size. Users cannot scroll to hidden content.

.card-image {
  overflow: hidden;
  border-radius: 1rem;
}

This technique is commonly used for image cards, sliders, hero banners, and rounded corners.

overflow: scroll

Scroll always displays scrollbars, even when the content fits.

.panel {
  height: 300px;
  overflow: scroll;
}

This ensures consistent UI dimensions but can create unnecessary scrollbars.

overflow: auto

Auto is the most commonly used overflow value. Scrollbars appear only when content exceeds the container size.

.sidebar {
  max-height: 500px;
  overflow: auto;
}

Use auto for sidebars, data grids, comments, chat windows, and dashboard panels.

overflow: clip

The clip value hides overflowing content without creating scrollbars. It is a modern alternative to some hidden overflow use cases.

.container {
  overflow: clip;
}

Clip is useful when content should never be scrollable.

overflow-x and overflow-y

CSS allows horizontal and vertical overflow to be controlled independently.

.table-wrapper {
  overflow-x: auto;
  overflow-y: hidden;
}

This is especially useful for responsive tables and code examples.

Property Purpose
overflow-x Controls horizontal overflow.
overflow-y Controls vertical overflow.

CSS Text Overflow

Long text can break layouts. The text-overflow property helps manage overflowing text.

.title {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

This produces the familiar "..." truncation effect.

Value Result
clip Cuts off text.
ellipsis Displays ...

Responsive Tables with Overflow

Large tables often overflow on mobile devices. Wrapping them inside a scrollable container improves usability.

.table-wrapper {
  overflow-x: auto;
}
<div class="table-wrapper">
  <table>
    ...
  </table>
</div>

Code Blocks and Horizontal Scrolling

Long code examples often exceed the width of mobile screens.

pre {
  overflow-x: auto;
}

This allows users to scroll horizontally without breaking the layout.

CSS Overflow and Accessibility

  • Ensure hidden content is not critical information.
  • Provide keyboard access to scrollable regions.
  • Use visible focus states inside scroll containers.
  • Avoid trapping users inside nested scrolling areas.
  • Test overflow behavior on mobile devices.

CSS Overflow and SEO

Overflow does not directly affect rankings, but poor overflow management can negatively affect usability and Core Web Vitals.

  • Prevent horizontal scrolling across the entire page.
  • Keep important content visible and accessible.
  • Improve mobile usability.
  • Reduce layout shifts caused by oversized content.
  • Support responsive design best practices.

Common CSS Overflow Mistakes

  • Using overflow: hidden and hiding important content.
  • Forgetting mobile testing.
  • Allowing wide tables to break layouts.
  • Not handling long code blocks.
  • Creating nested scrollbars unnecessarily.
  • Using fixed heights that cause content clipping.
  • Ignoring accessibility requirements for scrollable regions.

CSS Overflow Best Practices

  • Use overflow: auto in most scrollable containers.
  • Use overflow-x: auto for tables and code blocks.
  • Avoid hiding important content with overflow: hidden.
  • Test overflow behavior on mobile screens.
  • Provide accessible scrolling experiences.
  • Use responsive layouts to reduce overflow issues.
  • Combine overflow with Flexbox and Grid thoughtfully.

Key Takeaways

  • Overflow controls how content behaves when it exceeds its container.
  • visible is the default behavior.
  • hidden clips extra content.
  • scroll always shows scrollbars.
  • auto shows scrollbars only when needed.
  • overflow-x and overflow-y control individual directions.
  • Proper overflow handling improves responsiveness and usability.

Pro Tip

Use overflow-x: auto on tables and code blocks rather than forcing the entire page to scroll horizontally. This greatly improves mobile usability.