Mobile-First Design Principles: A Complete Guide for Frontend Developers
Mobile-First Design Principles: A Complete Guide for Frontend Developers
/* Base styles - Mobile first */
.container {
padding: 16px;
display: flex;
flex-direction: column;
gap: 12px;
}
.card {
width: 100%;
padding: 16px;
border-radius: 8px;
}
/* Tablet - 768px and up */
@media (min-width: 768px) {
.container {
padding: 24px;
flex-direction: row;
flex-wrap: wrap;
}
.card {
width: calc(50% - 12px);
}
}
/* Desktop - 1024px and up */
@media (min-width: 1024px) {
.container {
padding: 32px;
max-width: 1200px;
margin: 0 auto;
}
.card {
width: calc(33.33% - 16px);
}
}/* Mobile-first typography */
body {
font-size: 16px; /* Never go below 16px on mobile */
line-height: 1.5;
}
h1 {
font-size: 1.75rem;
}
h2 {
font-size: 1.5rem;
}
@media (min-width: 768px) {
h1 { font-size: 2.25rem; }
h2 { font-size: 1.75rem; }
}
@media (min-width: 1024px) {
h1 { font-size: 3rem; }
h2 { font-size: 2rem; }
}Mobile-first design means designing for the smallest screen first and then progressively enhancing the layout for larger screens. This approach leads to better performance, cleaner code, and improved user experience.
Why Mobile-First?
- Over 60% of web traffic comes from mobile devices
- Forces you to prioritize content and features
- Results in faster loading times on mobile networks
- Progressive enhancement is easier than graceful degradation
- Google uses mobile-first indexing for search rankings
CSS Media Queries: Mobile-First Approach
Start with base styles for mobile, then add breakpoints for larger screens:
/* Base styles - Mobile first */
.container {
padding: 16px;
display: flex;
flex-direction: column;
gap: 12px;
}
.card {
width: 100%;
padding: 16px;
border-radius: 8px;
}
/* Tablet - 768px and up */
@media (min-width: 768px) {
.container {
padding: 24px;
flex-direction: row;
flex-wrap: wrap;
}
.card {
width: calc(50% - 12px);
}
}
/* Desktop - 1024px and up */
@media (min-width: 1024px) {
.container {
padding: 32px;
max-width: 1200px;
margin: 0 auto;
}
.card {
width: calc(33.33% - 16px);
}
}/* Mobile-first typography */
body {
font-size: 16px; /* Never go below 16px on mobile */
line-height: 1.5;
}
h1 {
font-size: 1.75rem;
}
h2 {
font-size: 1.5rem;
}
@media (min-width: 768px) {
h1 { font-size: 2.25rem; }
h2 { font-size: 1.75rem; }
}
@media (min-width: 1024px) {
h1 { font-size: 3rem; }
h2 { font-size: 2rem; }
}