Skip to content
KALANBLON
Show contents

Chapter 5 of 7 · 20 min

Grid and responsive design: from mobile to big screen

By the end of this chapter, you'll be able to

  • Create a grid of columns with grid-template-columns and the fr unit
  • Place named areas with grid-template-areas
  • Adapt the layout with media queries, starting from mobile
  • Use relative units (rem, %, minmax) rather than fixed pixels

A two-dimensional grid

Flexbox aligns on one line. Grid organises in rows and columns at the same time:

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 16px;
}
  • fr (fraction) shares the available space: 1fr 2fr gives two columns, the second twice as wide;
  • minmax(160px, 1fr): a column of at least 160 pixels, which then grows to share the space;
  • repeat(auto-fill, …) creates as many columns as possible.

This single rule gives one or two columns on a phone, and more and more on a tablet then a big screen, without any media query: the browser does the maths based on the available space.

Named areas

For a page's structure, grid-template-areas draws the grid with names:

.page {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-areas:
    "header"
    "content"
    "aside"
    "footer";
  gap: 16px;
}
header { grid-area: header; }
main   { grid-area: content; }
aside  { grid-area: aside; }
footer { grid-area: footer; }

On mobile, everything stacks in a single column, in the logical reading order.

Media queries: starting from mobile

A media query applies rules only if a condition is true, for example "the screen is at least 48rem wide". You first write the CSS for the small screen, then add rules for bigger screens: that's the mobile first approach.

@media (min-width: 48rem) {
  .page {
    grid-template-columns: 2fr 1fr;
    grid-template-areas:
      "header  header"
      "content aside"
      "footer  footer";
  }
}

From 48rem (768 pixels with the default text size), the aside box moves to the right of the content. The HTML hasn't changed by a single line.

The media query's rules are added: two columns, the box moves to the right. The HTML is the same.

Step 4 of 4
Text description of the animation
  1. On a phone, only the base rules apply: one column, everything stacks in reading order.
  2. The media query's condition is false: its rules don't apply.
  3. On a big screen, the condition becomes true.
  4. The media query's rules are added: two columns, the box moves to the right. The HTML is the same.

One line is essential in the <head> of every page, otherwise the phone displays a miniature desktop version and ignores your media queries:

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

Units that adapt

  • rem: a multiple of the base text size. If the user enlarges the text in their settings, everything follows. Prefer it for font sizes and spacing.
  • % and fr: proportions of the available space.
  • max-width: 100% on images: they never overflow their container.
  • min(), max(), clamp(): font-size: clamp(1.5rem, 4vw, 2.5rem) makes a heading vary with the screen width, between two limits.

Your turn

Make this page responsiveHTML · CSS

Preview

The preview is isolated from the page and runs no scripts · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Worked exercise

Worked exercise: the course grid

A catalogue page displays course cards. Write the CSS for the .catalogue grid to get: one column on a small phone, then as many columns as possible with cards of at least 15rem, 1rem of space between them, and a total width limited to 70rem and centred. Use no media query.

Hint 1
repeat(auto-fill, minmax(15rem, 1fr)) creates the columns automatically.
Hint 2
On a screen narrower than 15rem, a 15rem column would overflow. minmax(min(15rem, 100%), 1fr) takes the smaller of the two.
Show the solution and common mistakes
.catalogue {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(15rem, 100%), 1fr));
  gap: 1rem;
  max-width: 70rem;
  margin-inline: auto;
  padding-inline: 1rem;
}

margin-inline: auto centres the grid horizontally. The -inline properties are logical properties: they follow the reading direction, and stay correct in languages written from right to left, such as Arabic.

Common mistakes:

  • Writing width: 70rem instead of max-width: the grid overflows on every screen smaller than 70rem.
  • Adding one media query per screen size (360, 414, 768…): the list is never complete, whereas auto-fill adapts to every width.

Summary

  1. 1Grid organises in rows and columns: fr shares the space, minmax sets limits.
  2. 2repeat(auto-fill, minmax(…)) creates a grid that adapts without a media query.
  3. 3grid-template-areas draws the page structure with names.
  4. 4Mobile first: small-screen CSS first, then @media (min-width: …) to grow.
  5. 5Without the meta viewport tag, a phone ignores your responsive layout.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.