Skip to content

Tailwind CSS - Flex Shrink

The flex-shrink utilities in Tailwind CSS control how a flex item shrinks relative to other items in the same flex container when there isn’t enough space. It’s a fundamental part of creating responsive and robust flexbox layouts.

By default, flex items are allowed to shrink (flex-shrink: 1). Tailwind provides utilities to control this behavior.

ClassCSS Properties
shrinkflex-shrink: 1;
shrink-0flex-shrink: 0;
  • shrink: (Default behavior) Allows a flex item to shrink if the flex container runs out of space. Items with a higher shrink factor will shrink more.
  • shrink-0: Prevents a flex item from shrinking. The item will maintain its original width, potentially overflowing the container if space is limited.

Practical Application: Building a Responsive Header

Section titled “Practical Application: Building a Responsive Header”

A classic use case for flex-shrink is in a site header. You typically want the logo and user avatar to maintain their size, while the navigation links or search bar can shrink to fit on smaller screens.

In this example, we’ll create a header with a logo, a search bar, and a user profile section. The search bar will be allowed to shrink, but the logo and profile will not.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tailwind Flex Shrink Example</title>
<!-- Assuming a modern build setup with generated CSS -->
<link href="./output.css" rel="stylesheet">
<script src="https://kit.fontawesome.com/a076d05399.js" crossorigin="anonymous"></script> <!-- for icons -->
</head>
<body class="bg-gray-100 p-4 font-sans">
<h1 class="text-2xl font-bold text-center mb-4">Responsive Header Component</h1>
<div class="max-w-4xl mx-auto border rounded-lg p-4 bg-gray-50">
<p class="text-center mb-4 text-gray-600">Resize your browser window to see the effect.</p>
<!-- Header Container -->
<header class="bg-white p-4 rounded-lg shadow-md">
<div class="flex items-center space-x-4">
<!-- Logo: Should not shrink -->
<div class="flex-shrink-0">
<img src="https://tailwindui.com/img/logos/workflow-logo-indigo-600-mark-gray-800-text.svg" alt="Logo" class="h-8 w-auto">
</div>
<!-- Search Bar: Will shrink to fit available space -->
<div class="flex-1 min-w-0">
<div class="relative">
<input type="text" placeholder="Search for anything..."
class="w-full p-2 border border-gray-300 rounded-md focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 transition">
</div>
</div>
<!-- User Profile: Should not shrink -->
<div class="flex-shrink-0 flex items-center space-x-3">
<button class="p-2 rounded-full hover:bg-gray-200 text-gray-600">
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6 6 0 10-12 0v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9" /></svg>
</button>
<img class="h-10 w-10 rounded-full" src="https://source.unsplash.com/random/100x100?portrait" alt="User avatar">
</div>
</div>
</header>
</div>
<h2 class="text-2xl font-bold text-center mt-12 mb-4">Demonstration</h2>
<!-- Visualizing shrink and shrink-0 -->
<div class="max-w-4xl mx-auto border rounded-lg p-4 bg-white shadow-md">
<p class="mb-2 font-semibold">Container with one shrinking item:</p>
<div class="flex bg-gray-200 p-2 rounded">
<div class="shrink-0 p-4 bg-red-400 text-white rounded w-48 text-center">`shrink-0` (fixed width)</div>
<div class="shrink p-4 bg-blue-500 text-white rounded w-96 text-center">`shrink` (will shrink)</div>
<div class="shrink-0 p-4 bg-red-400 text-white rounded w-48 text-center">`shrink-0` (fixed width)</div>
</div>
</div>
</body>
</html>
  • Combining with flex-basis and flex-grow: The flex-shrink property is part of the flex shorthand. For more complex control, you might use utilities like flex-1 (which sets flex: 1 1 0%) or flex-auto (flex: 1 1 auto).
  • Minimum Width: A shrinking item will shrink indefinitely unless a minimum width is set. Use min-w-* utilities (e.g., min-w-0) on shrinking elements to prevent them from collapsing completely and causing content overflow issues. In our header example, min-w-0 on the search bar’s parent div is crucial.
  • Responsive Design: Use responsive prefixes to change shrink behavior at different breakpoints. For example, an item might be shrink-0 on mobile (sm:shrink-0) but allowed to shrink on larger screens.