Skip to content

CSS Responsive Design

Build responsive layouts with media queries, mobile-first, and fluid units.

What you'll learn

  • What responsive design is and why mobile-first is the right approach
  • How media queries adapt styles at different breakpoints
  • Fluid units: percentages, vw/vh, rem/em, and clamp()
  • Common breakpoint strategies and the role of container queries

Concept

CSS Responsive Design

Responsive design means your layout adapts to the screen size — from a 320px phone to a 2560px desktop monitor — without serving separate pages or requiring pinch-to-zoom. The goal is one codebase that looks good at every size. CSS provides the tools: media queries, fluid units, and flexible layout systems like Flexbox and Grid.

Mobile-First

The mobile-first approach means you write your base styles for the smallest screens, then add media queries to enhance the layout for larger screens. This is the opposite of building a desktop site and "shrinking it down." Mobile-first is better because mobile styles are simpler (usually stacked, single-column), and adding complexity for larger screens is easier than removing it. It also produces smaller CSS for mobile devices, which often have slower networks.

Media Queries

A media query applies styles only when a condition is met — usually the viewport width:

/* Base: mobile (single column) */
.card-grid { display: grid; grid-template-columns: 1fr; }

/* Tablet and up: two columns */
@media (min-width: 768px) {
  .card-grid { grid-template-columns: 1fr 1fr; }
}

/* Desktop and up: three columns */
@media (min-width: 1024px) {
  .card-grid { grid-template-columns: repeat(3, 1fr); }
}

Using min-width (not max-width) is the mobile-first pattern. Each breakpoint *adds* to the base, never overrides it with simpler styles.

Fluid Units

Hardcoded pixel sizes break responsiveness. Prefer fluid units:

  • % — relative to the parent.
  • vw / vh — relative to the viewport width/height. 100vw is the full width.
  • rem — relative to the root font size. Scales with user preferences.
  • em — relative to the element's own font size.
  • clamp(min, ideal, max) — a value that grows fluidly but never goes below min or above max. Perfect for font sizes: font-size: clamp(1rem, 2.5vw, 2rem).

Breakpoints

Common breakpoints align with device classes: 640px (large phone), 768px (tablet), 1024px (laptop), 1280px (desktop). But the best breakpoints are content-based — choose them where your layout breaks, not where devices happen to fall. Test by slowly resizing the browser and adding a breakpoint only when the design looks wrong.

Example

              /* Mobile-first: base styles for phones */
.container {
  width: 100%;
  padding: 0 16px;
}

.hero-title {
  font-size: clamp(1.75rem, 5vw, 3.5rem);
  line-height: 1.2;
}

.layout {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
}

/* Tablet: 768px and up */
@media (min-width: 768px) {
  .container { padding: 0 24px; }
  .layout {
    grid-template-columns: 240px 1fr;
    gap: 24px;
  }
}

/* Desktop: 1024px and up */
@media (min-width: 1024px) {
  .container { max-width: 1200px; margin: 0 auto; }
  .layout { gap: 32px; }
}
            

Base styles target mobile. clamp() makes the hero title scale fluidly between 1.75rem and 3.5rem. Each min-width media query adds enhancements for larger screens — wider padding, a sidebar column, and a max-width container on desktop.

Try it

  • Image Color Picker

    Pick responsive color values for your media query styles.

  • SVG Optimizer

    Optimize SVGs that scale responsively with vw units and viewBox.

Common mistakes

The mistake

Using max-width media queries (desktop-first) and overriding with simpler mobile styles.

The fix

Write mobile-first with min-width queries. Base styles target the smallest screen; each breakpoint adds complexity for larger screens. This produces smaller CSS for mobile and a cleaner cascade.

The mistake

Using fixed pixel widths that cause horizontal scrolling on small screens.

The fix

Use percentages, fr units, or max-width instead of fixed widths. For containers, use width: 100% with a max-width in px and margin: 0 auto to center on large screens without overflowing on small ones.

Related Snippets

Related Cheatsheets

  • CSS Cheatsheet

    Reference for media queries, fluid units, and responsive patterns.

Practice CSS Responsive Design

5 exercises

Practice CSS Responsive Design

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors