← Back to Chapters

CSS Overflow

? CSS Overflow

⚡ Quick Overview

The overflow property in CSS controls what happens when content is too large to fit inside its box. You can choose to:

  • Let extra content be visible outside the box.
  • Hide the overflowing content.
  • Add scrollbars so the user can scroll.
  • Automatically show scrollbars only when needed.

? Key Concepts

  • Overflow area: Content that does not fit inside the element’s width/height.
  • Scrollbars: Shown when content is scrollable using scroll or auto.
  • Axes: You can control overflow on the x-axis (overflow-x) and y-axis (overflow-y) separately.
  • Requires dimensions: For overflow to be visible, hidden, or scrollable, the element usually needs a fixed width and/or height.

? Common Use Cases

  • Scrollable chat windows, cards, or sidebars.
  • Code blocks with long lines of text.
  • Image galleries where the images overflow their container.
  • Modals with long content on small screens.

? Syntax of CSS Overflow

Basic syntax to control overflow on both axes:

? View Code Example
/* Basic overflow shorthand */
selector {
  overflow: visible;   /* visible | hidden | scroll | auto */
}

/* Axis-specific control */
selector {
  overflow-x: auto;    /* horizontal overflow */
  overflow-y: hidden;  /* vertical overflow */
}

The overflow property can take these main values:

  • visible — Default; extra content is visible outside the box.
  • hidden — Extra content is clipped and not visible.
  • scroll — Content is clipped, and scrollbars are always shown.
  • auto — Scrollbars appear only if content actually overflows.

? Practical CSS Example

Here is a set of boxes with different overflow values applied:

? View Code Example
/* Different overflow behaviors for demo boxes */
.container-visible {
  overflow: visible;
  width: 200px;
  height: 100px;
  background-color: lightblue;
}

.container-hidden {
  overflow: hidden;
  width: 200px;
  height: 100px;
  background-color: lightgreen;
}

.container-scroll {
  overflow: scroll;
  width: 200px;
  height: 100px;
  background-color: lightcoral;
}

.container-auto {
  overflow: auto;
  width: 200px;
  height: 100px;
  background-color: lightyellow;
}

? HTML Structure for the Demo

This HTML uses the above classes to show how content behaves when it overflows:

? View Code Example
<!-- Overflow demo containers with long content -->
<div class="container-visible">
  This is some very long text that will overflow the box
  and remain fully visible outside its boundaries.
</div>

<div class="container-hidden">
  This is some very long text that will overflow the box,
  but anything that does not fit will be clipped.
</div>

<div class="container-scroll">
  This is some very long text that will overflow the box,
  and scrollbars will always be visible for scrolling.
</div>

<div class="container-auto">
  This is some very long text that will overflow the box,
  and scrollbars will appear only if needed.
</div>

? Live Output / Explanation

Imagine the four boxes rendered one below another (each 200px wide and 100px tall) with long text inside:

  • Visible: Text spills out of the box and remains fully readable outside the edges.
  • Hidden: Text that does not fit within the 100px height is cut off and cannot be seen.
  • Scroll: The content is clipped to the box, and scrollbars are always visible so the user can scroll.
  • Auto: Scrollbars appear only if the text is long enough to overflow the box.

? Why Use the Overflow Property?

  • Manage large content: Control how extra content behaves when it exceeds its container.
  • Improve layout: Create scrollable sections like cards, modals, sidebars, or chat windows.
  • Prevent layout breaks: Keep long text or elements from breaking your design by spilling out unexpectedly.

? Tips & Best Practices

  • Always set a fixed width and/or height when expecting overflow behavior.
  • Use overflow: auto instead of scroll to avoid always-visible scrollbars.
  • Prefer overflow-x or overflow-y when you only need scrolling on one axis.
  • Check your design on small screens to ensure overflow areas are usable and readable.

? Try It Yourself

  • Create a <div> with a fixed height of 80px and experiment with overflow: visible, hidden, scroll, and auto.
  • Make a chat-like box using overflow-y: auto and add multiple message lines to test scrolling.
  • Build a horizontal image list using white-space: nowrap; and overflow-x: scroll; to create a sideways scroller.
  • Observe how changing only overflow-x vs overflow-y affects scrollbars.