Skip to content
Tutorials Center
My learning

Topics

Lesson 49 of 122 ยท Page Structure and Layout

Responsive Web Design

Make pages that work on every screen. Learn the viewport meta tag, fluid widths, flexible images, media queries, mobile-first CSS and fluid type with clamp().

People will open your page on a phone held in one hand, a tablet on the sofa, a laptop and a huge office monitor. Responsive web design means building one page that adapts to all of them, instead of separate "mobile" and "desktop" sites.

The good news: HTML is responsive by default. Plain text wraps to fit any window. Most responsive problems come from things we add โ€” fixed widths, big images, multi-column layouts. This lesson covers the handful of techniques that fix them: the viewport meta tag, fluid widths, flexible images, media queries, a mobile-first mindset and fluid sizing with clamp().

Step 1: the viewport meta tag

Early smartphones had to show websites that were designed for desktop monitors. So mobile browsers pretend the screen is about 980 pixels wide, render the page at that width and then shrink it down. Without instructions, your carefully responsive page gets that treatment too, and everything looks tiny.

This one line in the <head> turns the behavior off:

HTML
<meta name="viewport" content="width=device-width, initial-scale=1">
  • width=device-width โ€” make the layout width equal to the device's real width in CSS pixels.
  • initial-scale=1 โ€” start at 100% zoom.

Put it in every page you build. You will meet it again in Meta Tags.

Never block zooming. You may see maximum-scale=1 or user-scalable=no in old code. They stop people with low vision from zooming in, and many browsers now ignore them anyway. Leave them out.

Step 2: fluid widths

A box with width: 800px overflows any screen narrower than 800 pixels, and the visitor has to scroll sideways. Use relative units and limits instead:

Instead ofโ€ฆ Useโ€ฆ Why
width: 800px max-width: 800px Can shrink, never grows too wide.
width: 300px for a column width: 30% or 1fr in Grid Grows and shrinks with its container.
Fixed heights Let content decide (auto) Text wraps onto more lines on small screens and needs room.
Fixed vs fluid

If the preview is narrower than 700 pixels, the pink box sticks out to the right while the green box fits neatly.

Step 3: flexible images

Images have a natural pixel size, and a 1,600-pixel-wide photo will burst out of a phone screen. One rule fixes that for the whole site:

HTML
<style>
  img, video {
    max-width: 100%;
    height: auto;
  }
</style>

max-width: 100% stops an image from ever being wider than its container. height: auto keeps the proportions correct while it shrinks. You should still set the width and height attributes on each <img> โ€” the browser uses them to reserve the right amount of space while the image loads.

Flexible image

Delete the max-width: 100%; line and watch the image overflow the card. To send smaller files to smaller screens (and save bandwidth), see Responsive Images.

Step 4: media queries

Fluid widths take you a long way, but sometimes the layout itself should change: one column on a phone, two or three on a wider screen. A media query applies CSS only when a condition is true:

CSS
@media (min-width: 600px) {
  /* these rules only apply when the viewport is at least 600px wide */
}

The point where the layout changes is called a breakpoint. Choose breakpoints where your content starts to look wrong, not to match particular phone models โ€” there are far too many devices to chase.

A layout that changes at a breakpoint

Inside the preview, media queries respond to the width of the preview pane, not your whole screen. Depending on your window size you will see one, two or three columns. Make the browser window narrower or wider and the cards rearrange. (In a real browser you can also open DevTools and use the device toolbar to test many widths at once โ€” see Viewing Source and Using DevTools.)

Other useful media features include orientation: landscape, prefers-color-scheme: dark (the user chose a dark theme) and prefers-reduced-motion: reduce (the user wants fewer animations). Modern browsers also accept the shorter range syntax @media (width >= 600px).

Mobile first

Notice the order in that example. The base CSS describes the phone layout, and each media query uses min-width to add columns as space grows. That approach is called mobile first, and it has real benefits:

  • The simplest layout is the default, so it works even on unusual or very small devices.
  • Each breakpoint only adds a few rules, instead of undoing desktop styles.
  • It forces you to decide what matters most when space is limited.

The opposite, "desktop first" with max-width queries, works too, but it usually leads to more CSS that overrides other CSS.

You may not need a media query at all. Grid can choose the column count by itself: grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)) fits as many columns of at least 180px as there is room for. Try it in the example above in place of the media queries.

Fluid sizing with clamp()

A heading that looks great on a laptop can take up half a phone screen. Instead of setting font sizes at each breakpoint, clamp() lets a value scale smoothly between a minimum and a maximum:

CSS
font-size: clamp(MINIMUM, PREFERRED, MAXIMUM);

The browser uses the preferred value, but never goes below the minimum or above the maximum. The preferred value usually includes a viewport unit โ€” vw is 1% of the viewport width โ€” so it grows with the screen.

Fluid type with clamp()

Mixing a rem value into the preferred size (1rem + 4vw) matters for accessibility: pure vw text would ignore the user's font-size settings and browser zoom would barely affect it.

A responsive checklist

  1. The viewport meta tag is in the <head>.
  2. No element has a fixed width larger than a phone screen; use max-width and relative units.
  3. Images and videos have max-width: 100%; height: auto.
  4. The base CSS is the small-screen layout; media queries add to it as space grows.
  5. Breakpoints are chosen by content, not by device names.
  6. Text sizes respect user settings (rem, clamp() with a rem part).
  7. You have tested by actually resizing the window, and on a real phone if you can.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Your page looks tiny and zoomed-out on a phone, even though the CSS uses fluid widths. What is the most likely cause?

    Without the viewport meta tag, mobile browsers lay the page out at roughly 980px wide and then scale it down.
  2. 2. Which rule lets an image shrink to fit a narrow container while keeping its proportions?

    max-width: 100% caps the width at the container's width, and height: auto keeps the aspect ratio.
  3. 3. In a mobile-first stylesheet, what do the media queries usually look like?

    Mobile first means the base CSS is the small-screen layout, and min-width queries build on it as space grows.
  4. 4. What does font-size: clamp(1rem, 2.5vw, 2rem) do on a very wide screen where 2.5vw equals 3rem?

    The preferred value is capped by the maximum, so the size stops growing at 2rem.

Summary

  • Responsive design is one page that adapts to every screen size.
  • Always include <meta name="viewport" content="width=device-width, initial-scale=1">, and never disable zooming.
  • Prefer max-width and relative units over fixed widths; give media max-width: 100%; height: auto.
  • Media queries change the layout at breakpoints chosen by your content.
  • Write CSS mobile first with min-width queries, and use clamp() for smoothly scaling sizes.

Next lesson: The head Element โ€” everything that goes into a page's <head>.