Breakpoints, media queries, and responsive design patterns
Design for mobile first, then scale up
/* Mobile styles first */
@media (min-width: 768px) { /* Tablet */ }
@media (min-width: 1024px) { /* Desktop */ }
Standard device breakpoints
/* Mobile: 320px-767px */
/* Tablet: 768px-1023px */
/* Desktop: 1024px+ */
/* Large: 1440px+ */
Define your own breakpoints
/* Custom breakpoint */
@media (min-width: 1200px) {
.container { max-width: 1140px; }
}
Styles apply when viewport is at least this wide
@media (min-width: 768px) {
.sidebar { display: block; }
}
Styles apply when viewport is at most this wide
@media (max-width: 767px) {
.sidebar { display: none; }
}
Target device orientation
@media (orientation: landscape) {
.header { height: 60px; }
}
Target dark/light mode preference
@media (prefers-color-scheme: dark) {
body { background: #1a1a1a; }
}
Units relative to viewport size
width: 100vw; /* Full viewport width */
height: 100vh; /* Full viewport height */
font-size: 4vw; /* Responsive font size */
Relative to parent element
width: 100%; /* Full parent width */
padding: 5%; /* 5% of parent width */
Relative to font size
font-size: 1rem; /* 16px by default */
margin: 2em; /* 2x current font size */
Responsive value with min/max bounds
font-size: clamp(1rem, 4vw, 2rem);
width: clamp(300px, 50%, 800px);
Images that scale properly
img {
max-width: 100%;
height: auto;
}
Multiple image sources for different sizes
<picture>
<source media="(min-width: 768px)" srcset="large.jpg">
<img src="small.jpg" alt="Description">
</picture>
Control how images scale
img {
width: 100%;
height: 200px;
object-fit: cover;
}
Responsive grid that starts mobile and scales up
.grid {
display: grid;
grid-template-columns: 1fr;
gap: 1rem;
padding: 1rem;
}
@media (min-width: 768px) {
.grid {
grid-template-columns: repeat(2, 1fr);
gap: 2rem;
padding: 2rem;
}
}
@media (min-width: 1024px) {
.grid {
grid-template-columns: repeat(3, 1fr);
gap: 3rem;
padding: 3rem;
}
}
Navigation that adapts to screen size
.nav {
display: flex;
flex-direction: column;
gap: 1rem;
}
.nav-toggle {
display: block;
}
.nav-menu {
display: none;
}
@media (min-width: 768px) {
.nav {
flex-direction: row;
align-items: center;
}
.nav-toggle {
display: none;
}
.nav-menu {
display: flex;
}
}
Text that scales with viewport size
html {
font-size: 16px;
}
h1 {
font-size: clamp(2rem, 5vw, 4rem);
line-height: 1.2;
}
p {
font-size: clamp(1rem, 2vw, 1.25rem);
line-height: 1.6;
}
Modern approach to component-based responsive design
.card {
container-type: inline-size;
}
.card-content {
padding: 1rem;
}
@container (min-width: 400px) {
.card-content {
padding: 2rem;
}
.card-title {
font-size: 1.5rem;
}
}
Start with mobile-first design approach
Use clamp()
for fluid typography
Test on real devices, not just browser dev tools
Use max-width
for containers
Consider touch targets (minimum 44px) for mobile