Flex direction control helps arrange items in a row or column inside a container. It makes layouts easy and flexible.
Flex direction control in Tailwind
flex-row flex-row-reverse flex-col flex-col-reverse
Use flex-row to arrange items horizontally from left to right.
Use flex-col to arrange items vertically from top to bottom.
<div class="flex flex-row"> <div>Item 1</div> <div>Item 2</div> <div>Item 3</div> </div>
<div class="flex flex-col"> <div>Item 1</div> <div>Item 2</div> <div>Item 3</div> </div>
<div class="flex flex-row-reverse"> <div>Item 1</div> <div>Item 2</div> <div>Item 3</div> </div>
<div class="flex flex-col-reverse"> <div>Item 1</div> <div>Item 2</div> <div>Item 3</div> </div>
This example shows four sections demonstrating each flex direction control class. Each section has colored boxes labeled Item 1, Item 2, and Item 3 arranged according to the flex direction.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Flex Direction Control Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="p-6"> <section> <h2 class="text-lg font-semibold mb-2">Flex Row (default left to right)</h2> <div class="flex flex-row gap-4 bg-gray-100 p-4 rounded"> <div class="bg-blue-500 text-white p-2 rounded">Item 1</div> <div class="bg-blue-500 text-white p-2 rounded">Item 2</div> <div class="bg-blue-500 text-white p-2 rounded">Item 3</div> </div> </section> <section class="mt-6"> <h2 class="text-lg font-semibold mb-2">Flex Column (top to bottom)</h2> <div class="flex flex-col gap-4 bg-gray-100 p-4 rounded w-32"> <div class="bg-green-500 text-white p-2 rounded">Item 1</div> <div class="bg-green-500 text-white p-2 rounded">Item 2</div> <div class="bg-green-500 text-white p-2 rounded">Item 3</div> </div> </section> <section class="mt-6"> <h2 class="text-lg font-semibold mb-2">Flex Row Reverse (right to left)</h2> <div class="flex flex-row-reverse gap-4 bg-gray-100 p-4 rounded"> <div class="bg-red-500 text-white p-2 rounded">Item 1</div> <div class="bg-red-500 text-white p-2 rounded">Item 2</div> <div class="bg-red-500 text-white p-2 rounded">Item 3</div> </div> </section> <section class="mt-6"> <h2 class="text-lg font-semibold mb-2">Flex Column Reverse (bottom to top)</h2> <div class="flex flex-col-reverse gap-4 bg-gray-100 p-4 rounded w-32"> <div class="bg-purple-500 text-white p-2 rounded">Item 1</div> <div class="bg-purple-500 text-white p-2 rounded">Item 2</div> <div class="bg-purple-500 text-white p-2 rounded">Item 3</div> </div> </section> </body> </html>
Use gap-* classes to add space between flex items.
Flex direction works only if the container has flex class.
You can combine flex direction with responsive prefixes like md:flex-col to change layout on medium screens.
flex-row arranges items horizontally left to right.
flex-col stacks items vertically top to bottom.
Adding -reverse reverses the order of items.
Practice
Solution
Step 1: Understand flex direction basics
flex-rowarranges items horizontally from left to right.Step 2: Compare with other classes
flex-colstacks vertically, and-reversevariants reverse order.Final Answer:
flex-row -> Option CQuick Check:
Horizontal left to right = flex-row [OK]
- Confusing flex-row with flex-col
- Thinking reverse means horizontal direction
- Mixing up flex-col and flex-col-reverse
Solution
Step 1: Identify vertical stacking classes
flex-colstacks items vertically top to bottom.Step 2: Apply reverse to vertical stacking
flex-col-reversereverses vertical order, stacking bottom to top.Final Answer:
flex-col-reverse -> Option AQuick Check:
Reverse vertical stack = flex-col-reverse [OK]
- Choosing flex-row-reverse for vertical reverse
- Selecting flex-col instead of reversed version
- Mixing horizontal and vertical classes
flex-row-reverse on a flex container with items A, B, C?Solution
Step 1: Understand flex-row-reverse direction
flex-row-reversearranges items horizontally but reverses order.Step 2: Apply to items A, B, C
Original order left to right is A B C; reversed is C B A left to right.Final Answer:
C B A (left to right) -> Option DQuick Check:
flex-row-reverse reverses horizontal order [OK]
- Assuming reverse changes vertical stacking
- Confusing left-to-right with top-to-bottom
- Ignoring the reverse effect on order
<div class="flex flex-col-reverse"> <div>Item 1</div> <div>Item 2</div> <div>Item 3</div> </div>
What will be the visual order of items?
Solution
Step 1: Analyze flex-col-reverse effect
flex-col-reversestacks items vertically but reverses order from bottom to top.Step 2: Determine visual order of items
Original order top to bottom is Item 1, 2, 3; reversed is Item 3, 2, 1 top to bottom.Final Answer:
Item 3, Item 2, Item 1 (top to bottom) -> Option AQuick Check:
flex-col-reverse reverses vertical order [OK]
- Thinking reverse affects horizontal order
- Assuming order stays original
- Confusing flex-col with flex-row
Solution
Step 1: Understand vertical stacking requirement
Stacking vertically means usingflex-color its reverse.Step 2: Reverse order to show last item first
To show last item at top, useflex-col-reversewhich reverses vertical order.Final Answer:
flex-col-reverse -> Option BQuick Check:
Vertical stack reversed = flex-col-reverse [OK]
- Choosing flex-row or flex-row-reverse for vertical stack
- Using flex-col without reverse
- Confusing horizontal and vertical directions
Ready to prove it?
Pass the test and this pattern moves unknown
