Overflow & Scroll — Tailwind CSS

Overflow is content that is bigger than its box. An overflow-* class decides what happens to that overflowing content: overflow-visible (the default) lets it…

A family WhatsApp group can hold hundreds of messages, yet the chat screen doesn't grow taller. Its height stays fixed, and you swipe up to read older messages. How does that work?

Content that is bigger than its box is called overflow. Say the box has a fixed height, but the content is too long. The part that doesn't fit can go outside the box, be cut off, or become scrollable (you can move it inside the box). The WhatsApp chat screen uses the last choice. In Tailwind, that choice is the class overflow-y-auto: the box stays the same, and its content can be scrolled.

Four choices for content that overflows

This is the same box as in the Lab and the exercise:

<div id="scroll-box" class="overflow-y-auto h-32 p-4">
  <p>Paragraph 1</p>
  <p>Paragraph 2</p>
  <p>Paragraph 3</p>
  <p>Paragraph 4</p>
  <p>Paragraph 5</p>
  <p>Paragraph 6</p>
</div>

Calculate the sizes first:

Part Size
One paragraph (16px text, line height 1.5) 24px
Six paragraphs 6 × 24 = 144px
The box's height, h-32 128px
Top + bottom padding, p-4 16 + 16 = 32px
Room for the content 128 - 32 = 96px
What overflows 144 - 96 = 48px, about 2 paragraphs

The 128px height already includes the padding, because Tailwind gives every element box-sizing: border-box. So 48px of the content doesn't fit. What happens to those 48px is decided by an overflow-* class:

Class CSS The overflowing content
overflow-visible overflow: visible (default) goes outside the box and covers whatever is below it
overflow-hidden overflow: hidden is cut off, not visible at all
overflow-scroll overflow: scroll can be scrolled, a scrollbar (the bar you drag to scroll) is always added, even when the content fits
overflow-auto overflow: auto can be scrolled, the scrollbar only appears when needed

For a box that should scroll, overflow-auto is nearly always the right choice. With overflow-scroll, an empty scrollbar stays visible on Windows, even when the content fits.

One direction only: overflow-x and overflow-y

Scrolling needs a height

A plain <div> grows to fit its content. Without h-32, the #scroll-box becomes 144 + 32 = 176px tall. Every paragraph fits and nothing overflows, so there's nothing to scroll. The scrollbar doesn't show up. overflow-y-auto only means "if it overflows, make it scrollable". The fixed height is what makes the content overflow.

Another choice: max-h-32, which writes a max-height (the tallest the box may grow) of 128px. The box may grow with its content up to 128px, and only scrolls when the content is taller than that. Good for a list that's sometimes short, sometimes long.

Text that's too long: truncate and line-clamp

For text that is longer than its box, there are two classes:

<p class="truncate">Learn Tailwind from zero until you can build your own landing page</p>
<p class="line-clamp-2">A long product description, cut off after two lines...</p>
<div class="flex w-64 gap-3">
  <div class="size-10 rounded-full bg-gray-300"></div>
  <div class="flex-1 min-w-0">
    <p class="truncate">Learn Tailwind from zero until you can build your own landing page</p>
  </div>
</div>

Now the title item is 204px (what is left of 256px after the 40px photo and the 12px gap-3), the profile photo (size-10) stays 40px, and the title ends in "...". If truncate sits on the flex item itself, min-w-0 isn't needed.

💡 Nicer scrolling: snap and smooth

This is a row of three slides that can be scrolled sideways:

<div class="flex gap-4 overflow-x-auto snap-x snap-mandatory">
  <div class="w-64 shrink-0 snap-center">Slide 1</div>
  <div class="w-64 shrink-0 snap-center">Slide 2</div>
  <div class="w-64 shrink-0 snap-center">Slide 3</div>
</div>

⚠️ Scrolling that won't appear, and cuts you didn't mean

TL;DR: Overflow is content that is bigger than its box. An overflow-* class decides what happens to that overflowing content: overflow-visible (the default) lets it go outside the box, overflow-hidden cuts it off, and overflow-scroll and overflow-auto make it scrollable. The difference: overflow-auto only shows a scrollbar (the bar you drag to scroll) when needed. Add -x- or -y- for one direction only, like overflow-y-auto for up and down. Scrolling needs a fixed height (h-32 = 128px) or a height limit (max-h-32). Without one, the box just grows and nothing overflows. For text that's too long: truncate cuts it to one line ending in "...", and line-clamp-2 limits it to two lines.

⚡ The box stays, the content scrolls

Write:

<div id="scroll-box" class="overflow-y-auto h-32 p-4">
  <p>Paragraph 1</p>
  <p>Paragraph 2</p>
  <p>Paragraph 3</p>
  <p>Paragraph 4</p>
  <p>Paragraph 5</p>
  <p>Paragraph 6</p>
</div>

Meaning: h-32 fixes the box's height at 128px, so six paragraphs (144px) don't fit inside it. overflow-y-auto lets the overflowing content scroll up and down, and the scrollbar (the bar you drag to scroll) only appears when needed.

In the exercise, the checker (the program that checks your answer) looks for overflow-y set to auto, 16px of padding (p-4), and a height of 128px (h-32).

⚡ Scrolling needs a height

Guess first: remove h-32 from that box. Does the scrollbar appear?

No. The box grows until every paragraph fits, so nothing overflows.

Meaning: Want a box that may grow first? Use max-h-32 (at most 128px tall). The box follows the height of its content, and only scrolls once the content is taller than 128px.

⚡ Cut it, or scroll it

Meaning: overflow-hidden cuts off what overflows, with no scrolling. overflow-scroll always adds a scrollbar, even when the content fits, so overflow-auto (the scrollbar only appears when needed) is nearly always the better choice. For text that's too long, truncate cuts it to one line ending in "...".

What you will learn