Flex direction tells the browser how to arrange items inside a container. It helps decide if items go side by side or stacked up.
Flex direction in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
CSS
flex-direction: row | row-reverse | column | column-reverse;row arranges items left to right (default).
column stacks items top to bottom.
Examples
CSS
flex-direction: row;CSS
flex-direction: column;CSS
flex-direction: row-reverse;CSS
flex-direction: column-reverse;Sample Program
This example shows a container with three items stacked vertically because flex-direction: column; is used. The container has a border and spacing between items for clarity.
CSS
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Flex Direction Example</title> <style> .container { display: flex; flex-direction: column; gap: 1rem; border: 2px solid #333; padding: 1rem; width: 200px; } .item { background-color: #4a90e2; color: white; padding: 0.5rem; text-align: center; border-radius: 0.25rem; font-family: Arial, sans-serif; } </style> </head> <body> <section class="container" aria-label="Flex direction container"> <div class="item">Item 1</div> <div class="item">Item 2</div> <div class="item">Item 3</div> </section> </body> </html>
Important Notes
Remember to set display: flex; on the container before using flex-direction.
Using flex-direction changes the main axis, which affects how other flex properties behave.
For accessibility, use semantic HTML and ARIA labels to describe the container's role.
Summary
flex-direction controls how flex items are laid out: in a row or column, normal or reversed.
It works only when display: flex; is set on the container.
Changing direction helps create flexible and responsive layouts easily.
Practice
1. What does the CSS property
flex-direction control in a flex container?easy
Solution
Step 1: Understand the role of flex-direction
Theflex-directionproperty sets the main axis direction for flex items inside a flex container.Step 2: Identify what flex-direction affects
It controls whether items are laid out in a row (horizontally) or column (vertically), and if the order is normal or reversed.Final Answer:
The direction in which flex items are laid out (row or column) -> Option AQuick Check:
flex-direction controls layout direction [OK]
Hint: Remember: flex-direction sets row or column layout [OK]
Common Mistakes:
- Confusing flex-direction with color or size properties
- Thinking flex-direction changes container size
- Assuming flex-direction affects font styles
2. Which of the following is the correct syntax to set flex items in a column direction?
easy
Solution
Step 1: Check the container display property
Flexbox requiresdisplay: flex;on the container to work.Step 2: Verify the flex-direction syntax
The correct property isflex-directionwith values likeroworcolumn. The valuecolumnis valid.Final Answer:
display: flex; flex-direction: column; -> Option DQuick Check:
Use display flex + flex-direction column [OK]
Hint: Always use display:flex before flex-direction [OK]
Common Mistakes:
- Using invalid values like 'row-column'
- Forgetting display:flex on container
- Using 'direction' instead of 'flex-direction'
3. Given this CSS:
div.container {
display: flex;
flex-direction: row-reverse;
}
What will be the order of flex items inside div.container?medium
Solution
Step 1: Identify flex-direction value
The valuerow-reversemeans items are placed in a row but in reverse order, starting from the right.Step 2: Understand layout direction
Flex items will appear horizontally but reversed, so the last item appears first on the right side.Final Answer:
Items will be laid out from right to left in reverse order -> Option CQuick Check:
row-reverse means horizontal reversed layout [OK]
Hint: row-reverse flips horizontal order right to left [OK]
Common Mistakes:
- Confusing row-reverse with column direction
- Assuming normal left-to-right order
- Thinking items stack vertically
4. This CSS code does not change the layout direction as expected. What is the error?
.box {
flex-direction: column;
}medium
Solution
Step 1: Check if flex container is set
The propertyflex-directiononly works if the container hasdisplay: flex;.Step 2: Identify missing display property
The code does not includedisplay: flex;, so the layout direction won't change.Final Answer:
Missing display: flex; on the container -> Option AQuick Check:
flex-direction needs display:flex [OK]
Hint: Always set display:flex before flex-direction [OK]
Common Mistakes:
- Forgetting to set display:flex
- Using wrong property names
- Thinking flex-direction works on inline elements
5. You want a flex container to display items vertically but with the last item shown at the top. Which CSS flex-direction value should you use?
hard
Solution
Step 1: Understand vertical layout
To display items vertically, usecolumnorcolumn-reverse.Step 2: Reverse order with last item on top
Thecolumn-reversevalue stacks items vertically but reverses their order, so the last item appears at the top.Final Answer:
column-reverse -> Option BQuick Check:
column-reverse stacks vertically reversed [OK]
Hint: Use column-reverse for vertical reversed order [OK]
Common Mistakes:
- Choosing row-reverse which is horizontal
- Using column without reverse order
- Confusing row and column directions
