Course topics

By WebNest Studio Engineering Team

CSS Tutorial

Responsive Web Design Principles

Responsive design is the practice of building layouts that adapt gracefully to any screen size, from a small phone to a large desktop monitor, using a single codebase rather than separate sites for each device type. It rests on a handful of core techniques: fluid layouts, flexible media, and media queries (covered in depth in the next topic).

The dominant philosophy for building responsive sites today is "mobile-first": you design and write CSS for the smallest screen first, then progressively add complexity and additional layout for larger screens, rather than designing for desktop and trying to cram it down to mobile afterward.

Why it matters

More than half of web traffic comes from phones, and Google ranks pages by their mobile version. A site that only works at desktop width loses most of its audience. Responsive design is not an extra feature; it is the default expectation for every page.

Mobile-First Design Philosophy

In a mobile-first approach, your base (unqualified) CSS rules describe the simplest, single-column, small-screen experience, and you use min-width media queries to layer on enhancements as the viewport grows — for example, switching from a single column to a multi-column grid once there's enough horizontal space. This approach tends to produce leaner CSS overall, because you're adding complexity only where it's needed, and it naturally prioritizes performance and content on the most constrained devices.

Fluid Layouts

A fluid layout uses relative units (percentages, fr, rem) instead of fixed pixel widths, so containers and content naturally resize with the viewport instead of requiring a breakpoint for every possible screen width. Combining fluid widths with max-width prevents content from becoming uncomfortably wide on very large screens while still shrinking gracefully on small ones — a pattern often seen as width: 100%; max-width: 1200px;.

Flexible Media

Images and other embedded media should scale within their container rather than overflowing it or leaving awkward gaps. The near-universal baseline rule is img, video { max-width: 100%; height: auto; }, which lets media shrink to fit its container on small screens while never exceeding its natural size on larger ones.

Examples

A mobile-first layout that adds columns only once there is enough space

CSS
.gallery {
  display: grid;
  grid-template-columns: 1fr; /* single column by default, mobile-first base */
  gap: 1rem;
}

@media (min-width: 768px) {
  .gallery {
    grid-template-columns: repeat(2, 1fr); /* two columns on tablets and up */
  }
}

@media (min-width: 1200px) {
  .gallery {
    grid-template-columns: repeat(3, 1fr); /* three columns on large desktops */
  }
}
Output
On a phone, gallery items stack in a single column; on a tablet-sized viewport they arrange into two columns; on a large desktop they expand into three, all from one shared stylesheet.

Fluid width with a max-width cap and responsive images

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

img {
  max-width: 100%;
  height: auto;
  display: block;
}
Output
The container fills available width on small screens and centers itself with a comfortable maximum width on large monitors, while every image scales down to fit its column without ever stretching beyond its original resolution.

Common Mistakes

  • Designing for desktop first and retrofitting mobile styles with max-width overrides, which usually results in bloated, override-heavy CSS.
  • Using fixed pixel widths for major layout containers, forcing horizontal scrolling on smaller screens.
  • Forgetting max-width: 100% on images, letting a large image overflow its container on narrow viewports.
  • Adding too many arbitrary breakpoints tuned to specific devices instead of choosing breakpoints based on where the content itself starts to look cramped or sparse.

Exercise

Write mobile-friendly base styles: main takes 90% of the width but never more than 40rem and is centred; images never overflow their container and keep their proportions; and the button fills the full width so that it is easy to tap.

Expected result: on a narrow preview the content fills the width with a margin either side; on a wide one it stops growing at 40rem and stays centred.

CSS · starter code
/* The page contains: <main> with an <h1>, a <p> and a <button> */

main {
  /* TODO: fluid width with a maximum, centred */
}

img {
  /* TODO: never wider than the container; keep proportions */
}

button {
  /* TODO: full width with comfortable padding */
}
Show hints
  • Combine a percentage width with a max-width, and centre with margin: 0 auto.
  • The standard rule for fluid images is max-width: 100%; height: auto;.
Show solution
CSS · solution
/* The page contains: <main> with an <h1>, a <p> and a <button> */

main {
  width: 90%;
  max-width: 40rem;
  margin: 0 auto;
}

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

button {
  display: block;
  width: 100%;
  padding: 0.75rem;
}

Quick quiz

1. What does "mobile-first" mean in CSS?

  • A. Writing the base styles for small screens and adding rules for larger ones with min-width queries
  • B. Hiding content on phones
  • C. Using only pixel units
  • D. Building a separate mobile site
Show answer

A. Larger screens then build on the simpler small-screen layout.

2. Which rule stops an image from overflowing a narrow container?

  • A. width: 100vw
  • B. max-width: 100%
  • C. overflow: visible
  • D. position: fixed
Show answer

B. With height: auto alongside it, the image shrinks in proportion.

3. Which HTML tag must be present for responsive CSS to work on phones?

  • A. A link to a mobile stylesheet
  • B. The charset meta tag
  • C. The viewport meta tag
  • D. The canonical link
Show answer

C. Without it, a phone lays the page out at desktop width and scales it down.

Interview questions

What are the building blocks of responsive design?

Show answer

The viewport meta tag, so the page uses the real width of the device. Fluid layouts, sized in percentages, fr and rem instead of fixed pixels, and built with flexbox and grid. Flexible images and media that scale with their container. Media queries, and now container queries, that change the layout at the widths where it starts to look wrong.

How do you choose breakpoints?

Show answer

From the content, not from particular devices. Start with the narrowest layout, widen the window, and add a breakpoint where the design begins to look stretched or cramped, for example where lines become too long or there is room for a second column. Device sizes change constantly, so breakpoints tied to specific phones date quickly. A handful of breakpoints is usually enough.

Key Points to Remember

  • Mobile-first design starts with base styles for small screens and layers on enhancements with min-width media queries.
  • Fluid layouts use relative units so content resizes naturally instead of relying solely on breakpoints.
  • max-width: 100%; height: auto; is the standard baseline rule for responsive images and media.
  • Choose breakpoints based on where your content needs them, not arbitrary device widths.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Shows a live preview of your HTML, CSS and JavaScript. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering