Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Create a Simple Grid Container
📖 Scenario: You are building a photo gallery webpage. You want to arrange photos in a neat grid so they look organized and easy to browse.
🎯 Goal: Create a grid container that arranges child items in three equal columns with some space between them.
📋 What You'll Learn
Use CSS Grid to create the layout
Set the container to have exactly three columns of equal width
Add a gap of 1rem between grid items
Use semantic HTML with a <section> as the grid container
Ensure the grid container is responsive and uses modern CSS properties
💡 Why This Matters
🌍 Real World
Grid containers are used in web design to create clean, organized layouts for galleries, product listings, dashboards, and more.
💼 Career
Understanding CSS Grid is essential for front-end developers to build responsive and modern web layouts efficiently.
Progress0 / 4 steps
1
Create the HTML structure
Create a <section> element with the class gallery. Inside it, add exactly three <div> elements with the classes photo1, photo2, and photo3 respectively.
CSS
Hint
Use semantic <section> for the container and add three child <div> elements with the exact class names.
2
Set the container to use CSS Grid
In your CSS, select the .gallery class and set its display property to grid.
CSS
Hint
Use display: grid; to turn the container into a grid layout.
3
Define three equal columns and add gap
Add to the .gallery CSS rule: set grid-template-columns to create three equal columns using 1fr units, and set gap to 1rem.
CSS
Hint
Use grid-template-columns: 1fr 1fr 1fr; for three equal columns and gap: 1rem; for spacing.
4
Add basic styling to photos for visibility
Add CSS rules for .photo1, .photo2, and .photo3 to give each a background color and a fixed height of 8rem so they are visible on the page.
CSS
Hint
Give each photo a distinct background color and set height: 8rem; so they show as colored blocks.
Practice
(1/5)
1. What CSS property do you use to make an element a grid container?
easy
A. grid-template-columns: auto;
B. display: flex;
C. position: grid;
D. display: grid;
Solution
Step 1: Understand grid container basics
To create a grid layout, the container must have display: grid; set.
Step 2: Check other options
display: flex; creates a flexbox, not grid. position: grid; is invalid. grid-template-columns defines columns but does not make container a grid.
C. repeat() requires two arguments: count and size
D. grid-template-columns cannot use repeat()
Solution
Step 1: Check repeat() syntax
The repeat() function needs two arguments: number of times and size, e.g. repeat(3, 1fr).
Step 2: Identify the error
Here, only repeat(3) is given, missing the size argument, so it's invalid.
Final Answer:
repeat() requires two arguments: count and size -> Option C
Quick Check:
repeat() needs count and size [OK]
Hint: repeat() always needs count and size, like repeat(3, 1fr) [OK]
Common Mistakes:
Using repeat() with only one argument
Forgetting to specify display: grid;
Thinking repeat() is invalid in grid-template-columns
5. You want a grid container that has 2 columns: the first column is 150px wide, and the second column fills the remaining space. Which CSS is correct?
hard
A. display: grid; grid-template-columns: 150px auto;
B. display: grid; grid-template-columns: 150px 1fr;
C. display: grid; grid-template-columns: auto 150px;
D. display: grid; grid-template-columns: 1fr 150px;
Solution
Step 1: Understand fixed and flexible columns
150px is fixed width. To fill remaining space, use 1fr which means flexible fraction.
Step 2: Check options
display: grid; grid-template-columns: 150px auto; uses auto which 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 B
Quick Check:
Fixed + flexible = 150px 1fr [OK]
Hint: Use 1fr for flexible space after fixed width [OK]