← Back to the Build Your Homepage series
🎨
EPISODE 07
viewport Β· media queries Β· mobile-first Β· clamp()

Responsive Design

Build one layout that works on phones, tablets, and desktops. Use a mobile-first approach, sensible breakpoints, and fluid units like clamp() for typography.

responsivemedia querymobile-firstclamp
Duration
⏱ About 2 hours
Level
πŸ“Š Beginner+
Prerequisite
🎯 css-05
OUTCOME
A layout that adapts gracefully from 320px to 1440px screens

What you'll learn

  • 1Include the viewport meta tag correctly
  • 2Write mobile-first media queries
  • 3Use rem/em/% and clamp() for fluid sizing
  • 4Test layouts in browser DevTools device mode

1. The Viewport Meta Tag

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

Without this meta tag, mobile browsers render at 980px and zoom out β€” media queries effectively do nothing.

2. Mobile-First Media Queries

css
/* Base styles target mobile first */
.container { padding: 16px; }
.grid      { display: grid; gap: 12px; grid-template-columns: 1fr; }

@media (min-width: 640px) {
  .grid { grid-template-columns: repeat(2, 1fr); }
}
@media (min-width: 1024px) {
  .container { max-width: 960px; margin: 0 auto; padding: 32px; }
  .grid      { grid-template-columns: repeat(3, 1fr); gap: 24px; }
}

3. Fluid Sizing with clamp()

css
h1 {
  /* min, preferred, max */
  font-size: clamp(1.75rem, 4vw, 3rem);
}
.container {
  width: min(100%, 1200px);
  padding-inline: clamp(16px, 4vw, 48px);
}

clamp() scales smoothly between viewports β€” fewer breakpoints needed.

4. Suggested Breakpoints

NameMin widthUse case
sm640pxLarge phone / small tablet
md768pxTablet portrait
lg1024pxTablet landscape / small laptop
xl1280pxDesktop
2xl1536pxLarge desktop
Example code / lecture materials

All lecture materials and example code are openly available on GitHub.

View on GitHub β†—