Responsive Design
Mobile-First Development
Learn the mobile-first methodology for building modern responsive websites. Start with mobile design and progressively enhance for larger screens.
What is Mobile-First?
Mobile-first is a design philosophy where you design and build for mobile devices first, then add features and layouts for larger screens using media queries.
⌠Desktop-First (Old Way)
- Design for desktop (1920px)
- Remove features for tablets
- Remove more features for mobile
- Result: Clunky mobile experience
✅ Mobile-First (Modern Way)
- Design for mobile (375px)
- Add features for tablets
- Add more features for desktop
- Result: Great experience on all devices
Why Mobile-First?
Performance Benefits
- ✅ Load only what's needed for mobile (faster)
- ✅ Progressive enhancement for larger screens
- ✅ Smaller initial CSS download
- ✅ Better for slow mobile networks
User Experience Benefits
- ✅ Forces focus on essential content
- ✅ Touch-friendly by design
- ✅ Better mobile usability
- ✅ Consistent experience across devices
Development Benefits
- ✅ Simpler CSS (adding vs. removing)
- ✅ Easier to maintain
- ✅ Better performance by default
- ✅ Future-proof (mobile traffic growing)
Mobile-First CSS
⌠Desktop-First Approach
CSS
/* Desktop styles (default) */
.container {
width: 1200px;
display: grid;
grid-template-columns: repeat(4, 1fr);
}
/* Remove columns for tablet */
@media (max-width: 1024px) {
.container {
width: 100%;
grid-template-columns: repeat(2, 1fr);
}
}
/* Remove more for mobile */
@media (max-width: 768px) {
.container {
grid-template-columns: 1fr;
}
}
/* Problems:
- Mobile devices load desktop CSS first
- Overriding is complex
- Performance impact on mobile -->✅ Mobile-First Approach
CSS
/* Mobile styles (default) */
.container {
width: 100%;
display: grid;
grid-template-columns: 1fr;
}
/* Add columns for tablet */
@media (min-width: 768px) {
.container {
grid-template-columns: repeat(2, 1fr);
}
}
/* Add more columns for desktop */
@media (min-width: 1024px) {
.container {
max-width: 1200px;
margin: 0 auto;
grid-template-columns: repeat(4, 1fr);
}
}
/* Benefits:
- Mobile gets simple CSS first
- Progressive enhancement
- Better performance -->Mobile-First Layout Example
HTML
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
/* === MOBILE FIRST (Base Styles) === */
body {
font-family: Arial, sans-serif;
font-size: 16px;
line-height: 1.6;
padding: 10px;
}
header {
background: #333;
color: white;
padding: 15px;
margin-bottom: 20px;
}
nav ul {
list-style: none;
}
nav li {
margin: 10px 0;
}
nav a {
color: white;
text-decoration: none;
display: block;
padding: 10px;
background: #444;
}
main {
padding: 15px;
}
.card {
background: #f5f5f5;
padding: 20px;
margin-bottom: 20px;
border-radius: 8px;
}
img {
max-width: 100%;
height: auto;
}
/* === TABLET (768px+) === */
@media (min-width: 768px) {
body {
padding: 20px;
}
nav ul {
display: flex;
gap: 10px;
}
nav li {
margin: 0;
}
.grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 20px;
}
}
/* === DESKTOP (1024px+) === */
@media (min-width: 1024px) {
body {
max-width: 1200px;
margin: 0 auto;
}
.layout {
display: grid;
grid-template-columns: 250px 1fr;
gap: 30px;
}
.grid {
grid-template-columns: repeat(3, 1fr);
}
}
</style>
</head>
<body>
<header>
<h1>Mobile-First Site</h1>
<nav>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#services">Services</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
</header>
<div class="layout">
<aside>
<h2>Sidebar</h2>
<p>Sidebar content</p>
</aside>
<main>
<h1>Main Content</h1>
<div class="grid">
<div class="card">Card 1</div>
<div class="card">Card 2</div>
<div class="card">Card 3</div>
<div class="card">Card 4</div>
<div class="card">Card 5</div>
<div class="card">Card 6</div>
</div>
</main>
</div>
</body>
</html>Mobile-First Typography
CSS
/* Mobile base (16px) */
body {
font-size: 16px;
line-height: 1.5;
}
h1 { font-size: 1.75rem; } /* 28px */
h2 { font-size: 1.5rem; } /* 24px */
h3 { font-size: 1.25rem; } /* 20px */
p { margin-bottom: 1rem; }
/* Tablet: slightly larger */
@media (min-width: 768px) {
body { font-size: 18px; }
h1 { font-size: 2rem; }
h2 { font-size: 1.75rem; }
}
/* Desktop: larger still */
@media (min-width: 1024px) {
body { font-size: 18px; }
h1 { font-size: 2.5rem; }
h2 { font-size: 2rem; }
}
/* Or use fluid typography */
h1 {
font-size: clamp(1.75rem, 4vw, 3rem);
}Mobile-First Images
HTML
<!-- Load small image by default (mobile) -->
<img src="image-small-400w.jpg"
srcset="image-small-400w.jpg 400w,
image-medium-800w.jpg 800w,
image-large-1200w.jpg 1200w"
sizes="(min-width: 1024px) 1200px,
(min-width: 768px) 800px,
400px"
alt="Responsive image">
<!-- Browser automatically loads:
- 400w image on mobile
- 800w image on tablet
- 1200w image on desktop -->Mobile-First JavaScript
Load Features Conditionally
JAVASCRIPT
// Only load heavy features on desktop
if (window.innerWidth >= 1024) {
// Load parallax scrolling
loadParallax();
// Load animations
loadAnimations();
}
// Feature detection
if ('IntersectionObserver' in window) {
// Use modern API
setupLazyLoading();
} else {
// Fallback for older browsers
loadAllImages();
}
// Touch vs. mouse
if ('ontouchstart' in window) {
// Touch device - use touch events
setupTouchGestures();
} else {
// Desktop - use mouse events
setupMouseHovers();
}Mobile-First Navigation
HTML
<style>
/* Mobile: Hamburger menu */
.nav-toggle {
display: block;
background: none;
border: none;
font-size: 24px;
cursor: pointer;
}
.nav-menu {
display: none;
flex-direction: column;
}
.nav-menu.active {
display: flex;
}
/* Desktop: Always show menu */
@media (min-width: 768px) {
.nav-toggle {
display: none;
}
.nav-menu {
display: flex;
flex-direction: row;
}
}
</style>
<nav>
<button class="nav-toggle" onclick="toggleMenu()">☰</button>
<ul class="nav-menu" id="navMenu">
<li><a href="#home">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#services">Services</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
<script>
function toggleMenu() {
document.getElementById('navMenu').classList.toggle('active');
}
</script>Testing Mobile-First
Test in This Order:
- Mobile (320px - 375px): Start here, test everything
- Phablet (414px - 480px): Larger phones
- Tablet Portrait (768px): Test enhanced features
- Tablet Landscape (1024px): Test grid layouts
- Desktop (1280px - 1920px): Test full experience
Real Device Testing
- ✅ Test on actual phones and tablets
- ✅ Test different browsers (Safari, Chrome, Firefox)
- ✅ Test slow 3G connections
- ✅ Test touch interactions
- ✅ Test in bright sunlight (contrast)
Mobile-First Checklist
Content
- ☑ Focus on essential content first
- ☑ Prioritize above-the-fold content
- ☑ Keep mobile content concise
- ☑ Use progressive disclosure
Design
- ☑ Design mobile layout first
- ☑ Use single-column layout as base
- ☑ Touch targets 48x48px minimum
- ☑ Adequate spacing between elements
- ☑ Readable font sizes (16px+)
Code
- ☑ Write mobile CSS first (no media query)
- ☑ Use min-width media queries
- ☑ Load mobile images first
- ☑ Lazy load below-the-fold content
- ☑ Defer non-critical JavaScript
Performance
- ☑ Optimize for 3G networks
- ☑ Minimize initial page weight
- ☑ Use image compression
- ☑ Minimize render-blocking resources
- ☑ Test with Chrome Lighthouse
Common Mistakes to Avoid
⌠Hiding Content on Mobile
If content is important, show it on mobile too (maybe differently).
⌠Using max-width Media Queries
Use min-width for mobile-first approach.
⌠Testing Only in DevTools
Always test on real devices - touch, performance, and network differ.
⌠Ignoring Performance
Mobile users often have slow connections - optimize aggressively.
⌠Assuming High-Speed Internet
Test on 3G or slower to understand real mobile experience.
Mobile-First Frameworks
CSS Frameworks (Mobile-First)
- Bootstrap 5: Mobile-first grid and components
- Tailwind CSS: Utility-first, mobile-first responsive design
- Bulma: Modern mobile-first CSS framework
Best Practices from Frameworks
- ✅ Use flexible grids
- ✅ Use relative units
- ✅ Mobile-first breakpoints
- ✅ Component-based design