CSS Overflow
CSS Overflow: Controlling Scrollbars and Clipped Content
CSS overflow decides what happens when text, code blocks, or wide data tables outgrow their parent container's width or height. Choosing between auto, hidden, scroll, and clip prevents broken mobile layouts and unwanted permanent scrollbars.
Comparing CSS Overflow Values
visible(default): Content spills outside the container boundary without clipping or scrollbars.hidden: Clips overflowing content cleanly without scrollbars (useful on rounded cards so child images respectborder-radius, though it blocksposition: stickyinside).scroll: Forces scrollbar tracks visible at all times even when content already fits.auto: Displays scrollbars only when content actually overflows—makingoverflow-x: auto;the standard choice for wideprecode blocks and tables.clip: Clips overflowing visuals likehiddenwithout creating a scroll container, preservingposition: stickyon descendants.
.code-block-shell {
max-width: 100%;
overflow-x: auto;
background-color: #0f172a;
color: #f8fafc;
padding: 16px;
border-radius: 8px;
}
.card-rounded {
overflow: hidden;
border-radius: 12px;
border: 1px solid #e2e8f0;
}
.chat-history {
max-height: 280px;
overflow-y: auto;
overscroll-behavior: contain;
}
Controlling Axis Independently
Use overflow-x and overflow-y when scrolling should happen in only one direction—such as scrollable modals (overflow-y: auto) or wide code previews (overflow-x: auto). Adding overscroll-behavior: contain; stops a scrollable sidebar from scrolling the main background page once it reaches the bottom.
Common Mistakes to Avoid
- Slapping
overflow-x: hiddenonhtmlorbodyto hide layout bugs: Fix the overflowing element instead, and rememberoverflow-x: hiddenonbodybreaksposition: sticky. Useoverflow-x: clipif guarding decorative shapes. - Using
overflow: scrollinstead ofauto: Permanent gray scrollbar tracks look clunky on Windows when boxes have plenty of space. - Forgetting
overflow-wrap: break-word;for long URLs: Unbroken 80-character URLs push containers wide unless wrapped cleanly.
Frequently Asked Questions
When should I use overflow: auto vs overflow: scroll?
Always prefer overflow: auto because scrollbars appear only when needed, whereas scroll renders disabled scrollbar tracks even on short content.
Why does overflow: clip help sticky navigation bars?
Unlike overflow: hidden, overflow: clip trims horizontal bleed without forming a new scroll container, so position: sticky keeps working.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.
Test Your Knowledge
You've completed this section! Take a quick 5-question quiz to check your understanding.