- CSS Tutorial
- CSS Introduction
- CSS Syntax
- CSS Comments
- CSS Selectors
- CSS Fonts
- CSS Colors
- CSS Backgrounds
- CSS Box Model
- CSS Borders
- CSS Margins
- CSS Padding
- CSS Text
- CSS Images
- CSS Links
- CSS Lists
- CSS Tables
- CSS Outline
- CSS Icons
- CSS Display
- CSS max-witdh
- CSS Position
- CSS z-index
- CSS Overflow
- CSS Float
- CSS Align
- CSS Opacity
- CSS Navigation Bar
- CSS Dropdowns
- CSS Forms
- CSS Units
- CSS !important
- CSS Specificity
- CSS Combinators
- CSS inline-block
- CSS Hover
- CSS Cursors
- CSS Selectors
- CSS Type Selector
- CSS Class Selector
- CSS ID Selector
- CSS Attribute Selector
- CSS Pseudo-class Selector
- CSS Pseudo-element Selector
- CSS Universal Selector
- CSS Advanced
- CSS Text Formatting
- CSS Gradients
- CSS Shadow
- CSS Rounded Corners
- CSS Text Effects
- CSS 2D Transform
- CSS 3D Transform
- CSS Border Images
- CSS Inherit
- CSS Transitions
- CSS Animations
- CSS Box Sizing
- CSS Tooltip
- CSS Masking
- CSS Pagination
- CSS Styling Images
- CSS object-fit
- CSS object-position
- CSS Buttons
- CSS Multiple Columns
- CSS Variables
- CSS Flexbox
- CSS Grid
- CSS Media Queries
CSS Overflow
In the world of CSS, every element is a rectangular box. But what happens when the content inside that box like a large image or a long paragraph is bigger than the box itself? This is where the overflow property comes in. It gives you control over how to handle "spillover" content: whether to hide it, show scrollbars, or simply let it hang outside the boundaries.
height or max-height) or if content is prevented from wrapping (like using white-space: nowrap).
Syntax
The overflow property is a shorthand for overflow-x (horizontal) and overflow-y (vertical). If you provide one value, it applies to both directions.
/* Standard syntax */
.container {
overflow: auto;
}
/* Applying different behavior to horizontal and vertical */
.container {
overflow: hidden auto; /* x: hidden, y: auto */
}
Values
- visible: This is the default setting. The content is not clipped and will "bleed" outside the element's borders, potentially overlapping other elements on the page.
- hidden: Any content that exceeds the box dimensions is simply cut off and becomes invisible. There are no scrollbars to see the rest.
- scroll: This forces scrollbars to appear on the element, even if the content fits perfectly. This is often used to keep UI layouts consistent.
- auto: The smartest choice for most layouts. Scrollbars only appear if the content actually overflows the container.
- inherit: The element adopts the overflow behavior of its parent container.
overflow: auto; instead of scroll in most cases. This prevents "phantom" scrollbars from appearing when they aren't needed, keeping your UI much cleaner.
Usage Examples
Visible Overflow (The Default Behavior)
In this scenario, notice how the text ignores the blue box's boundaries. This is rarely what you want in a finished design, but it’s how browsers handle overflow by default.
<div class="visible-box">
This text is inside a small box, but because overflow is set to visible,
it will just keep going right over the edge of the background color.
</div>
.visible-box {
width: 150px;
height: 100px;
overflow: visible;
background-color: lightblue;
border: 1px solid blue;
}
Hidden Overflow
This is useful for cropping images or ensuring that messy content doesn't break your site's grid. Think of it like a window: you only see what's directly in the frame.
overflow: hidden will also clip things like box-shadows, dropdown menus, or tooltips that are supposed to pop out of the container.
<div class="hidden-box">
You will only see the first few words of this sentence because the rest
is clipped by the container's boundaries.
</div>
.hidden-box {
width: 150px;
height: 100px;
overflow: hidden;
background-color: lightgreen;
}
Scroll Overflow
This forces the browser to reserve space for scrollbars. This is helpful if you want multiple boxes side-by-side to stay the same size regardless of their content.
<div class="scroll-box">
Even if this text was short, you would still see the scrollbar tracks
on the bottom and right side of this coral box.
</div>
.scroll-box {
width: 150px;
height: 100px;
overflow: scroll;
background-color: lightcoral;
}
Auto Overflow
This is the workhorse of responsive web design. It’s perfect for sidebars, chat windows, or any area where the amount of content is dynamic.
<div class="auto-box">
If this content grows too long, a vertical scrollbar will appear
automatically. If it's short, it looks like a normal box.
</div>
.auto-box {
width: 150px;
height: 100px;
overflow: auto;
background-color: lightyellow;
}
Related Properties
Sometimes you only want to control overflow in one direction. For instance, you might want a code snippet to scroll horizontally but expand vertically to its full height.
- overflow-x: Specifically manages the left and right edges.
- overflow-y: Specifically manages the top and bottom edges.
overflow-x: hidden while expecting overflow-y: visible to work. Due to CSS specifications, if one axis is hidden/auto/scroll, the other axis cannot be visible; it will automatically be treated as auto.
Examples with overflow-x and overflow-y
Horizontal Scroll (Common for Data Tables)
Often used for wide tables on mobile screens where you want the user to swipe left and right to see more data.
<div class="overflow-x-box">
<div style="width: 300px;">This inner div is wider than the parent!</div>
</div>
.overflow-x-box {
width: 150px;
overflow-x: auto;
overflow-y: hidden;
background-color: lightcyan;
white-space: nowrap;
}
Vertical Scroll Only (Common for Sidebars)
<div class="overflow-y-box">
Line 1<br>Line 2<br>Line 3<br>Line 4<br>Line 5<br>Line 6
</div>
.overflow-y-box {
width: 150px;
height: 80px;
overflow-y: scroll;
overflow-x: hidden;
background-color: lightpink;
}
Practical Tips
Mastering overflow is about more than just hiding text. Here are some real-world applications:
- Sticky Headers: When creating a scrollable area within a page, ensure the parent has a defined height so the
overflow: autotakes effect. - Text Truncation: Combine
overflow: hiddenwithtext-overflow: ellipsisandwhite-space: nowrapto create those "..." trailing dots on long headlines. - Modals: Developers often apply
overflow: hiddento the<body>tag when a modal is open to prevent the background page from scrolling.
Understanding the overflow property is essential for creating robust, polished layouts. By choosing the right value for the right situation, you can ensure your content stays accessible and your UI remains clean, no matter how much data you throw at it.