Flex grow and shrink help control how items inside a container get bigger or smaller to fill space nicely.
Flex grow and shrink in Tailwind
flex-grow-{number}
flex-shrink-{number}flex-grow-{number} sets how much an item can grow compared to others.
flex-shrink-{number} sets how much an item can shrink compared to others.
<div class="flex"> <div class="flex-grow-0">No grow</div> <div class="flex-grow">Grow</div> </div>
<div class="flex"> <div class="flex-shrink-0">No shrink</div> <div class="flex-shrink">Shrink</div> </div>
<div class="flex"> <div class="flex-grow-1">Grow 1</div> <div class="flex-grow-2">Grow 2</div> </div>
This page shows two rows. The first row has three boxes: two grow with different rates and one does not grow. The second row has two boxes: one that never shrinks and one that shrinks if space is tight. Resize the browser to see how they behave.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Flex Grow and Shrink Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="p-6"> <h1 class="text-xl font-bold mb-4">Flex Grow and Shrink Demo</h1> <div class="flex border border-gray-400 h-20 mb-6"> <div class="bg-blue-400 flex-grow text-white flex items-center justify-center">Grow 1</div> <div class="bg-green-400 flex-grow-2 text-white flex items-center justify-center">Grow 2</div> <div class="bg-red-400 flex-grow-0 text-white flex items-center justify-center">No Grow</div> </div> <div class="flex border border-gray-400 h-20 max-w-xs overflow-hidden"> <div class="bg-purple-400 flex-shrink-0 text-white flex items-center justify-center w-40">No Shrink</div> <div class="bg-yellow-400 flex-shrink text-black flex items-center justify-center w-40">Shrink</div> </div> </body> </html>
Use flex-grow-0 to prevent an item from growing.
Use flex-shrink-0 to prevent an item from shrinking.
Tailwind's default flex-grow and flex-shrink classes use 1 as the value.
Flex grow controls how much an item grows to fill extra space.
Flex shrink controls how much an item shrinks when space is tight.
Tailwind makes it easy to set these with simple classes like flex-grow-1 or flex-shrink-0.
Practice
flex-grow-0 do to a flex item?Solution
Step 1: Understand flex-grow property
The flex-grow property controls how much a flex item can grow relative to others when extra space is available.Step 2: Interpret the class flex-grow-0
The classflex-grow-0sets flex-grow to 0, meaning the item will not grow to fill extra space.Final Answer:
Prevents the item from growing to fill extra space -> Option AQuick Check:
flex-grow-0 means no growth [OK]
- Confusing grow with shrink
- Thinking flex-grow-0 allows growth
- Mixing flex-grow with flex-shrink
Solution
Step 1: Understand flex-shrink property
The flex-shrink property controls how much a flex item shrinks when space is limited.Step 2: Identify the class that disables shrinking
The classflex-shrink-0sets flex-shrink to 0, preventing the item from shrinking.Final Answer:
flex-shrink-0 -> Option DQuick Check:
flex-shrink-0 means no shrinking [OK]
- Choosing flex-grow classes instead of flex-shrink
- Assuming flex-shrink-1 prevents shrinking
- Mixing grow and shrink properties
<div class="flex w-64"> <div class="flex-grow-0 w-16 bg-blue-500">A</div> <div class="flex-grow w-32 bg-red-500">B</div> </div>
What will happen to the widths of the two divs inside the flex container?
Solution
Step 1: Analyze flex-grow classes
Div A hasflex-grow-0so it won't grow beyond its width 16. Div B hasflex-grow(equivalent to flex-grow-1) so it will grow to fill leftover space.Step 2: Understand container width and item widths
The container is 64 width. A uses 16 width fixed, B starts at 32 but grows to fill remaining space (64 - 16 = 48 or more if allowed).Final Answer:
A stays 16 width, B grows to fill remaining space -> Option BQuick Check:
flex-grow-0 no grow, flex-grow fills space [OK]
- Assuming flex-grow-0 items grow
- Thinking both grow equally without flex-grow class
- Ignoring container width limits
<div class="flex w-40"> <div class="flex-shrink-0 w-24 bg-green-400">X</div> <div class="flex-shrink w-24 bg-yellow-400">Y</div> </div>
But the second div overflows the container. What is the problem?
Solution
Step 1: Check widths and container size
Container width is 40. Both divs have width 24, total 48, which is more than container.Step 2: Analyze flex-shrink classes
First div hasflex-shrink-0, so it cannot shrink below 24. Second div has default shrink, so it tries to shrink but can't reduce enough to fit container.Final Answer:
flex-shrink-0 on first div prevents it from shrinking, causing overflow -> Option CQuick Check:
flex-shrink-0 stops shrinking, causes overflow [OK]
- Thinking flex-shrink on second div causes overflow
- Ignoring container width limits
- Assuming flex-grow affects shrinking
Solution
Step 1: Understand proportional growth with flex-grow
Flex-grow values determine how leftover space is divided. A value of 2 means twice the space compared to 1.Step 2: Assign classes to match proportions
First item needs flex-grow-2, second flex-grow-1, third flex-grow-0 to not grow.Final Answer:
First: flex-grow-2, Second: flex-grow-1, Third: flex-grow-0 -> Option AQuick Check:
flex-grow values set proportional growth [OK]
- Using wrong grow numbers or order
- Assigning flex-grow-0 to growing items
- Confusing grow with shrink classes
Ready to prove it?
Pass the test and this pattern moves unknown
