Lesson 13 of 14 · 10 min

Responsive design

Most people will visit your pages on a phone. Responsive design means one page that adapts to any screen.

1. The viewport tag

Without this line in <head>, phones pretend to be a wide desktop screen and shrink the whole page until it is unreadable:

<meta name="viewport" content="width=device-width, initial-scale=1">

Every page you make should have it.

2. Flexible sizes

Prefer sizes that adapt: percentages, fr in grids, max-width instead of a fixed width:

img {
  max-width: 100%;   /* never wider than its container */
  height: auto;
}

3. Media queries

A media query applies CSS only when a condition is true, usually about the screen width:

.columns {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

@media (max-width: 600px) {
  .columns {
    grid-template-columns: 1fr;   /* one column on small screens */
  }
}

Rules inside the @media block win when the condition matches, because they come later in the file.

Many developers write the phone layout first and use min-width media queries to add columns on bigger screens. Both approaches work; the important thing is to test at phone width (your browser's developer tools have a device toolbar for this).

Your task

  1. Add the viewport <meta> tag.
  2. Keep .cards at three columns, but add a media query so that on screens up to 600px wide the cards stack in one column.
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Courses</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <h1>Our courses</h1>
  <div class="cards">
    <div class="card">HTML and CSS</div>
    <div class="card">JavaScript</div>
    <div class="card">Python</div>
  </div>
</body>
</html>

Your page

Discussion

Stuck? Ask here. Be kind, and share hints rather than full answers.

Loading the discussion…