Responsive Image Gallery
What you'll build
An HTML page with a gallery of 6 placeholder images laid out with CSS Grid. The grid shows 1 column on mobile, 2 on tablet, and 3 on desktop using media queries.
Goals
- Use CSS Grid to create a responsive column layout
- Apply media queries for different screen sizes
- Use gap property for consistent spacing between images
- Make images responsive with object-fit and width: 100%
Prerequisites
- HTML structure and image tags
- Basic CSS including selectors and properties
- Familiarity with CSS Grid (grid-template-columns)
Steps
- 1
Set up the HTML structure
Create gallery.html with a heading and a div.gallery containing 6 img elements pointing to placeholder images.
Code<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Image Gallery</title> <link rel="stylesheet" href="gallery.css"> </head> <body> <h1>My Gallery</h1> <div class="gallery"> <img src="https://placehold.co/300x200" alt="Gallery image 1"> <img src="https://placehold.co/300x200" alt="Gallery image 2"> <img src="https://placehold.co/300x200" alt="Gallery image 3"> <img src="https://placehold.co/300x200" alt="Gallery image 4"> <img src="https://placehold.co/300x200" alt="Gallery image 5"> <img src="https://placehold.co/300x200" alt="Gallery image 6"> </div> </body> </html>Expected result
A page with a heading and 6 images stacked vertically with default styling.
- 2
Create the base grid layout (mobile-first)
Create gallery.css. Start with 1 column for mobile. Set display: grid, grid-template-columns: 1fr, and a gap of 1rem.
Code.gallery { display: grid; grid-template-columns: 1fr; gap: 1rem; padding: 1rem; } .gallery img { width: 100%; border-radius: 0.5rem; }Expected result
Images display in a single column with consistent spacing and rounded corners.
- 3
Add tablet breakpoint (2 columns)
Add a media query for min-width: 640px to show 2 columns on tablet screens.
Code@media (min-width: 640px) { .gallery { grid-template-columns: repeat(2, 1fr); } }Expected result
On screens 640px or wider, images arrange into 2 columns.
- 4
Add desktop breakpoint (3 columns)
Add another media query for min-width: 1024px to show 3 columns on desktop.
Code@media (min-width: 1024px) { .gallery { grid-template-columns: repeat(3, 1fr); } }Expected result
On desktop screens, images arrange into 3 equal columns.
Hint
Resize the browser window to see the layout change at each breakpoint.
- 5
Add responsive styles
Use media queries to adjust the layout for mobile: switch multi-column layouts to single column on small screens, reduce font sizes, and adjust spacing. Mobile-first means small-screen experience matters equally.
Code@media (max-width: 640px) { .container { flex-direction: column; padding: 1rem; } h1 { font-size: 1.5rem; } .card { width: 100%; margin-bottom: 1rem; } }Expected result
Below 640px viewport, the layout becomes single-column with smaller font and spacing; content does not overflow.
Try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Gallery</title>
<link rel="stylesheet" href="gallery.css">
</head>
<body>
<h1>My Gallery</h1>
<div class="gallery">
<!-- TODO: add 6 images -->
</div>
</body>
</html><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Gallery</title>
<link rel="stylesheet" href="gallery.css">
</head>
<body>
<h1>My Gallery</h1>
<div class="gallery">
<img src="https://placehold.co/300x200" alt="Gallery image 1">
<img src="https://placehold.co/300x200" alt="Gallery image 2">
<img src="https://placehold.co/300x200" alt="Gallery image 3">
<img src="https://placehold.co/300x200" alt="Gallery image 4">
<img src="https://placehold.co/300x200" alt="Gallery image 5">
<img src="https://placehold.co/300x200" alt="Gallery image 6">
</div>
</body>
</html>
/* gallery.css */
.gallery {
display: grid;
grid-template-columns: 1fr;
gap: 1rem;
padding: 1rem;
}
.gallery img {
width: 100%;
border-radius: 0.5rem;
}
@media (min-width: 640px) {
.gallery { grid-template-columns: repeat(2, 1fr); }
}
@media (min-width: 1024px) {
.gallery { grid-template-columns: repeat(3, 1fr); }
}What you learned
You built a responsive image gallery with CSS Grid and mobile-first media queries — the same pattern used by photo galleries, product grids, and card layouts across the web.
Related Resources
Related Lessons
- CSS Grid
Grid layout used for the gallery columns.
- CSS Responsive Design
Media queries for breakpoints.
Related Practice
- Grid Template Columns
Quiz on grid-template-columns.
- Media Query Quiz
Practice with media queries.
Related Cheatsheets
- CSS Cheatsheet
Grid and media query reference.
Related Errors
- Broken Image
Image not loading issues.
Related Glossary
- CSS
Cascading Style Sheets.
Related Glossary
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
- CSS
Cascading Style Sheets.
Related Errors
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
- Broken Image
Image not loading issues.