Overflow Handling: Visible, Hidden, Scroll, and Custom Scrollbars
Master CSS overflow handling. Learn how to manage overflowing content with visible, hidden, scroll, and auto properties, alongside custom webkit scrollbars.
Introduction
In responsive design, elements must handle content of varying sizes. If you build a card container with fixed dimensions, but the description text is longer than expected, the text will spill out of the card and overlap other page content.
To control what happens when content is too large for its container box, CSS provides the overflow properties. By configuring overflow values, you can choose to hide extra content, wrap it inside local scroll areas, or build styled custom scrollbars to maintain your website's design system.
What You Will Learn
- How the browser handles box content using
overflow. - The differences between
visible,hidden,scroll, andauto. - Controlling overflow independently on the X and Y axes.
- Hiding unwanted horizontal scroll bars.
- Styling scrollbars using modern CSS.
Prerequisites
Detailed Explanation: The Overflow Property
The overflow property controls how content is rendered when it exceeds the width or height of its parent element.
> [!NOTE]
> The overflow property ONLY works on block-level containers that have a specified height or width (or max-height/max-width). If an element is allowed to expand infinitely to fit its content, overflow rules do not apply.
Core Overflow Values
visible: (Default) The overflowing content is not clipped. It renders outside the boundaries of the parent box, overlapping other page elements.hidden: The overflowing content is clipped and hidden. No scrollbars are rendered, making the hidden content inaccessible to the user unless revealed via JavaScript.scroll: The overflowing content is clipped, and the browser forces scrollbars to render (both horizontally and vertically), even if the content fits inside the container.auto: (Recommended) The browser clips the content and only renders scrollbars if the content actually overflows the container bounds.
Directional Axis Controls (overflow-x and overflow-y)
You can control overflow behavior on the horizontal and vertical axes independently:
.sidebar {
/* Allow vertical scrolling, but hide horizontal overflow */
overflow-y: auto;
overflow-x: hidden;
}
Styling Custom Scrollbars
Browser default scrollbars can look out of place on modern dark-mode designs. CSS allows you to customize scrollbar styles using vendor prefixes (-webkit) or modern standards.
The Webkit Method (Chrome, Safari, Edge)
You can style scrollbar elements directly using pseudo-elements:
/* Width of the scrollbar path */
.custom-scroll::-webkit-scrollbar {
width: 8px;
}
/* Background track area */
.custom-scroll::-webkit-scrollbar-track {
background: #1e293b;
}
/* Draggable handle bar */
.custom-scroll::-webkit-scrollbar-thumb {
background: #3b82f6;
border-radius: 4px;
}
/* Hover effect on handle */
.custom-scroll::-webkit-scrollbar-thumb:hover {
background: #2563eb;
}
Visual Learning Diagram (Mermaid)
graph TD
ContentOverflow[Content exceeds Card height] --> Check{What is the overflow value?}
Check -->|visible| RenderOutside[Render outside borders. Overlaps other text.]
Check -->|hidden| ClipContent[Clip content. Hide extra text.]
Check -->|scroll| ForceBars[Render scrollbars on both axes.]
Check -->|auto| ConditionalBars[Render scrollbars only on the axis that overflows.]
Code Examples
Example 1: Custom Scrollable Code Block
Build a code block wrapper that allows horizontal scrolling for code lines, but prevents vertical expansion:
.code-preview {
max-height: 300px;
background-color: #0f172a;
padding: 16px;
border-radius: 8px;
/* Enable horizontal scroll, auto vertical scroll */
overflow-x: auto;
overflow-y: auto;
}
.code-preview code {
/* Prevent code lines wrapping automatically */
white-space: pre;
}
Example 2: Glassmorphic Scrollable Panel
Create a responsive list panel with a custom styled scrollbar:
.scroll-panel {
width: 320px;
height: 400px;
background: rgba(255, 255, 255, 0.05);
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 16px;
/* Vertical scrolling only */
overflow-y: auto;
overflow-x: hidden;
}
/* Style scrollbar to match the glass design */
.scroll-panel::-webkit-scrollbar {
width: 6px;
}
.scroll-panel::-webkit-scrollbar-track {
background: transparent;
}
.scroll-panel::-webkit-scrollbar-thumb {
background: rgba(255, 255, 255, 0.2);
border-radius: 3px;
}
.scroll-panel::-webkit-scrollbar-thumb:hover {
background: rgba(255, 255, 255, 0.4);
}
Best Practices & Common Mistakes
- Avoid Horizontal Viewport Scroll: The most common mobile layout bug is a horizontal scrollbar on the main
bodyelement. This is usually caused by elements with hardcoded widths or negative margins that push past the viewport width. To fix this, usemax-width: 100%on child elements and avoid declaringoverflow-x: hiddenon thebodyorhtmlelements to mask the issue, as this can break sticky positioning. - Scroll Bar Layout Shifts: Adding scrollbars on hover can shift content layout as the scrollbar track takes up space. To prevent this, use
scrollbar-gutter: stable;to reserve space for the scrollbar track ahead of time.
FAQs
Q: What is the difference between overflow: scroll and overflow: auto?
A: scroll always renders scrollbar tracks, even if the content fits inside the container. auto only displays scrollbar tracks when the content actually overflows the container bounds.
Q: Can I style scrollbars on Firefox? A: Yes. Firefox uses modern standardized properties:
.firefox-scroll {
scrollbar-width: thin;
scrollbar-color: #3b82f6 #1e293b; /* thumb color | track color */
}
Summary
CSS overflow properties manage content boundaries. While visible lets content spill out, hidden clips content, and scroll/auto enable scrolling viewports. Scrollbars can also be styled using vendor pseudo-elements or standardized rules.
Related Articles
Next Tutorial
Now that we have mastered sizing elements and their boxes, how do we arrange and align them across the page document flow? Let's check: CSS Display Properties.