Skip to content

Tailwind CSS - Overflow

Tailwind’s overflow utilities let you control how content is handled when it exceeds the boundaries of its container. You can choose to clip the content, hide it, or display scrollbars to make it accessible.

Here is a comprehensive list of Tailwind’s overflow utilities. They can be applied to a container to manage its content flow both horizontally and vertically.

ClassCSS Properties
overflow-autooverflow: auto;
overflow-hiddenoverflow: hidden;
overflow-clipoverflow: clip;
overflow-visibleoverflow: visible;
overflow-scrolloverflow: scroll;
overflow-x-autooverflow-x: auto;
overflow-y-autooverflow-y: auto;
overflow-x-hiddenoverflow-x: hidden;
overflow-y-hiddenoverflow-y: hidden;
overflow-x-clipoverflow-x: clip;
overflow-y-clipoverflow-y: clip;
overflow-x-visibleoverflow-x: visible;
overflow-y-visibleoverflow-y: visible;
overflow-x-scrolloverflow-x: scroll;
overflow-y-scrolloverflow-y: scroll;

The best way to understand these utilities is to see them in action. The example below shows a grid of containers, each with the same overflowing content but a different overflow utility applied. Notice the difference in how the text is handled in each case.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Overflow Comparison</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 p-8 font-sans">
<div class="container mx-auto">
<h1 class="text-3xl font-bold mb-6 text-center">Tailwind CSS Overflow Utilities</h1>
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8">
<!-- Base text for demonstration -->
<div id="lorem-ipsum" class="hidden">
<p class="text-sm text-gray-800">
This is a long piece of content designed to overflow its container. By applying different Tailwind CSS overflow classes, we can control whether this content is clipped, hidden, or made scrollable. This demonstrates the power and flexibility of utility-first CSS for handling common layout challenges.
</p>
</div>
<!-- Overflow Cards -->
<div class="overflow-card">
<h3 class="font-bold mb-2">overflow-auto</h3>
<div id="box1" class="box overflow-auto"></div>
</div>
<div class="overflow-card">
<h3 class="font-bold mb-2">overflow-hidden</h3>
<div id="box2" class="box overflow-hidden"></div>
</div>
<div class="overflow-card">
<h3 class="font-bold mb-2">overflow-visible</h3>
<div id="box3" class="box overflow-visible"></div>
</div>
<div class="overflow-card">
<h3 class="font-bold mb-2">overflow-scroll</h3>
<div id="box4" class="box overflow-scroll"></div>
</div>
<div class="overflow-card">
<h3 class="font-bold mb-2">overflow-clip</h3>
<div id="box5" class="box overflow-clip"></div>
</div>
<div class="overflow-card">
<h3 class="font-bold mb-2">overflow-x-scroll</h3>
<div id="box6" class="box overflow-x-scroll whitespace-nowrap"></div>
</div>
</div>
</div>
<style>
.overflow-card { background-color: white; padding: 1rem; border-radius: 0.5rem; box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1); }
.box { height: 6rem; width: 100%; background-color: #f3f4f6; border: 1px solid #d1d5db; padding: 0.5rem; }
</style>
<script>
// Use JavaScript to populate the boxes with the same content
document.addEventListener('DOMContentLoaded', () => {
const content = document.getElementById('lorem-ipsum').innerHTML;
for (let i = 1; i <= 6; i++) {
document.getElementById(`box${i}`).innerHTML = content;
}
});
</script>
</body>
</html>
  • overflow-hidden: Commonly used for containing elements with rounded corners (rounded-lg overflow-hidden) to ensure child elements don’t spill outside the border radius. It’s also fundamental for creating CSS masks and clearing floats in older layout models.
  • overflow-auto: The most practical choice for user-generated content or dynamic data. It only shows scrollbars when necessary, providing a clean UI.
  • overflow-y-auto: Perfect for creating scrollable sidebars, navigation panels, or chat windows that need to scroll vertically but not horizontally.
  • overflow-x-auto: Ideal for displaying wide tables on small screens or for preformatted code blocks (<pre>) that should not wrap.
  • overflow-clip vs. overflow-hidden: overflow-clip is a newer, more performant alternative to overflow-hidden. It prevents any kind of scrolling, including programmatic scrolling. Use overflow-clip when you are certain no scrolling is ever needed.

Problem: overflow-hidden is not working. Solution: Check for absolutely positioned child elements. position: absolute can cause an element to escape the overflow context of its parent. Also, ensure the container has a defined height or max-height, as an element with no height constraint won’t have any content to ‘overflow’.