Grid and Flexbox help arrange things on a webpage neatly. They make layouts easy and flexible without messy code.
Grid vs flexbox in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
/* Flexbox container */ display: flex; /* Grid container */ display: grid;
Flexbox works mainly in one direction: row or column.
Grid works in two directions: rows and columns together.
/* Flexbox example */ .container { display: flex; flex-direction: row; justify-content: center; align-items: center; }
/* Grid example */ .container { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
This page shows two sections: one uses Flexbox to arrange three boxes in a row with space around them. The other uses Grid to create three equal columns with boxes inside. Both have borders and spacing so you can see the difference clearly.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Grid vs Flexbox Example</title> <style> body { font-family: Arial, sans-serif; margin: 2rem; } h2 { margin-bottom: 0.5rem; } .flex-container { display: flex; flex-direction: row; justify-content: space-around; align-items: center; border: 2px solid #4a90e2; padding: 1rem; margin-bottom: 2rem; gap: 1rem; } .grid-container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; border: 2px solid #e24a4a; padding: 1rem; } .box { background-color: #f0f0f0; border: 1px solid #ccc; padding: 1rem; text-align: center; font-weight: bold; user-select: none; } </style> </head> <body> <section> <h2>Flexbox Layout</h2> <div class="flex-container" role="list"> <div class="box" role="listitem">Item 1</div> <div class="box" role="listitem">Item 2</div> <div class="box" role="listitem">Item 3</div> </div> </section> <section> <h2>Grid Layout</h2> <div class="grid-container" role="list"> <div class="box" role="listitem">Item A</div> <div class="box" role="listitem">Item B</div> <div class="box" role="listitem">Item C</div> </div> </section> </body> </html>
Flexbox is great for simple layouts in one direction, like menus or toolbars.
Grid is better for complex layouts with rows and columns, like entire pages or galleries.
Both work well on small screens and can be combined for powerful designs.
Flexbox arranges items in a row or column, good for simple linear layouts.
Grid arranges items in rows and columns, good for complex layouts.
Use the right tool depending on how you want your content arranged.
Practice
Solution
Step 1: Understand layout capabilities
Flexbox arranges items in one direction: row or column, but not both at once.Step 2: Identify the method for two-dimensional layout
Grid allows arranging items in rows and columns simultaneously, making it suitable for complex layouts.Final Answer:
Grid -> Option BQuick Check:
Two-dimensional layout = Grid [OK]
- Thinking Flexbox can handle rows and columns at the same time
- Confusing float with layout methods
- Assuming position absolute arranges items in grid
Solution
Step 1: Identify the property to create flex container
The CSS property to create a flex container isdisplay: flex;.Step 2: Check other options for correctness
display: grid;creates a grid container, not flex.flex-direction: grid;is invalid.flex-container: true;is not a valid CSS property.Final Answer:
display: flex; -> Option DQuick Check:
Flex container syntax = display: flex [OK]
- Using display: grid instead of flex
- Trying to set flex-direction to grid
- Using non-existent CSS properties
.container?
.container {
display: flex;
flex-direction: column;
gap: 1rem;
}
.item {
width: 100px;
height: 50px;
background-color: lightblue;
}Solution
Step 1: Analyze flex container properties
The container usesdisplay: flex;andflex-direction: column;, so items stack vertically.Step 2: Understand gap effect
Thegap: 1rem;adds space between items vertically in column direction.Final Answer:
Items arranged vertically with 1rem gap -> Option CQuick Check:
flex-direction: column + gap = vertical spacing [OK]
- Assuming flex defaults to row direction
- Ignoring the gap property
- Thinking items form a grid with flex
.grid-container {
display: flex;
grid-template-columns: repeat(3, 1fr);
}Solution
Step 1: Check display property
The container usesdisplay: flex;, which does not supportgrid-template-columns.Step 2: Understand property compatibility
grid-template-columnsworks only withdisplay: grid;. Mixing flex display with grid properties causes no effect or errors.Final Answer:
Using flex display with grid-template-columns property -> Option AQuick Check:
grid-template-columns requires display: grid [OK]
- Mixing flex display with grid properties
- Assuming repeat syntax is wrong
- Forgetting to set display property
.gallery {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.photo {
flex: 1 1 200px;
height: 150px;
}
Why might CSS Grid be a better choice here?Solution
Step 1: Analyze flexbox gallery approach
Flexbox with wrapping creates rows but columns depend on item size and wrapping, which can be uneven and unpredictable.Step 2: Compare with grid advantages
Grid lets you define explicit rows and columns, so the gallery layout is consistent and easier to control across screen sizes.Final Answer:
Grid allows explicit control of rows and columns, making layout predictable and consistent. -> Option AQuick Check:
Responsive rows + columns = Grid best choice [OK]
- Assuming flex-wrap creates perfect grid layouts
- Thinking Grid always uses less CSS
- Believing flexbox cannot have gaps
