Mobile-First Design: Building for Small Screens First
What Is Mobile-First Design?
Mobile-first design is an approach where you design and code for the smallest screens first, then progressively add features and complexity for larger screens. Instead of starting with a desktop layout and stripping things away for mobile, you start simple and build up.
Why Mobile-First?
Mobile traffic accounts for over 50% of global web traffic. Starting with mobile makes sense because:
- Performance: Mobile users often have slower connections. Starting simple means faster initial load times.
- Focus: Small screens force you to prioritize the most important content and features.
- Progressive enhancement: You add features for larger screens, rather than removing features on smaller ones.
- Easier CSS: It is simpler to add layout rules than to override and undo them.
Desktop-First vs Mobile-First
Desktop-First (Not Recommended)
/* Start with desktop styles */
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 30px;
}
.sidebar {
width: 300px;
float: left;
}
/* Then override for mobile */
@media (max-width: 768px) {
.grid {
grid-template-columns: 1fr;
gap: 15px;
}
.sidebar {
width: 100%;
float: none;
}
}Mobile-First (Recommended)
/* Start with mobile styles - simple, single column */
.grid {
display: grid;
grid-template-columns: 1fr;
gap: 15px;
}
.sidebar {
width: 100%;
}
/* Enhance for tablet */
@media (min-width: 768px) {
.grid {
grid-template-columns: repeat(2, 1fr);
gap: 20px;
}
}
/* Enhance for desktop */
@media (min-width: 1024px) {
.grid {
grid-template-columns: repeat(3, 1fr);
gap: 30px;
}
.sidebar {
width: 300px;
}
}Notice how the mobile-first version adds rules progressively while the desktop-first version must override and undo rules for smaller screens.
Building a Mobile-First Layout
Step 1: Start with Base Mobile Styles
/* Reset and base */
*, *::before, *::after {
box-sizing: border-box;
}
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
font-size: 1rem;
line-height: 1.6;
color: #333;
}
/* Container: full width with padding */
.container {
width: 100%;
padding: 0 16px;
}
/* Single column layout */
.main-content {
width: 100%;
}
.sidebar {
width: 100%;
margin-top: 30px;
}
/* Stack everything vertically */
.page-layout {
display: flex;
flex-direction: column;
}Step 2: Enhance for Tablets
@media (min-width: 768px) {
.container {
padding: 0 24px;
max-width: 768px;
margin: 0 auto;
}
/* Two-column layout for some content */
.card-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 24px;
}
}Step 3: Enhance for Desktop
@media (min-width: 1024px) {
.container {
max-width: 1200px;
padding: 0 40px;
}
/* Sidebar layout */
.page-layout {
flex-direction: row;
gap: 40px;
}
.main-content {
flex: 1;
}
.sidebar {
width: 300px;
flex-shrink: 0;
margin-top: 0;
}
/* Three-column card grid */
.card-grid {
grid-template-columns: repeat(3, 1fr);
}
}Mobile-First Navigation Pattern
/* Mobile: hamburger menu */
.header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 12px 16px;
background-color: white;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
position: sticky;
top: 0;
z-index: 100;
}
.logo {
font-size: 1.25rem;
font-weight: 700;
color: #2c3e50;
}
.menu-btn {
display: flex;
align-items: center;
background: none;
border: none;
font-size: 1.5rem;
cursor: pointer;
padding: 4px;
}
.nav-menu {
display: none;
position: absolute;
top: 100%;
left: 0;
right: 0;
background-color: white;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
list-style: none;
padding: 0;
margin: 0;
}
.nav-menu.open {
display: block;
}
.nav-menu a {
display: block;
padding: 14px 20px;
color: #333;
text-decoration: none;
border-bottom: 1px solid #f0f0f0;
}
/* Tablet/Desktop: horizontal menu */
@media (min-width: 768px) {
.menu-btn {
display: none;
}
.nav-menu {
display: flex;
position: static;
box-shadow: none;
gap: 4px;
}
.nav-menu a {
padding: 8px 16px;
border-bottom: none;
border-radius: 6px;
}
.nav-menu a:hover {
background-color: #f5f5f5;
}
}Mobile-First Typography
/* Mobile base - readable sizes */
body {
font-size: 1rem;
line-height: 1.7;
}
h1 {
font-size: 1.75rem;
line-height: 1.2;
margin-bottom: 0.75rem;
}
h2 {
font-size: 1.375rem;
line-height: 1.3;
margin-top: 2rem;
margin-bottom: 0.5rem;
}
p {
margin-bottom: 1.25rem;
}
/* Larger screens: scale up */
@media (min-width: 768px) {
h1 { font-size: 2.25rem; }
h2 { font-size: 1.75rem; }
}
@media (min-width: 1024px) {
body { font-size: 1.0625rem; }
h1 { font-size: 2.75rem; }
h2 { font-size: 2rem; }
p {
max-width: 70ch; /* Optimal reading width */
}
}Touch-Friendly Design
Mobile users interact with touch, not a mouse. Design accordingly.
/* Minimum touch target size: 44px x 44px (Apple guideline) */
.button,
.nav-link,
.form-control {
min-height: 44px;
min-width: 44px;
}
/* Generous padding on interactive elements */
.button {
padding: 12px 24px;
font-size: 1rem;
}
/* Adequate spacing between touch targets */
.button-group {
display: flex;
flex-wrap: wrap;
gap: 12px;
}
.list-item {
padding: 14px 16px;
}
/* Larger form inputs on mobile */
input,
select,
textarea {
font-size: 1rem; /* Prevents iOS zoom on focus */
padding: 12px;
border: 1px solid #ccc;
border-radius: 8px;
width: 100%;
}
@media (min-width: 768px) {
input,
select,
textarea {
padding: 10px;
}
}Important: Setting `font-size: 1rem` (16px) on inputs prevents iOS Safari from auto-zooming when an input is focused.
Performance Considerations
/* Use efficient selectors */
.card { } /* Good: single class */
div > p.text { } /* Less good: more complex */
/* Avoid expensive properties in animations */
.animate {
/* Good: GPU-accelerated properties */
transition: transform 0.3s ease, opacity 0.3s ease;
}
.animate-slow {
/* Avoid: triggers layout recalculation */
transition: width 0.3s ease, height 0.3s ease;
}
/* Reduce motion for users who prefer it */
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
animation-duration: 0.01ms;
animation-iteration-count: 1;
transition-duration: 0.01ms;
}
}Mobile-First Checklist
When building mobile-first, follow this order:
- Write base styles for the smallest screen first
- Use `min-width` media queries to add tablet and desktop styles
- Ensure touch targets are at least 44px x 44px
- Set form input font-size to at least 16px to prevent iOS zoom
- Use `rem` for typography and spacing
- Test on real devices, not just browser simulators
- Respect `prefers-reduced-motion` for accessibility
- Keep critical CSS small for fast initial render
Summary
Mobile-first design is not just a CSS technique but a design philosophy. Start with the essentials for small screens, then progressively add features for larger ones. This approach results in faster loading pages, better-focused content, cleaner CSS, and a better experience for the majority of users who browse on mobile devices. Use `min-width` media queries, touch-friendly sizing, and performance-conscious animations.
In the next section, you will learn about CSS Transitions and Animations.