Flex wrap helps items in a container move to the next line when there is not enough space. This keeps your layout neat and readable on all screen sizes.
0
0
Flex wrap behavior in Tailwind
Introduction
When you want items in a row to wrap to the next line on smaller screens.
When you have many buttons or images that should not shrink too small.
When you want a responsive gallery that adjusts to screen width.
When you want to avoid horizontal scrolling by wrapping content.
When you want to control how items break into new lines inside a flex container.
Syntax
Tailwind
flex-wrap flex-nowrap flex-wrap-reverse
flex-wrap allows items to wrap onto multiple lines.
flex-nowrap keeps all items on one line (default).
flex-wrap-reverse wraps items onto multiple lines in reverse order.
Examples
This container uses
flex-wrap so boxes wrap to next line if needed.Tailwind
<div class="flex flex-wrap"> <div class="w-40 h-20 bg-blue-400 m-1">Box 1</div> <div class="w-40 h-20 bg-green-400 m-1">Box 2</div> <div class="w-40 h-20 bg-red-400 m-1">Box 3</div> <div class="w-40 h-20 bg-yellow-400 m-1">Box 4</div> </div>
This container uses
flex-nowrap so all boxes stay on one line and may overflow horizontally.Tailwind
<div class="flex flex-nowrap"> <div class="w-40 h-20 bg-blue-400 m-1">Box 1</div> <div class="w-40 h-20 bg-green-400 m-1">Box 2</div> <div class="w-40 h-20 bg-red-400 m-1">Box 3</div> <div class="w-40 h-20 bg-yellow-400 m-1">Box 4</div> </div>
This container uses
flex-wrap-reverse so wrapped lines appear above the first line.Tailwind
<div class="flex flex-wrap-reverse"> <div class="w-40 h-20 bg-blue-400 m-1">Box 1</div> <div class="w-40 h-20 bg-green-400 m-1">Box 2</div> <div class="w-40 h-20 bg-red-400 m-1">Box 3</div> <div class="w-40 h-20 bg-yellow-400 m-1">Box 4</div> </div>
Sample Program
This example shows a flex container with flex-wrap. When the window is narrow, boxes move to the next line to stay fully visible. This keeps the layout clean and easy to read.
Tailwind
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Flex Wrap Behavior Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="p-6"> <h1 class="text-xl font-semibold mb-4">Flex Wrap Behavior Demo</h1> <p class="mb-4">Resize the browser window to see how boxes wrap inside the container.</p> <section aria-label="Flex wrap example"> <div class="flex flex-wrap border border-gray-400 p-2"> <div class="w-40 h-20 bg-blue-400 m-1 flex items-center justify-center text-white font-bold">Box 1</div> <div class="w-40 h-20 bg-green-400 m-1 flex items-center justify-center text-white font-bold">Box 2</div> <div class="w-40 h-20 bg-red-400 m-1 flex items-center justify-center text-white font-bold">Box 3</div> <div class="w-40 h-20 bg-yellow-400 m-1 flex items-center justify-center text-black font-bold">Box 4</div> </div> </section> </body> </html>
OutputSuccess
Important Notes
Use flex-wrap to make your layout responsive and avoid horizontal scrolling.
flex-nowrap can cause overflow if items don't fit in one line.
flex-wrap-reverse changes the order of wrapped lines visually.
Summary
Flex wrap controls if flex items stay in one line or wrap to new lines.
Use flex-wrap to keep content neat on small screens.
Tailwind classes: flex-wrap, flex-nowrap, flex-wrap-reverse.