Responsive Design & Media Queries
Responsive design means one website that works well on every screen, from a small Android phone to a wide office monitor. In East Africa most people browse on phones, so designing for small screens first isn't optional; it's where your users are.
Step 1: the viewport meta tag
Without this line in the head, phones pretend to be a desktop screen about 980px wide and shrink the whole page, so text becomes tiny. Every responsive page needs it:
<meta name="viewport" content="width=device-width, initial-scale=1">
It tells the browser to make the layout as wide as the device's screen. The HTML series covers it in Head, Meta Tags & SEO.
Step 2: fluid layouts and media
Avoid fixed widths for big containers. Use percentages, max-width, fr units and Flexbox or Grid so boxes can shrink and grow. Make images scale down with their container:
img,
video {
max-width: 100%;
height: auto;
}
.container {
width: min(100% - 2rem, 70rem); /* full width minus a gutter, never above 70rem */
margin-inline: auto;
}
The container is the full screen width minus 1rem on each side, but never wider than 70rem (1120px), and it is centred. A lot of responsiveness comes from these fluid techniques alone, before you write a single media query.
Step 3: media queries
A media query applies CSS only when a condition is true, such as the screen being at least a certain width.
@media (min-width: 40rem) {
.cards {
grid-template-columns: repeat(2, 1fr);
}
}
Rules inside the block apply only when the viewport is 40rem (640px) wide or wider. You'll also see the newer range syntax, @media (width >= 40rem), which means the same thing and is supported in all current browsers.
Mobile first
In a mobile-first stylesheet you write the base styles for small screens with no media query at all, then use min-width queries to add enhancements as the screen grows. This keeps phone CSS simple (the phone doesn't load layout rules it then has to undo) and forces you to prioritise content.
/* Base: phones. One column. */
.layout {
display: grid;
gap: 1rem;
}
/* Tablets and up: two columns */
@media (min-width: 40rem) {
.layout { grid-template-columns: 1fr 1fr; }
}
/* Laptops and up: sidebar plus three columns */
@media (min-width: 64rem) {
.layout { grid-template-columns: 220px repeat(3, 1fr); }
}
min-width query adds columns once there is room. Each query builds on the ones before it.Choosing breakpoints
A breakpoint is the width at which a query kicks in. Don't chase specific phone models; there are thousands. Instead, start narrow, widen the browser slowly and add a breakpoint where your design starts to look stretched or cramped. Writing breakpoints in rem or em means they also respond when users zoom their text. Many sites manage with two or three.
Other useful media features
| Media feature | Matches when… |
|---|---|
(orientation: landscape) | The viewport is wider than it is tall |
(hover: hover) | The main input can hover (a mouse), so hover-only effects make sense |
(prefers-color-scheme: dark) | The user has chosen dark mode in their device settings |
(prefers-reduced-motion: reduce) | The user has asked the system to minimise animation |
print | The page is being printed (a media type, used as @media print) |
@media (prefers-color-scheme: dark) {
body { background: #0f172a; color: #e2e8f0; }
}
@media (prefers-reduced-motion: reduce) {
* { animation: none !important; transition: none !important; }
}
Respecting these preferences is part of accessibility: some people feel dizzy or sick from motion on screen. Media queries look at the whole viewport; to adapt a component to the space its container gives it, see container queries in Modern CSS Features.
Try it
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Responsive</title>
<style>
body { font-family: system-ui, sans-serif; margin: 0; background: #f8fafc; }
.container { width: min(100% - 2rem, 60rem); margin-inline: auto; }
.layout { display: grid; gap: 1rem; padding-block: 1rem; }
.box { background: #dbeafe; padding: 1.5rem; border-radius: 8px; }
.label::after { content: " (phone layout)"; }
@media (min-width: 40rem) {
.layout { grid-template-columns: 1fr 1fr; }
.label::after { content: " (tablet layout)"; }
}
@media (min-width: 64rem) {
.layout { grid-template-columns: repeat(4, 1fr); }
.label::after { content: " (desktop layout)"; }
}
@media (prefers-color-scheme: dark) {
body { background: #0f172a; color: #e2e8f0; }
.box { background: #1e3a8a; }
}
</style>
</head>
<body>
<div class="container">
<h1 class="label">Resize me</h1>
<div class="layout">
<div class="box">One</div><div class="box">Two</div>
<div class="box">Three</div><div class="box">Four</div>
</div>
</div>
</body>
</html>
In a narrow preview the four blue boxes stack in one column and the heading reads "Resize me (phone layout)". Widen it past 640px and they form a 2 × 2 grid labelled "tablet layout"; past 1024px all four sit in one row. If your device is in dark mode, the page is dark navy with light text.
Key points
- Always include the viewport meta tag.
- Start with fluid layouts:
max-width: 100%images, flexible containers, Flexbox and Grid. - Write mobile-first base styles, then enhance with
min-widthmedia queries. - Choose breakpoints where your content needs them, and respect
prefers-color-schemeandprefers-reduced-motion.
Tip: use the ← and → keys to move between pages.
