Skip to content
FORGENO ADS GAMESUnlock all $8

CSS · Unit 3: Layout with flexbox and grid · Lesson 5 of 6

Responsive design with media queries

Start with phones: write your basic styles for small screens first, then add rules for wider screens.

Opens this lesson in Code Academy. Lessons open one after another, and each unit opens when you pass the checkpoint quiz before it (you can take a checkpoint early to skip ahead). If this lesson is not open yet, the button shows you its unit on your map.

The idea

Start with phones: write your basic styles for small screens first, then add rules for wider screens.

A media query wraps rules that only apply when a condition is true. @media (min-width: 600px) { ... } applies at 600px wide and up.

The viewport <meta> tag from the HTML lessons is what makes phones report their real width, so media queries work.

Worked example

CSS

.cols {
  display: grid;
  gap: 12px;
}

@media (min-width: 600px) {
  .cols {
    grid-template-columns: 1fr 1fr;
  }
}

The HTML it styles

<div class="cols">
  <div>Left</div>
  <div>Right</div>
</div>

On a narrow screen the two parts stack. At 600px and wider they sit side by side.

Key terms

Media query
@media (min-width: 600px) { ... } applies rules only when the screen is wide enough.

Practise it in Code Academy

It has 4 exercises (multiple choice, fill in the blank and write code from a prompt) and takes about 8 minutes. Every answer is checked as you go, and feedback names the exact mistake. Hints and the solution are there if you get stuck.

Every lesson is free: no ads, no lives and no paywall.