CSS

CSS overflow Property: Overflowing Text in CSS

The overflow property in CSS controls what happens when content overflows an element’s box.

How to Use the CSS overflow Property

The overflow CSS property can take several values: visible, hidden, scroll, and auto. Each value describes a different way to handle overflow content.

.element {
  overflow: hidden;
}
  • visible: Content can overflow the element’s box and may overlap other elements.
  • hidden: Overflow content is clipped and becomes invisible.
  • scroll: Always show scroll bars, even if the content’s size fits in the element’s box.
  • auto: Shows scroll bars only when the content overflows.

When to Use the CSS overflow Property

Managing Overflowing Text

You can use overflow to manage overflowing text in CSS. By setting it to hidden, you can prevent text from spilling out of its container.

.text-container {
  width: 200px;
  overflow: hidden;
}

Creating Scrollable Content Areas

The overflow property helps in creating scrollable content areas. You can set overflow to scroll or auto to allow users to scroll through the content.

.scrollable {
  height: 200px;
  overflow: auto;
}

Hiding Overflowing Elements

When you need to hide elements that exceed a container’s dimensions, overflow: hidden is a reliable choice.

.image-container {
  width: 100px;
  height: 100px;
  overflow: hidden;
}

Examples of Using the CSS overflow Property

Improving User Experience

Websites use overflow: auto to create sections where users can see more content without leaving the current page.

.sidebar {
  height: 300px;
  overflow: auto;
}

Custom Scrollbars

Web applications sometimes implement custom scrollbars to match their design aesthetics.

.custom-scroll {
  height: 150px;
  overflow: scroll;
}

/* Custom scrollbar styling */
.custom-scroll::-webkit-scrollbar {
  width: 10px;
}
.custom-scroll::-webkit-scrollbar-thumb {
  background-color: darkgrey;
  border-radius: 10px;
}

Preventing Layout Disruption

When content within a fixed-size container grows too large, overflow: hidden prevents the content from breaking the layout.

.card {
  max-height: 400px;
  overflow: hidden;
}

Learn More About the CSS overflow Property

Advanced Handling of Overflowing Text

The overflow property in CSS is vital for handling overflowing text. You can use it in combination with text-overflow and white-space for even more control.

.truncated-text {
  width: 150px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

Controlling Overflow in Different Directions

Use overflow-x and overflow-y to control overflow content horizontally or vertically.

.horizontal-scroll {
  overflow-x: scroll;
  overflow-y: hidden;
}

CSS3 Overflow Properties

CSS3 introduced additional properties like overflow-wrap and word-break to manage overflowing text CSS effectively.

.break-word {
  overflow-wrap: break-word;
}

.break-all {
  word-break: break-all;
}

Supporting Responsive Design

In responsive design, use overflow to ensure content adapts to various screen sizes.

.responsive-container {
  overflow: auto;
  max-width: 100%;
}

Handling Overflow in Nested Elements

Nest elements with different overflow values to create sophisticated UI patterns.

.outer-container {
  height: 300px;
  overflow: hidden;
}
.inner-scroll {
  height: 100%;
  overflow: auto;
}

CSS Overflow and Flexbox

When using Flexbox, the overflow property can be crucial for managing space within flexible containers. It ensures that content within flex items behaves as expected, preventing overflow issues.

.flex-container {
  display: flex;
  height: 100vh;
}

.flex-item {
  flex: 1;
  overflow: auto;
}

Managing Overflow in Grid Layouts

In CSS Grid layouts, the overflow property helps manage content that extends beyond grid cells. This is useful for maintaining grid integrity and preventing layout shifts.

.grid-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.grid-item {
  height: 100px;
  overflow: hidden;
}
Learn to Code in Python for Free
Start learning now
button icon
To advance beyond this tutorial and learn Python by doing, try the interactive experience of Mimo. Whether you're starting from scratch or brushing up your coding skills, Mimo helps you take your coding journey above and beyond.

Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.

You can code, too.

© 2024 Mimo GmbH