Introduction
Mobile-first design means writing your base CSS for small screens (phones) first, then progressively adding complexity for larger screens using min-width media queries. This is the opposite of the older 'desktop-first' approach, which started with a wide layout and shrank it down with max-width queries. Since most global web traffic today comes from mobile devices, mobile-first ensures the core experience is fast and usable for the majority of users by default.
Cricket analogy: Mobile-first CSS is like training a young cricketer on basic technique first (forward defense, straight bat) before layering on advanced shots for bigger grounds, rather than starting them on IPL-style power-hitting and scaling back for club cricket.
Approach
/* Base styles: apply to all screens, mobile included */
.container {
display: flex;
flex-direction: column;
padding: 1rem;
}
.card {
width: 100%;
}
/* Enhance for tablets and up */
@media (min-width: 600px) {
.container {
flex-direction: row;
flex-wrap: wrap;
padding: 1.5rem;
}
.card {
width: 48%;
}
}
/* Enhance for desktops and up */
@media (min-width: 1024px) {
.container {
padding: 2rem;
}
.card {
width: 31%;
}
}Explanation
In the code above, the unqualified rules (no media query) are the mobile baseline: a single-column layout with full-width cards. The min-width: 600px query then overrides those rules once the viewport is at least 600px wide, switching to a row layout. A further min-width: 1024px query refines spacing and card widths for large desktop screens. Because each query only adds or overrides rules for wider viewports, the CSS naturally cascades upward, and mobile devices never have to download or parse styles meant for larger screens that don't apply to them.
Cricket analogy: The unqualified base styles are like a batter's default stance used on any pitch, the 600px query is like adjusting technique for a bouncier pitch, and the 1024px query is like fine-tuning further for a fast, true wicket — younger players on club pitches never need to learn the advanced adjustments.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!-- The viewport meta tag is REQUIRED for mobile-first CSS to work correctly -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mobile-First Example</title>
</head>
<body>
<div class="container">
<div class="card">Product A</div>
<div class="card">Product B</div>
<div class="card">Product C</div>
</div>
</body>
</html>Without the <meta name="viewport" content="width=device-width, initial-scale=1.0"> tag, mobile browsers render the page at a fake desktop width (usually 980px) and then scale it down, which breaks min-width media queries entirely.
Key Takeaways
- Mobile-first writes unqualified base styles for small screens, then layers on min-width media queries for larger screens.
- It typically produces leaner CSS because mobile devices don't have to override desktop styles.
- Always include the viewport meta tag or media queries won't behave as expected on real devices.
- It aligns with progressive enhancement: start with a solid core experience, then enhance it.
Practice what you learned
1. In mobile-first design, which media query feature is primarily used to add styles for larger screens?
2. Which meta tag is required in the <head> for mobile-first CSS to render correctly on real mobile devices?
3. What is a key advantage of mobile-first CSS compared to desktop-first CSS?
4. In a desktop-first approach, which media query feature is most commonly used instead?
Was this page helpful?
You May Also Like
Responsive Design and Media Queries
Techniques and CSS media queries used to adapt layouts across different screen sizes and devices.
Responsive Images
Techniques like srcset, sizes, and the picture element that let browsers choose the most appropriate image for a device's screen size and resolution.
CSS Units for Layout (px, %, rem, em, vh/vw)
How absolute and relative CSS length units differ and when to use px, %, rem, em, and viewport units in layouts.
Related Reading
Related Study Notes in Web Development
Browse all study notesWebSockets Study Notes
Web Development · 30 topics
Web DevelopmentWebAssembly Study Notes
WebAssembly · 30 topics
Web DevelopmentgRPC Study Notes
Protocol Buffers · 30 topics
Web DevelopmentSpring Boot Study Notes
Java · 30 topics
Web DevelopmentFlask Study Notes
Python · 30 topics
Web DevelopmentDjango Study Notes
Python · 30 topics