Lesson 8 / 1267%

Lesson 8

Responsive Design

28 min

Media queries and responsive layouts for mobile, tablet and desktop.

Definition

Responsive design is an approach to building websites whose layout, images and text automatically adapt to the size and capabilities of the screen displaying them, using a single HTML and CSS codebase for all devices. It rests on three pillars: a fluid grid, flexible images and media queries.

What is responsive design?

Responsive design means designing a page that adapts to every screen size: mobile, tablet, computer. It’s essential today — most of the world’s web traffic comes from mobile devices.

The three pillars of responsive design

  • A fluid grid — sizes expressed in percentages, fr or rem rather than fixed pixels, so the layout stretches and contracts.
  • Flexible images — media that never exceed their container and keep their proportions.
  • Media queries — conditional rules that rearrange the page when the screen reaches certain widths.

The prerequisite: the viewport tag

Without this tag in the <head>, mobile browsers display the page as if it were a shrunken desktop screen — responsive design will never work properly.

viewport.html
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
  • width=device-width — the page width follows the actual width of the device’s screen.
  • initial-scale=1.0 — the page is displayed without zoom on load.

Media queries

A media query applies CSS rules only if certain conditions are met — most often, the screen width.

media-query-base.css
.card {
  width: 100%;
}

@media (min-width: 768px) {
  .card {
    width: 50%; /* only applies from 768px wide and up */
  }
}

@media (min-width: 1024px) {
  .card {
    width: 33.33%; /* applies from 1024px wide and up */
  }
}

Combining and enriching conditions

A media query can combine several conditions with the and keyword, for example to target only tablets between two widths. It can also test characteristics other than width: the screen orientation, or the user’s preferences.

media-query-advanced.css
/* A range of widths: only between 640px and 1023px */
@media (min-width: 640px) and (max-width: 1023px) {
  .card { width: 50%; }
}

/* Depending on the device's orientation */
@media (orientation: landscape) {
  .banner { height: 120px; }
}

/* Depending on the theme the user chose in their system */
@media (prefers-color-scheme: dark) {
  body {
    background-color: #0f172a;
    color: #e2e8f0;
  }
}

/* Reduce animations for people sensitive to motion */
@media (prefers-reduced-motion: reduce) {
  * { animation: none; transition: none; }
}

Mobile-first vs desktop-first

Two approaches are possible. The mobile-first approach (recommended) means writing the base style for mobile, then adding rules for larger screens with min-width.

mobile-first.css
/* Base style: for mobile */
.grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
}

/* Then we add columns as the screen grows */
@media (min-width: 640px) {
  .grid {
    grid-template-columns: 1fr 1fr;
  }
}

@media (min-width: 1024px) {
  .grid {
    grid-template-columns: 1fr 1fr 1fr;
  }
}

The opposite approach, desktop-first, starts from the desktop style and usesmax-widthto progressively simplify as the screen shrinks:

desktop-first.css
.grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
}

@media (max-width: 1023px) {
  .grid {
    grid-template-columns: 1fr 1fr;
  }
}

@media (max-width: 639px) {
  .grid {
    grid-template-columns: 1fr;
  }
}

Best practice

The mobile-first approach is recommended: it forces you to prioritize essential content from the start, and the CSS is generally easier to maintain (you add rules as the screen grows, rather than undoing them as it shrinks).

Common breakpoints

  • 640px — small tablets / large phones
  • 768px — tablets
  • 1024px — small computer screens
  • 1280px — large screens

These aren’t strict rules: choose your breakpoints based on whereyour own design starts to break down, not from a fixed list.

Fluid sizes with clamp()

Media queries work in steps. For a continuous transition, theclamp(min, preferred, max) function picks a value that follows the screen width, while staying between two bounds. It’s ideal for headings, which stay readable from mobile to large screens without any media query.

clamp.css
h1 {
  /* never less than 1.75rem, never more than 3rem, and fluid in between */
  font-size: clamp(1.75rem, 4vw + 1rem, 3rem);
}

.container {
  width: min(100% - 2rem, 1100px); /* 100% minus some margin, capped at 1100px */
  margin-inline: auto;
}

Responsive images in CSS

responsive-image.css
img {
  max-width: 100%; /* never exceeds the width of its container */
  height: auto;    /* keeps its proportions */
}

Serving the right image to each screen

Shrinking an image with CSS doesn’t reduce its file size: a phone still downloads the huge file meant for desktop. The srcset attribute offers several versions and lets the browser choose the most suitable one, while <picture> even lets you swap the image entirely (a different crop on mobile, for example).

images-srcset.html
<!-- The browser picks the file based on the display width -->
<img
  src="photo-800.jpg"
  srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
  sizes="(min-width: 1024px) 33vw, 100vw"
  alt="Description of the photo"
/>

<!-- A different image depending on the screen width -->
<picture>
  <source media="(min-width: 768px)" srcset="banner-wide.jpg" />
  <img src="banner-mobile.jpg" alt="Site banner" />
</picture>

Showing/hiding depending on screen size

show-hide.css
.mobile-menu {
  display: block;
}

.desktop-menu {
  display: none;
}

@media (min-width: 768px) {
  .mobile-menu {
    display: none;
  }
  .desktop-menu {
    display: flex;
  }
}

The horizontal scrolling trap

If a horizontal scrollbar appears on mobile, an element is wider than the screen: an image without max-width: 100%, a table, or a fixed width in pixels. Spot it with the inspector, then replace the fixed width with a max-width.

Testing your work

Browser developer tools include a device mode: it simulates the width of a phone or tablet and lets you resize the window at will. Browse your site while dragging the width, and note the places where the layout breaks: those are your real breakpoints. Also test on a real phone as soon as possible, because some behaviors can’t be reproduced on a computer.

Full example: a page that adapts

Let’s bring together the concepts from this lesson: a mobile-first layout with a sidebar that stacks on mobile, then sits next to the content from 768px and up.

responsive-page.html
<div class="page">
  <main class="content">Main content</main>
  <aside class="sidebar">Sidebar</aside>
</div>
responsive-page.css
.page {
  display: grid;
  grid-template-columns: 1fr;   /* mobile: everything is stacked */
  gap: 24px;
  padding: 16px;
}

@media (min-width: 768px) {
  .page {
    grid-template-columns: 2fr 1fr; /* tablet and up: two columns */
    padding: 32px;
  }
}

Common mistakes

  • Forgetting the viewport tag: media queries seem to never trigger on phones.
  • Mixing min-width and max-width in the same stylesheet without a clear logic, which makes conflicts hard to follow.
  • Choosing breakpoints based on specific device models, which change every year.
  • Using fixed pixel widths for the main containers.
  • Hiding essential content on mobile instead of rearranging it: information must remain accessible everywhere.
  • Testing only on your own screen, without ever resizing the window or trying a real phone.

Exercise

Make your card grid (from the previous lesson) display 1 column on mobile, 2 on tablet (from 640px), and 3 on desktop (from 1024px), using media queries.

Exercise: fluid typography

Give your main heading a fluid size withclamp(), then resize the window to check that it stays readable from 320px to 1440px wide.

Exercise: user preferences

Add a prefers-color-scheme: darkmedia query that inverts the colors of your page, then test it by changing your system theme.

Going further

Add a navigation bar that displays all links side by side on desktop, but a simplified menu (for example stacked vertically) below 640px wide.