Responsive Design
More than half of web traffic is on phones. Responsive design means one website that looks good on every screen, from a small phone to a wide monitor.
On this page · 5 min · Intermediate
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Intermediate
- Updated
- Oct 28, 2026
Related tutorials
Learning objectives#
- Explain mobile-first design
- Use fluid widths and max-width
- Make images and media flexible
- Use clamp() for fluid font sizes
- Build layouts that adapt without media queries
Explanation#
Start with the viewport meta tag. Without it, phones pretend to be desktops and shrink your page.
Mobile first: write the base styles for small screens, then add rules for bigger screens. Small screens get the simplest CSS, and it is easier to add columns than remove them.
Fluid sizes: use %, max-width, rem and fr instead of fixed pixel widths. img { max-width: 100%; height: auto; } stops images overflowing.
clamp(min, preferred, max) sets a size that grows with the screen but stays within limits: font-size: clamp(1.5rem, 5vw, 3rem).
Intrinsic layouts adapt without media queries: grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)) creates as many columns as fit.
Code example: A layout that adapts on its own#
<style>
* { box-sizing: border-box; }
body { margin: 0; font-family: sans-serif; }
img { max-width: 100%; height: auto; display: block; }
.container { width: min(100% - 2rem, 1000px); margin-inline: auto; }
h1 { font-size: clamp(1.5rem, 5vw, 2.75rem); }
.grid {
display: grid;
gap: 1rem;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}
.card { border: 1px solid #ddd; border-radius: 8px; overflow: hidden; }
.card p { padding: 0 12px; }
</style>
<div class="container">
<h1>Our Destinations</h1>
<div class="grid">
<div class="card"><img src="https://picsum.photos/id/1015/400/250" alt="River valley"><p>Valley</p></div>
<div class="card"><img src="https://picsum.photos/id/1016/400/250" alt="Canyon"><p>Canyon</p></div>
<div class="card"><img src="https://picsum.photos/id/1018/400/250" alt="Mountains"><p>Mountains</p></div>
</div>
</div>
Cards show three per row on wide screens, two on tablets and one on phones — resize the preview to see it change, with no media queries.
Step-by-step explanation#
- box-sizing: border-box makes widths include padding and border.
- Images never exceed their container.
- min(100% - 2rem, 1000px) gives a 1rem gutter on phones and a 1000px max on desktops.
- clamp() makes the heading grow smoothly between 1.5rem and 2.75rem.
- auto-fit + minmax(200px, 1fr) fits as many 200px+ columns as the width allows.
Real-world use case#
Online stores, news sites and dashboards serve the same HTML to phones and desktops; responsive CSS rearranges it.
Common mistakes#
width: 1200px causes a horizontal scrollbar on every phone. Use max-width instead.
Media queries and fluid layouts appear not to work on phones without it.
Use DevTools device mode (Ctrl+Shift+M) and test on a real phone.
Best practices#
- Write mobile-first CSS
- Use max-width instead of width for containers
- Make all images fluid
- Prefer intrinsic layouts (auto-fit, flex-wrap) and add media queries only where needed
- Make tap targets at least 44×44px
Remember this#
Mobile first: design for the smallest screen, then add.
max-width, not width. %, rem and fr, not px.
auto-fit + minmax = columns that count themselves.
Practice exercise#
Your task: Make a .gallery that shows images in as many 150px-minimum columns as fit, with 10px gaps, and fluid images.
Need a hint?
Show solution
.gallery {
display: grid;
gap: 10px;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
}
.gallery img { max-width: 100%; height: auto; display: block; }
Mini quiz#
What does “mobile first” mean?
Want more? Practise all CSS MCQs · CSS interview questions
My notes#
Saved in this browser.