Skip to content
Codewilds

Lesson 25 of 29 · Grid & Responsive Design

Boss: Build a Responsive CSS Grid Gallery

Boss battlehard100 XP2 min read

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: grid with grid-template-columns: repeat(3, 1fr) creates three equal columns that share the width.
  • gap puts space between the rows and the columns.
  • grid-column: span 2 stretches an item across two columns.
  • max-width plus margin: 0 auto gives 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:

  1. Add the viewport meta tag with content="width=device-width, initial-scale=1".
  2. Centre .page with max-width: 960px and margin: 0 auto.
  3. Make .gallery a grid with 3 equal columns (repeat(3, 1fr)) and a 24px gap.
  4. Make the .featured photo span 2 columns. On desktop, the 8 photos then fill exactly 3 rows.
  5. 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
Play the 7 free lessons

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 .

Write code ↓
HTML & CSS+100 XP

Lesson 25 is part of the full course. The first 7 lessons are free.

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
Play the 7 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.