Lesson 25 of 29 · Grid & Responsive Design
Boss: Build a Responsive CSS Grid Gallery
The Shapeshifter blocks the way. Every test you pass deals damage — pass them all to defeat it and claim a loot chest.
The Hall of Portraits at the top of the cliffs is haunted by the Shapeshifter. Whenever a visitor's screen changes size, it twists the gallery out of shape: on laptops the portraits pile up in one endless column, and on phones they spill over the edge. Build a gallery that keeps its form on every screen, and the Shapeshifter has nowhere left to hide.
Battle plan
This fight combines the whole chapter:
display: gridwithgrid-template-columns: repeat(3, 1fr)creates three equal columns that share the width.gapputs space between the rows and the columns.grid-column: span 2stretches an item across two columns.max-widthplusmargin: 0 autogives a centred container that can still shrink.- The viewport meta tag and a media query make the layout change on small screens.
A media query can change any property, not just the number of columns. Whatever you set for desktop, ask yourself whether it still makes sense on a phone:
@media (max-width: 600px) {
.gallery {
grid-template-columns: 1fr;
}
/* ...and anything else that only fits on wide screens */
}
Assignment
Build the gallery. Add the viewport tag where the comment in <head> says, and write your CSS below the comment in the <style> element:
- Add the viewport meta tag with
content="width=device-width, initial-scale=1". - Centre
.pagewithmax-width: 960pxandmargin: 0 auto. - Make
.gallerya grid with 3 equal columns (repeat(3, 1fr)) and a24pxgap. - Make the
.featuredphoto span 2 columns. On desktop, the 8 photos then fill exactly 3 rows. - On screens 600px wide or narrower (
@media (max-width: 600px)), turn the gallery into 1 column, where every photo, including the featured one, fills the full width.
The tests check the gallery at 1024px, 640px, 600px, 500px and 400px. Every passing test pins down one of the Shapeshifter's forms. Make all nine pass to win the battle.
Premium lesson
Unlock the full Learn HTML & CSS course
- All 29 lessons, bosses and quizzes — lifetime access
- One payment of $35, no subscription
- Finish within 30 days and claim $10.50 (30%) back
Want more than one course?
The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.
Keep learning
Need a quick reminder? The HTML & CSS cheat sheet has every topic of Learn HTML & CSS on one page.
Lesson last updated on .