Discover how a simple size setting can save hours of layout headaches!
Why Flex basis and sizing in Tailwind? - Purpose & Use Cases
Imagine you want to arrange boxes side by side on a webpage, each with a specific starting size. You try to set their widths manually using fixed pixel values for each box.
When the screen size changes or you add more boxes, you must recalculate and rewrite all widths. This is slow, error-prone, and the layout breaks easily on different devices.
Flex basis and sizing let you set a starting size for each box that flexibly adjusts as the screen changes. Tailwind classes make this easy and responsive without manual math.
<div style="width: 200px; float: left;">...</div><div class="flex"><div class="basis-1/3">...</div><div class="basis-1/3">...</div></div>
You can create flexible, responsive layouts that adapt smoothly to any screen size without rewriting widths.
Building a photo gallery where each image box starts at one-third width but shrinks or grows nicely on phones, tablets, and desktops.
Manual fixed widths break on different screen sizes.
Flex basis sets a flexible starting size for items.
Tailwind classes make responsive sizing simple and reliable.
Practice
basis-1/3 do in a flex container?Solution
Step 1: Understand flex basis meaning
Flex basis sets the starting size of a flex item before growing or shrinking.Step 2: Interpret
This class sets the flex basis to one-third of the container's main size, not forcing exact size but initial size.basis-1/3in TailwindFinal Answer:
Sets the initial size of the flex item to one-third of the container's main size -> Option BQuick Check:
Flex basis = initial size = one-third [OK]
- Confusing basis with grow or shrink
- Thinking basis fixes size exactly
- Assuming basis prevents shrinking
Solution
Step 1: Recall Tailwind sizing scale
Tailwind uses numeric scale where64equals 16rem (4rem * 16).Step 2: Match class syntax
The correct class isbasis-64to set basis to 16rem, no 'rem' suffix used.Final Answer:
basis-64 -> Option DQuick Check:
basis-64 = 16rem in Tailwind [OK]
- Adding 'rem' suffix in class name
- Using wrong numeric scale
- Confusing basis with width classes
<div class="flex w-96"> <div class="basis-1/4 bg-blue-500">Box 1</div> <div class="basis-1/2 bg-red-500">Box 2</div> <div class="basis-1/4 bg-green-500">Box 3</div> </div>
What will be the width of
Box 2 inside the 24rem wide container?Solution
Step 1: Calculate container width in rem
The container width is 24rem (w-96 means 24rem).Step 2: Calculate Box 2 basis size
Box 2 hasbasis-1/2, so initial size is half of 24rem = 12rem.Final Answer:
12rem -> Option CQuick Check:
Half of 24rem = 12rem [OK]
- Confusing basis fraction with grow factor
- Using container width in px instead of rem
- Adding basis sizes instead of calculating individually
<div class="flex"> <div class="basis-2/3">Item 1</div> <div class="basis-1/2">Item 2</div> </div>
But the items overflow the container width. What is the main reason?
Solution
Step 1: Calculate the sum of basis fractions
2/3 + 1/2 = 4/6 + 3/6 = 7/6 > 1.Step 2: Understand the effect
When the sum of flex-basis values exceeds 1, it causes the flex items to overflow the container.Final Answer:
The sum of basis sizes (2/3 + 1/2) exceeds 1, causing overflow -> Option AQuick Check:
Sum of basis fractions > 1 causes overflow [OK]
- Assuming basis fractions always fit container
- Ignoring default flex shrink behavior
- Confusing grow with shrink effects
Solution
Step 1: Set correct basis classes
Usebasis-1/4for 25% andbasis-1/2for 50% widths.Step 2: Apply grow classes
Tailwind usesgrowfor flex-grow:1 and[flex-grow:2]for flex-grow:2.Step 3: Assign grow classes to items
First and third items getgrow, middle item gets[flex-grow:2]to grow twice as fast.Final Answer:
basis-1/4 grow,basis-1/2 [flex-grow:2],basis-1/4 grow-> Option AQuick Check:
Usegrowand[flex-grow:2]with basis fractions [OK]
- Using non-existent flex-grow-1 class
- Combining grow and grow-2 on same element
- Forgetting to set basis sizes
Ready to prove it?
Pass the test and this pattern moves unknown
