A grid container helps you arrange items neatly in rows and columns, like a table but more flexible.
Grid container in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
selector {
display: grid;
grid-template-columns: value;
grid-template-rows: value;
gap: value;
}display: grid; turns an element into a grid container.
You define columns and rows with grid-template-columns and grid-template-rows.
.container { display: grid; grid-template-columns: 100px 100px 100px; }
.container { display: grid; grid-template-columns: 1fr 2fr 1fr; }
.container { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
This example creates a grid container with three columns. The middle column is twice as wide as the others. Each item is styled with a blue background and white text. The grid has gaps between items and padding around the container.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Grid Container Example</title> <style> .grid-container { display: grid; grid-template-columns: 1fr 2fr 1fr; gap: 1rem; padding: 1rem; background-color: #f0f0f0; } .grid-item { background-color: #4a90e2; color: white; padding: 1rem; text-align: center; border-radius: 0.5rem; font-weight: bold; } </style> </head> <body> <main> <section class="grid-container" aria-label="Example grid container"> <div class="grid-item">Item 1</div> <div class="grid-item">Item 2</div> <div class="grid-item">Item 3</div> </section> </main> </body> </html>
Use gap to add space between grid items easily.
Grid containers automatically place items in rows and columns based on your settings.
Try resizing the browser to see how grid adapts if you use flexible units like fr.
A grid container uses display: grid; to arrange items in rows and columns.
You control the layout with grid-template-columns and grid-template-rows.
Grid makes it easy to build neat, flexible, and responsive layouts.
Practice
Solution
Step 1: Understand grid container basics
To create a grid layout, the container must havedisplay: grid;set.Step 2: Check other options
display: flex;creates a flexbox, not grid.position: grid;is invalid.grid-template-columnsdefines columns but does not make container a grid.Final Answer:
display: grid; -> Option DQuick Check:
Grid container = display: grid; [OK]
- Using display: flex; instead of grid
- Trying to use position: grid;
- Setting grid-template-columns without display: grid;
Solution
Step 1: Understand grid-template-columns syntax
The propertygrid-template-columnsdefines columns. Using1fr 1fr 1frcreates 3 equal columns.Step 2: Check other options
3pxis a fixed small width, not equal columns.grid-columnsis invalid property.grid-template-rowssets rows, not columns.Final Answer:
grid-template-columns: 1fr 1fr 1fr; -> Option AQuick Check:
Equal columns = grid-template-columns: 1fr 1fr 1fr; [OK]
- Using grid-columns instead of grid-template-columns
- Confusing rows and columns properties
- Using fixed px values for equal flexible columns
.container {
display: grid;
grid-template-columns: 100px 200px;
grid-template-rows: 50px 50px;
}
How many grid cells are created inside .container?Solution
Step 1: Calculate columns and rows
There are 2 columns (100px and 200px) and 2 rows (50px and 50px).Step 2: Multiply columns by rows
Total grid cells = 2 columns x 2 rows = 4 cells.Final Answer:
4 -> Option AQuick Check:
2 columns x 2 rows = 4 cells [OK]
- Adding columns and rows instead of multiplying
- Counting only columns or only rows
- Confusing grid cells with grid lines
.grid {
display: grid;
grid-template-columns: repeat(3);
}Solution
Step 1: Check repeat() syntax
Therepeat()function needs two arguments: number of times and size, e.g.repeat(3, 1fr).Step 2: Identify the error
Here, onlyrepeat(3)is given, missing the size argument, so it's invalid.Final Answer:
repeat() requires two arguments: count and size -> Option CQuick Check:
repeat() needs count and size [OK]
- Using repeat() with only one argument
- Forgetting to specify display: grid;
- Thinking repeat() is invalid in grid-template-columns
Solution
Step 1: Understand fixed and flexible columns
150px is fixed width. To fill remaining space, use1frwhich means flexible fraction.Step 2: Check options
display: grid; grid-template-columns: 150px auto; usesautowhich sizes based on content, not remaining space. Options B and D put 150px in wrong column order.Final Answer:
display: grid; grid-template-columns: 150px 1fr; -> Option BQuick Check:
Fixed + flexible = 150px 1fr [OK]
- Using auto instead of 1fr for flexible space
- Swapping column order
- Not setting display: grid;
