A flex container helps arrange items in a row or column easily. It makes layouts flexible and neat without complicated code.
Flex container in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
selector {
display: flex;
}Use display: flex; on a container to make it a flex container.
All direct children of this container become flex items.
nav element a flex container, so its child links line up in a row by default.nav {
display: flex;
}.container { display: flex; flex-direction: column; }
.box { display: flex; justify-content: center; align-items: center; }
This example creates a flex container with three items. They appear side by side with space between them. On small screens, they stack vertically. The container and items have colors and padding for clarity.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Flex Container Example</title> <style> .flex-container { display: flex; gap: 1rem; background-color: #f0f0f0; padding: 1rem; border: 2px solid #ccc; } .flex-item { background-color: #4caf50; color: white; padding: 1rem 2rem; border-radius: 0.5rem; font-weight: bold; flex: 1; text-align: center; } @media (max-width: 600px) { .flex-container { flex-direction: column; } } </style> </head> <body> <section class="flex-container" aria-label="Example flex container"> <div class="flex-item">Item 1</div> <div class="flex-item">Item 2</div> <div class="flex-item">Item 3</div> </section> </body> </html>
Flex containers only affect their direct children, not nested elements inside those children.
Use browser DevTools to see the flex container and items highlighted and experiment with properties live.
Flexbox works well for one-dimensional layouts (row or column), not complex grids.
Flex container arranges child items in a row or column easily.
Set display: flex; on a container to start using flexbox.
Flexbox helps create responsive and neat layouts with little code.
Practice
display: flex; on a container do?Solution
Step 1: Understand the role of
Settingdisplay: flex;display: flex;on a container activates flexbox layout for its children.Step 2: Effect on child elements
Children inside a flex container are arranged in a row by default or column if specified.Final Answer:
It makes the container a flex container, arranging children in a row or column. -> Option DQuick Check:
Flex container = display: flex [OK]
- Confusing flex container with hiding elements
- Thinking it changes colors or text styles
- Assuming it only affects text formatting
Solution
Step 1: Identify correct CSS property and value
The correct property to enable flexbox isdisplaywith the valueflex.Step 2: Check syntax correctness
container { display: flex; } uses correct CSS syntax:display: flex;. Others are invalid CSS.Final Answer:
container { display: flex; } -> Option AQuick Check:
Correct syntax = display: flex [OK]
- Swapping property and value order
- Using non-existent properties like flex-display
- Adding extra words like 'true' or 'block-flex'
.container { display: flex; }
.box { width: 50px; height: 50px; background: red; margin: 5px; } <div class='container'>
<div class='box'></div>
<div class='box'></div>
<div class='box'></div>
</div>Solution
Step 1: Analyze the container's display property
The container hasdisplay: flex;, which arranges children in a row by default.Step 2: Understand the boxes' layout
Each box has fixed size and margin, so they appear side by side with space around them.Final Answer:
Boxes arranged horizontally in a row with space between them. -> Option AQuick Check:
Flex default direction = row [OK]
- Assuming flex defaults to column
- Thinking boxes overlap without positioning
- Ignoring margin spacing between boxes
.container {
display: flex;
flex-direction: column;
}Solution
Step 1: Check the flex-direction property value
The value 'column' arranges children vertically instead of horizontally.Step 2: Correct the value to 'row'
Changing 'column' to 'row' fixes the layout to arrange items horizontally.Final Answer:
The value 'column' is wrong; it should be 'row'. -> Option CQuick Check:
flex-direction: row for horizontal [OK]
- Using 'column' instead of 'row' for horizontal layout
- Confusing flex and flexbox in display
- Assuming flex-wrap controls direction
Solution
Step 1: Set flex-direction to column for vertical stacking
Usingflex-direction: column;stacks children vertically.Step 2: Use align-items: center to center horizontally
align-items: center;centers items along the cross axis (horizontal in column direction).Final Answer:
.container { display: flex; flex-direction: column; align-items: center; } -> Option BQuick Check:
Column + align-items center = vertical stack + horizontal center [OK]
- Using row direction when vertical stack needed
- Confusing justify-content with align-items for cross axis
- Using display block instead of flex
