Lesson

CSS Overflow

10 min reading
Free Course

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 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;
}

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: 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.
  • Using overflow: scroll instead of auto: 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.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
Lesson Recap Quiz Available

Test Your Knowledge

You've completed this section! Take a quick 5-question quiz to check your understanding.

CSS Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum