Responsive Design & Mobile First

Difficulty: Intermediate

Question

How do you implement responsive design, and what does a mobile-first approach mean in practice?

Answer

Responsive design means one codebase whose layout, typography and media adapt to any screen size, from a ₹8,000 Android phone to a 27-inch monitor. Mobile-first means you write the base CSS for the smallest screen and then add min-width media queries that layer on complexity as space grows, instead of building for desktop and stripping things away.

The building blocks

  1. Viewport meta tag: <meta name="viewport" content="width=device-width, initial-scale=1">. Without it mobile browsers render the page at roughly 980px wide and zoom out, so your media queries never fire as expected.
  2. Fluid layouts: percentages, fr units, max-width and flex or grid instead of fixed pixel widths.
  3. Media queries: change layout at breakpoints where the content starts to look wrong, not at specific device widths.
  4. Fluid typography and spacing: clamp(1.5rem, 4vw + 0.5rem, 3rem) scales smoothly between a minimum and a maximum.
  5. Responsive media: srcset/sizes and <picture> so a phone does not download a 2,400px hero image.
  6. Container queries: let a component adapt to the space it is placed in, not the whole viewport.

Why mobile-first wins

Mobile-firstDesktop-first
Base styles targetSmall screensLarge screens
Queries usemin-widthmax-width
CSS flowAdd columns and featuresUndo and hide things
Small devices downloadOnly what they needDesktop rules plus overrides

Most Indian users browse on phones over variable mobile networks, so starting from the constrained case forces you to prioritise content and keeps the base stylesheet lean. Simple single-column styles are also the natural default for block layout; you only add grid columns when there is room.

Choosing breakpoints

Common frameworks use Tailwind's 640 / 768 / 1024 / 1280 / 1536px or Bootstrap's 576 / 768 / 992 / 1200 / 1400px. Treat these as a starting point. Resize the browser slowly and add a breakpoint where line lengths get too long, cards get squashed or navigation wraps. Modern range syntax @media (width >= 768px) works in all current browsers and reads more clearly than min-width.

Mistakes that break responsive layouts

  • Forgetting the viewport meta tag
  • Fixed widths like width: 1200px instead of max-width: 1200px with width: 100%
  • Using 100vh for full-height sections on mobile; prefer 100dvh or 100svh so the address bar does not hide content
  • Hover-only interactions; touch screens have no hover, so check @media (hover: hover)
  • Tap targets smaller than about 44 by 44 CSS pixels
  • Hiding content on mobile with display: none while it still downloads heavy images

Test with DevTools device mode, then on at least one real low-end Android phone and one iPhone, because emulators do not reproduce slow CPUs or real touch behaviour.

Building a page mobile-first. Start with the smallest layout and add complexity only as space allows.
  1. Add the viewport meta tag: Makes CSS pixels match the device width.
  2. Write single-column base styles: No media query; this is the phone layout.
  3. Use fluid units and clamp(): Type and spacing scale smoothly between sizes.
  4. Add min-width breakpoints: Introduce columns where content needs room.
  5. Serve responsive images: srcset and sizes keep downloads small on phones.
  6. Test on real devices: Check touch targets, slow CPUs and dvh behaviour.

Code examples

Viewport meta tag

<head>
  <meta charset="utf-8">
  <!-- Required: match the layout viewport to the device width -->
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!-- Never add maximum-scale=1 or user-scalable=no:
       it blocks pinch-zoom and fails accessibility checks -->
</head>

This single line is what makes media queries behave on phones. Disabling zoom is a common mistake that hurts low-vision users.

Mobile-first layout with fluid type

/* Base: phones, no media query */
.container { padding: 1rem; }
.products {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
}
h1 { font-size: clamp(1.5rem, 4vw + 0.5rem, 3rem); }

/* Tablets and up */
@media (width >= 768px) {
  .container { padding: 2rem; }
  .products { grid-template-columns: repeat(2, 1fr); }
}

/* Laptops and up */
@media (width >= 1024px) {
  .container { max-width: 1200px; margin-inline: auto; }
  .products { grid-template-columns: repeat(4, 1fr); }
}

/* Full-height hero that respects mobile browser UI */
.hero { min-height: 100svh; }

/* Hover effects only where hover exists */
@media (hover: hover) {
  .product:hover { transform: translateY(-2px); }
}

Each breakpoint only adds rules, so phones parse the simplest version. clamp() removes the need for separate font sizes at every breakpoint.

Key points

  • Always include the viewport meta tag and never disable zoom.
  • Mobile-first = base styles for small screens + min-width queries to enhance.
  • Choose breakpoints where the content breaks, not by device names.
  • Use max-width, %, fr, flex and grid instead of fixed pixel widths.
  • clamp() gives fluid type and spacing without extra breakpoints.
  • Use dvh or svh instead of 100vh for full-height mobile sections.

Common follow-up questions

  • What is the difference between media queries and container queries?
  • How do srcset and sizes decide which image the browser downloads?
  • Why does 100vh cause problems on mobile browsers?
  • How would you make a data table responsive?
  • What does clamp() do and how would you calculate the preferred value?

Interview tips

  • Start with the viewport meta tag; it shows you know the foundation, not just the media query syntax.
  • Explain why mobile-first is better (less CSS to undo, smaller download), not only what it is.
  • Mention testing on a real low-end Android device; it signals practical experience.

Concepts covered

Mobile-first, Media queries, Viewport meta tag, Fluid layout, clamp(), Responsive images

More HTML & CSS Interview questions