On this page11 sections
Lecture 053: Responsive Design & Media Queries
| Category | 9: Responsive Design |
| Lecture | 053 of 057 |
| Estimated video length | 14 min |
| Target | CSS3 · any modern browser |
| Level | Intermediate |
More than half of all web visits come from phones. A responsive page uses one set of HTML and adapts its layout to the screen: one column on a phone, two or three on a laptop. Media queries are the tool that makes it happen.
Learning Objectives
- Add the viewport meta tag
- Write media queries with
min-widthandmax-width - Design mobile-first
- Make images and text scale with the screen
1. The Viewport Tag Comes First
Without this line in the <head>, phones show a shrunken desktop page (HTML Lecture 022):
<meta name="viewport" content="width=device-width, initial-scale=1">2. Media Queries
A media query wraps rules that apply only when a condition is true.
@media (min-width: 700px) {
/* these rules apply on screens 700px wide and up */
}Example: drag the bottom-right corner of the preview to make it narrower and wider:
<style>
.box { padding: 20px; color: white; background: #e8590c; text-align: center; }
.box::after { content: "Narrow screen (under 500px)"; }
@media (min-width: 500px) {
.box { background: #1971c2; }
.box::after { content: "Wide screen (500px and up)"; }
}
</style>
<div class="box"></div>3. Mobile-First
Write the phone layout first, with no media query, then add min-width queries for
bigger screens. Phones load the simplest CSS, and each bigger screen adds a little.
Example: one column on phones, three on wider screens (drag to resize):
<style>
.cards { display: grid; gap: 10px; }
.card { background: #e7f5ff; padding: 18px; border-radius: 8px; }
@media (min-width: 560px) {
.cards { grid-template-columns: repeat(3, 1fr); }
}
</style>
<div class="cards">
<div class="card">Card 1</div>
<div class="card">Card 2</div>
<div class="card">Card 3</div>
</div>Common breakpoints: 600px (large phones), 900px (tablets), 1200px (laptops). Pick them where your layout starts to look wrong, not to match particular devices.
4. Flexible Images
One rule stops images from ever overflowing their container:
img { max-width: 100%; height: auto; }Example
<style>
.frame { max-width: 260px; border: 2px dashed #adb5bd; padding: 6px; }
img { max-width: 100%; height: auto; display: block; }
</style>
<div class="frame">
<img src="https://picsum.photos/id/1040/800/500" alt="A castle on a hill" width="800" height="500">
</div>The image is 800px wide, but it shrinks to fit its 260px frame.
5. Text That Scales: clamp()
clamp(min, preferred, max) lets a size grow with the screen, within limits:
Example
<style>
h1 { font-size: clamp(1.4rem, 5vw, 2.6rem); }
</style>
<h1>I grow with the screen, but never too small or too big</h1>Code Examples
Example 1: A Responsive Page
<style>
* { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, sans-serif; }
header { background: #364fc7; color: white; padding: 14px; }
nav { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 6px; }
nav a { color: #dbe4ff; }
.layout { display: grid; gap: 12px; padding: 12px; }
aside { background: #fff3bf; padding: 12px; border-radius: 8px; }
main { background: #f1f3f5; padding: 12px; border-radius: 8px; }
@media (min-width: 640px) {
header { display: flex; justify-content: space-between; align-items: center; }
nav { margin-top: 0; }
.layout { grid-template-columns: 1fr 200px; }
}
</style>
<header>
<strong>Coding Blog</strong>
<nav><a href="#">Home</a><a href="#">Posts</a><a href="#">About</a></nav>
</header>
<div class="layout">
<main><h2>Latest post</h2><p>On a phone the sidebar sits below the post; on a wide screen it moves beside it.</p></main>
<aside><strong>Popular</strong><p>Flexbox in 10 minutes</p></aside>
</div>Common Beginner Mistakes
1. Forgetting the viewport tag. Media queries then don't behave as expected on real phones.
2. Desktop-first with lots of overrides. Starting from the desktop and undoing it for phones usually means more CSS. Start from mobile.
3. Fixed widths. width: 960px breaks small screens. Use max-width and flexible units.
4. Testing only on your laptop. Use DevTools' device mode (the phone icon), or open the page on a real phone.
Practice Questions
- Which
<meta>tag must a responsive page have? - Write a media query for screens 768px wide and up.
- What does "mobile-first" mean?
- Which CSS rule stops images from overflowing their container?
- What does
clamp(1rem, 4vw, 2rem)do? - Make
.gridone column by default and two columns from 600px up.
Practice Answers
<meta name="viewport" content="width=device-width, initial-scale=1">@media (min-width: 768px) { … }- Write the phone styles first, then add
min-widthmedia queries for larger screens. img { max-width: 100%; height: auto; }- It sets a size of 4% of the window width, but never below 1rem or above 2rem.
-
.grid { display: grid; gap: 12px; } @media (min-width: 600px) { .grid { grid-template-columns: 1fr 1fr; } }
Key Takeaways
- Responsive = one page that adapts to every screen; start with the viewport tag.
@media (min-width: …)adds rules for larger screens; design mobile-first.max-width: 100%for images, flexible units andclamp()for text.- Next: build a fluid grid and make images and videos responsive too.