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.
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 respect border-radius, though it blocks position: sticky inside).scroll: Forces scrollbar tracks visible at all times even when content already fits.auto: Displays scrollbars only when content actually overflows—making overflow-x: auto; the standard choice for wide pre code blocks and tables.clip: Clips overflowing visuals like hidden without creating a scroll container, preserving position: sticky on 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;
}
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.
overflow-x: hidden on html or body to hide layout bugs: Fix the overflowing element instead, and remember overflow-x: hidden on body breaks position: sticky. Use overflow-x: clip if guarding decorative shapes.overflow: scroll instead of auto: Permanent gray scrollbar tracks look clunky on Windows when boxes have plenty of space.overflow-wrap: break-word; for long URLs: Unbroken 80-character URLs push containers wide unless wrapped cleanly.Always prefer overflow: auto because scrollbars appear only when needed, whereas scroll renders disabled scrollbar tracks even on short content.
Unlike overflow: hidden, overflow: clip trims horizontal bleed without forming a new scroll container, so position: sticky keeps working.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.
You've completed this section! Take a quick 5-question quiz to check your understanding.