CSS · Lesson 9 of 12 · 4 min
Responsive Design Strategies & Mobile First
You will be asked to explain mobile-first. This is how you answer, and how you prove you think about performance and maintainability.
Mobile-First is the Default, Not a Choice
In an interview, if you're asked how you'd approach responsive design, the answer is "mobile-first." It's the professional standard. Don't present it as one of several equal options.
Mobile-first means you write your base CSS for the smallest screen. You then use min-width media queries to add or modify styles as the screen gets larger. This isn't just a preference; it's a performance and maintenance strategy.
Mobile users get a lean, fast-loading stylesheet. They don't download desktop-specific layout rules, large-screen assets, or complex hover effects. This is progressive enhancement: start with a solid core experience and build on it. The alternative, desktop-first ("graceful degradation"), forces mobile browsers to parse desktop styles and then parse more rules to override them. It's inefficient and leads to bloated, harder-to-maintain CSS.
The interview question: "Why is mobile-first better than desktop-first?"
Your answer should hit three points:
- Performance: Mobile devices download less CSS, leading to a faster First Contentful Paint.
- Maintainability: It's easier to add complexity than to remove or override it. Scaling up from a simple layout results in cleaner, more predictable CSS.
- Content Focus: Designing for small screens forces you to prioritize what's most important, leading to a better user experience on all devices.
How to Structure Mobile-First CSS
The pattern is simple: write your default styles, then layer on complexity inside min-width media queries.
Here is the classic example: a content area and a sidebar. On mobile, they stack vertically. On a larger screen, they sit side-by-side.
<div class="container">
<main class="content">
<h2>Main Content</h2>
<p>This is the primary content area. It appears first in the DOM.</p>
</main>
<aside class="sidebar">
<h3>Sidebar</h3>
<p>This is supplementary content.</p>
</aside>
</div>
The CSS starts with the mobile, stacked layout. No media query needed.
/* Base (Mobile) Styles */
.container {
display: flex;
flex-direction: column; /* Stacks items vertically by default */
gap: 1rem;
}
.content,
.sidebar {
/* Basic styling for both elements */
padding: 1rem;
background-color: #f0f0f0;
border-radius: 4px;
}
/* Tablet & Up Styles */
@media (min-width: 768px) {
.container {
flex-direction: row; /* Switches to horizontal layout */
}
.content {
flex: 3; /* Takes up 3/4 of the space */
}
.sidebar {
flex: 1; /* Takes up 1/4 of the space */
}
}
This code is minimal and efficient. The mobile browser only needs to parse the first few rules. The min-width media query is ignored. A desktop browser parses everything and applies the layout change.
Beyond Media Queries: Intrinsic Responsiveness
The best responsive design uses as few media queries as possible. Modern CSS gives you tools to build components that are "intrinsically" responsive—they adapt to their container without explicit breakpoints. Mentioning these shows you're current.
Grid is for this. You learned about Grid in the last lesson. The auto-fit and minmax() pattern is the most powerful example. This creates a grid that automatically adjusts the number of columns based on available space.
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
This one line of code replaces multiple media queries. It says: "create as many columns as you can fit. Each column should be at least 250px, and any extra space should be distributed equally."
Fluid typography is another area where you can avoid media queries. Use the clamp() function to set a font size that grows with the viewport but has a defined minimum and maximum.
font-size: clamp(1rem, 2.5vw, 1.5rem);
This sets the font size to be 2.5% of the viewport width, but never smaller than 1rem or larger than 1.5rem. It's a single declaration for smooth scaling.
Handling Responsive Images
Serving a 1MB, 2000px-wide hero image to a mobile device is a common performance blunder. An interviewer might ask how you'd optimize image delivery for different screen sizes.
There are two primary techniques.
-
Resolution Switching with
srcsetandsizes: Use this when you want to show the same image, just at a different size.srcsetprovides the browser with a list of available image files and their widths.sizestells the browser how much space the image will occupy on the screen. The browser then does the math and picks the most efficient image to download.<img srcset="image-small.jpg 480w, image-medium.jpg 800w, image-large.jpg 1200w" sizes="(max-width: 600px) 100vw, 50vw" src="image-medium.jpg" alt="A descriptive alt text."> -
Art Direction with
<picture>: Use this when you need to show a different image—like a different crop or aspect ratio—on different screen sizes. The<picture>element lets you provide multiple<source>elements with media queries.<picture> <source media="(min-width: 1024px)" srcset="desktop-landscape.jpg"> <source media="(min-width: 600px)" srcset="tablet-square.jpg"> <img src="mobile-portrait.jpg" alt="A descriptive alt text."> </picture>
The trap: Forgetting max-width: 100%; and height: auto; on your images. Without this, your fluid images will still break the layout.
Use Content-Driven Breakpoints
A junior developer memorizes device widths. A senior developer lets the content define the breakpoints.
When an interviewer asks, "What breakpoints do you use?", the correct answer is not a list of numbers. The answer is: "I start with the mobile layout and expand my browser window. When the content starts to look awkward or the line lengths get too long, that's where I add a breakpoint."
Your breakpoints should be few and meaningful. A layout might only need one or two. If you find yourself adding dozens of breakpoints, your layout is probably too rigid. Look for opportunities to use intrinsic techniques like auto-fit or clamp() instead.
This workflow demonstrates a mature, content-focused approach.
Try it
Take a non-responsive component you've built before, like a user profile card. Rewrite its CSS using the mobile-first approach. Start with a single-column, stacked layout for all elements, then add one min-width media query to create a two-column layout for larger screens.
Next: CSS Transitions, Transforms & Animations
Lock it in — apply this lesson under interview conditions.