HTML Responsive Web Design
People visit websites on phones, tablets, laptops and huge monitors. Responsive web design means building one page that adapts its layout, images and text to whatever screen it is shown on — no separate "mobile site" needed.
Responsive design rests on a few building blocks:
| Building block | What it does |
|---|---|
| Viewport meta tag | Tells mobile browsers to use the real screen width |
| Fluid widths | Sizes in %, fr, vw or max-width instead of fixed pixels |
| Flexible images | Images shrink to fit their container |
| Media queries | Different CSS rules for different screen sizes |
| Relative text units | rem, em, vw, clamp() so text scales sensibly |
1. The viewport
Put this in the <head> of every page:
<meta name="viewport" content="width=device-width, initial-scale=1.0">Without it, a phone renders your page as if it were roughly 980 pixels wide and zooms out, leaving tiny unreadable text.
2. Fluid images
A fixed-width image can overflow a narrow screen. The classic fix is two lines of CSS:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
img.fluid { max-width: 100%; height: auto; display: block; }
.box { border: 2px dashed #999; padding: 8px; font-family: sans-serif; }
</style>
</head>
<body>
<div class="box">
<p>This 800px-wide picture never overflows its box:</p>
<img class="fluid" width="800" height="200" alt="Sunset gradient"
src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='800' height='200'%3E%3Cdefs%3E%3ClinearGradient id='g'%3E%3Cstop offset='0' stop-color='%23ff7e5f'/%3E%3Cstop offset='1' stop-color='%23feb47b'/%3E%3C/linearGradient%3E%3C/defs%3E%3Crect width='800' height='200' fill='url(%23g)'/%3E%3Ccircle cx='400' cy='200' r='70' fill='%23fff3'/%3E%3C/svg%3E">
</div>
</body>
</html>max-width: 100% lets the image shrink but never grow beyond its natural size; height: auto keeps the proportions.
Different images for different screens
The <picture> element lets the browser choose an image file based on the screen:
<picture>
<source media="(min-width: 800px)" srcset="hero-wide.jpg">
<source media="(min-width: 400px)" srcset="hero-medium.jpg">
<img src="hero-small.jpg" alt="Mountain lake at dawn">
</picture>The browser uses the first <source> whose media condition matches, and falls back to the <img>.
3. Media queries
A media query wraps CSS rules that apply only when a condition is true, such as "the screen is at least 600px wide". The usual approach is mobile first: write the simple, single-column styles first, then add media queries for wider screens.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body { font-family: sans-serif; margin: 0; }
.grid { display: grid; grid-template-columns: 1fr; gap: 10px; padding: 10px; }
.grid div { background: #e34c26; color: white; padding: 20px; border-radius: 6px; text-align: center; }
.size::after { content: "Small screen: 1 column"; }
@media (min-width: 500px) {
.grid { grid-template-columns: 1fr 1fr; }
.size::after { content: "Medium screen: 2 columns"; }
}
@media (min-width: 800px) {
.grid { grid-template-columns: repeat(4, 1fr); }
.size::after { content: "Large screen: 4 columns"; }
}
</style>
</head>
<body>
<p class="size" style="padding: 0 10px"></p>
<div class="grid">
<div>One</div><div>Two</div><div>Three</div><div>Four</div>
</div>
</body>
</html>Tip: Resize your browser window or open DevTools' device toolbar (Ctrl+Shift+M in Chrome and Firefox) to watch the layout switch between 1, 2 and 4 columns.
Common breakpoints are around 480px (phones), 768px (tablets) and 1024px (laptops), but the best breakpoints are wherever your design starts to look cramped.
4. Responsive text
clamp(min, preferred, max) lets text grow with the viewport while staying within sensible limits.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
h1 { font-family: sans-serif; font-size: clamp(1.5rem, 6vw, 3.5rem); color: #1e293b; }
p { font-family: sans-serif; font-size: 1rem; max-width: 60ch; line-height: 1.6; }
</style>
</head>
<body>
<h1>I scale with the window</h1>
<p>The heading is never smaller than 1.5rem or larger than 3.5rem. The paragraph is limited to about 60 characters per line, which is comfortable to read on any screen.</p>
</body>
</html>5. Layouts that adapt without media queries
Grid's auto-fit with minmax() creates as many columns as fit, automatically:
<!DOCTYPE html>
<html>
<head>
<style>
.gallery {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
gap: 8px;
}
.gallery div { aspect-ratio: 1; display: grid; place-items: center; font-size: 40px; background: #f1f5f9; border-radius: 8px; }
</style>
</head>
<body>
<div class="gallery">
<div>🌲</div><div>🌊</div><div>🏔️</div><div>🌵</div><div>🌻</div><div>🍁</div>
</div>
</body>
</html>Checklist
- Viewport meta tag in every page.
- No fixed widths wider than a phone; prefer
max-width. - Images with
max-width: 100%; height: auto. - Mobile-first CSS, enhanced with
min-widthmedia queries. - Test on real devices or in DevTools' responsive mode.
Test yourself
What does the CSS img { max-width: 100%; height: auto; } achieve?
- Stretches every image to full width
- Lets images shrink to fit their container while keeping proportions
- Hides images on small screens
- Makes images load faster
In a mobile-first stylesheet, which media query adds rules for wider screens?
@media (max-width: 600px)@media (min-width: 600px)@media print@media (orientation: mobile)
Which element lets the browser pick between several image files depending on the screen?
<figure><picture><canvas><imgset>