Find a course

Module 5: Responsive Design and PublishingLesson 11 of 1212 min

Responsive Design

Make pages adapt to phones, tablets and desktops with the viewport meta tag, fluid images, relative units and mobile-first media queries.

Step 1 of 15Learn

One page, every screen

More than half of all web traffic now comes from phones. A visitor might open your page on a 360 px-wide phone in the morning, a tablet at lunch and a 2,560 px monitor at work. Building a separate site for each is impractical. Responsive design means one page that adapts its layout to the space available.

Responsive design rests on three ideas, first described together by designer Ethan Marcotte in 2010:

  1. Fluid layouts: sizes in relative units like %, fr and rem instead of fixed pixels, so layouts stretch and shrink.
  2. Flexible images: images that scale down to fit their container.
  3. Media queries: CSS that applies only at certain screen sizes, to rearrange the layout where needed.

Before any of that works, phones need one line in the head:

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

Without it, mobile browsers assume your page was designed for a desktop, render it at roughly 980 px wide and shrink it down, leaving tiny text that users must pinch to zoom. With it, the browser uses the device's real width, and your responsive CSS takes effect. Forgetting this tag is the number-one reason a "responsive" page looks wrong on phones.

Step 2 of 15Practice · True or false

Without the viewport meta tag, many mobile browsers render a page as if it were about 980 px wide and then shrink it to fit the screen.

Step 3 of 15Diagram

Typical viewport widths

Your layout has to look right at every width along this line, not only at the markers.

Typical viewport widthsScale: Small phone, Large phone, Tablet, portrait, Common laptop, Full HD monitor.PhonesTabletsLaptops and desktops5007501,0001,2501,5001,7502,000360 pxSmall phone430 pxLarge phone768 pxTablet, portrait1,366 pxCommon laptop1,920 pxFull HD monitor
Common CSS widths; actual device sizes vary widely.
Small phone

360 px

Large phone

430 px

Tablet, portrait

768 px

Common laptop

1,366 px

Full HD monitor

1,920 px

Step 4 of 15Learn

Flexible images and fluid sizing

An image that's 1,200 px wide will overflow a 360 px phone screen and force horizontal scrolling, one of the most annoying things a mobile page can do. One rule fixes it for the whole site:

img, video {
  max-width: 100%;
  height: auto;
}

max-width: 100% lets an image display at its natural size when there's room, but never wider than its container. height: auto keeps its proportions as it shrinks.

The same thinking applies everywhere. Instead of width: 960px for your main content, use:

.container {
  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
  padding: 0 1rem;
}

This container fills small screens edge to edge (with 1 rem of breathing room), grows with the screen, and stops at 1,100 px on large monitors, centered by the auto margins. It's one of the most reused snippets in web design.

Modern CSS adds more fluid tools. The clamp() function sets a value that scales with the viewport between limits: font-size: clamp(1.75rem, 4vw, 3rem); gives a heading that grows with the screen but never drops below 1.75rem or exceeds 3rem.

Step 5 of 15Practice · Work it out

A container has width: 100%; max-width: 1100px; padding: 0 1rem; with box-sizing: border-box and a 16 px root font size. On a 375 px-wide phone, how wide is the area available for content inside the padding, in pixels?

Step 6 of 15Explore

Fluid container: content width vs screen width

A container with width: 100%, a max-width and side padding (border-box). The horizontal axis is the viewport width in px.

05001,000 5001,0001,500
Content width on a 360 px phone
328 px
Content width on a 1,600 px screen
1,068 px

Step 7 of 15Learn

Media queries and mobile first

A media query applies CSS only when a condition is true:

@media (min-width: 768px) {
  .cards {
    grid-template-columns: repeat(3, 1fr);
  }
}

The rules inside apply only when the viewport is at least 768 px wide.

There are two strategies. Desktop first writes the wide layout as the default and uses max-width queries to undo it for smaller screens. Mobile first writes the simple, single-column phone layout as the default and uses min-width queries to add complexity as space grows:

.cards { display: grid; gap: 1rem; }            /* phones: one column */
@media (min-width: 600px) {
  .cards { grid-template-columns: 1fr 1fr; }    /* tablets: two */
}
@media (min-width: 1000px) {
  .cards { grid-template-columns: repeat(3, 1fr); }  /* desktops: three */
}

Mobile first is the common recommendation. Phone layouts are usually simpler, so you write less override code. It forces you to prioritize the most important content. And small devices, often on slower connections, don't have to process styles meant for large screens and then undo them.

Choosing breakpoints: don't target specific devices, which change every year. Start with your phone layout, widen the browser window slowly, and add a breakpoint where your design starts to look stretched or awkward. Common breakpoints happen to land somewhere around 600, 768, 1024 and 1280 px, but your content should decide.

Step 8 of 15Note

Step 9 of 15Practice · Choose one

In a mobile-first stylesheet, which media query adds a two-column layout for screens 700 px and wider?

Step 10 of 15Practice · Spot the mistake

This mobile-first CSS should show one column on phones and three on wide screens. Tap the line that breaks the mobile-first approach.

Step 11 of 15Diagram

Mobile first: start small, add as screens grow

Mobile first: start small, add as screens growProcess diagram: Base styles: 1 column → @media (min-width: 600px) → @media (min-width: 1000px).Base styles: 1column@media (min-width:600px)@media (min-width:1000px)

Tap a shape with to see more.

Base styles: 1 column

No media query. Phones get the simplest, fastest CSS.

@media (min-width: 600px)

Switch to 2 columns once there is room.

@media (min-width: 1000px)

3 columns, wider margins, larger headings.

Step 12 of 15Note

Step 13 of 15Note

Step 14 of 15Practice · Put in order

Put these steps for making an existing fixed-width page responsive into a sensible order.

Drag the items, or use the arrow buttons, to put them in order.

  1. Write the single-column phone layout as the default
  2. Make images fluid with max-width: 100% and height: auto
  3. Add the viewport meta tag to the head
  4. Replace fixed widths with %, fr, rem and max-width
  5. Add min-width media queries where the layout starts to look stretched

Step 15 of 15Practice · Select all that apply

Which practices are part of good responsive design? Select all that apply.

Select all that apply.