Grid auto-flow helps place items automatically in a grid layout. It decides how new items fill rows or columns without you placing each one manually.
Grid auto-flow and placement in Tailwind
grid-auto-flow: row | column | dense | row dense | column dense;
row means items fill rows first (default).
column means items fill columns first.
dense tries to fill gaps if possible.
grid-auto-flow: row;
grid-auto-flow: column;
grid-auto-flow: row dense;
grid-auto-flow: column dense;
This example shows a grid with 4 columns. Item 2 spans 2 columns, creating a gap. Because of grid-flow-row-dense, later items fill gaps tightly.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Grid Auto-flow Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="p-6"> <h1 class="text-xl font-bold mb-4">Grid Auto-flow: row dense</h1> <div class="grid grid-cols-4 grid-flow-row-dense gap-4 border p-4"> <div class="bg-blue-400 text-white p-4">1</div> <div class="bg-green-400 text-white p-4 col-span-2">2 (col-span-2)</div> <div class="bg-red-400 text-white p-4">3</div> <div class="bg-yellow-400 text-black p-4">4</div> <div class="bg-purple-400 text-white p-4">5</div> <div class="bg-pink-400 text-white p-4">6</div> </div> </body> </html>
Tailwind uses classes like grid-flow-row, grid-flow-col, and grid-flow-row-dense to control flow.
Use col-span-* or row-span-* to make items bigger and see how auto-flow fills gaps.
Try resizing the browser to see how the grid adapts with auto-flow.
Grid auto-flow controls how items fill rows or columns automatically.
Use row or column to choose direction, and dense to fill gaps.
Tailwind classes make it easy to apply these rules without writing CSS.
Practice
grid-flow-row do in a grid container?Solution
Step 1: Understand grid auto-flow direction
The classgrid-flow-rowsets the grid to place items in rows first, meaning items fill left to right in each row.Step 2: Confirm placement order
After filling a row, the next items start on the next row below, continuing left to right again.Final Answer:
It places grid items in rows from left to right, then top to bottom. -> Option AQuick Check:
grid-flow-row = row-wise placement [OK]
- Confusing row flow with column flow
- Thinking dense is automatic without class
- Assuming grid items stack vertically by default
Solution
Step 1: Identify correct Tailwind syntax for column flow
The classgrid-flow-colsets the grid to fill columns first, placing items top to bottom in each column.Step 2: Verify other options
Optionsgrid-auto-flow-colandgrid-auto-columnsare not valid Tailwind classes for this purpose.Final Answer:
grid-flow-col -> Option CQuick Check:
grid-flow-col = column-wise placement [OK]
- Using incorrect class names like grid-auto-flow-col
- Mixing up row and column flow classes
- Assuming grid-auto-columns controls flow
<div class="grid grid-cols-3 grid-flow-row-dense gap-2"> <div class="col-span-2">A</div> <div>B</div> <div>C</div> <div>D</div> </div>
How will the items be placed visually?
Solution
Step 1: Understand grid-flow-row-dense behavior
Thegrid-flow-row-denseclass places items row-wise but tries to fill gaps left by spanning items.Step 2: Analyze item placement
Item A spans 2 columns in row 1, leaving the 3rd column free. Item B fits in that 3rd column in row 1. Items C and D go to the next row filling columns 1 and 2.Final Answer:
A spans first two columns in row 1, B fills third column row 1, C and D fill next row -> Option AQuick Check:
Dense fills gaps left by spanning items [OK]
- Ignoring dense and placing items strictly in order
- Placing D before C incorrectly
- Assuming no gap filling happens
<div class="grid grid-flow-col gap-4"> <div>1</div> <div>2</div> <div>3</div> </div>
But items stack vertically in a single column instead of forming multiple columns. What is the likely mistake?
Solution
Step 1: Check grid container setup
Usinggrid-flow-colplaces items top to bottom in each column, then next column, but without defining the number of columns or rows, the grid defaults to one implicit column.Step 2: Identify missing grid size
Withoutgrid-cols-3or similar, the grid does not create multiple columns, so items stack vertically in one column.Final Answer:
Missing grid-cols or grid-rows to define grid size -> Option BQuick Check:
Define grid size to control flow layout [OK]
- Assuming grid-flow-col alone creates columns
- Blaming gap for layout issues
- Forgetting grid size classes
Solution
Step 1: Set default column flow for small screens
The classgrid-flow-colsets column flow by default (small screens).Step 2: Override with dense row flow on larger screens
The classsm:grid-flow-row-densechanges flow to dense row on screens medium and up.Step 3: Adjust columns for responsiveness
grid-cols-2for small screens andsm:grid-cols-4for larger screens set grid size accordingly.Final Answer:
grid grid-flow-col sm:grid-flow-row-dense grid-cols-2 sm:grid-cols-4 gap-3 -> Option DQuick Check:
Use responsive prefixes to switch flow and columns [OK]
- Mixing flow directions and breakpoints incorrectly
- Forgetting to set grid-cols for each breakpoint
- Using non-existent classes like grid-flow-col-dense
Ready to prove it?
Pass the test and this pattern moves unknown
