On this page11 sections
Lecture 024: Responsive Web Pages
| Category | 4: Page Structure |
| Lecture | 024 of 048 |
| Estimated video length | 12 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
A responsive page looks good on every screen, from a small phone to a wide monitor, using the same HTML. It starts with one tag in the
<head>, then a few habits: flexible images, flexible text, and layouts that rearrange themselves.
Learning Objectives
- Add the viewport meta tag
- Make images shrink to fit their container
- Size text relative to the screen
- Switch a layout between one and several columns with a media query
1. The Viewport Tag
<meta name="viewport" content="width=device-width, initial-scale=1.0">Without it, phones pretend to be about 980px wide and shrink the whole page to fit, so everything looks tiny. With it, the page is laid out for the phone's real width.
2. Images That Fit
Example: drag the preview corner to make it narrower:
<img src="https://picsum.photos/id/1043/900/400" alt="Autumn forest" style="max-width: 100%; height: auto;">| Rule | Effect |
|---|---|
width: 100% | Always fills the container. A small image may stretch and blur |
max-width: 100% | Shrinks when needed, but never grows past its real size. Usually best |
3. Different Images for Different Screens
The <picture> element (Lecture 012) can swap in a smaller or differently cropped image on phones:
<picture>
<source media="(max-width: 600px)" srcset="hero-small.jpg">
<img src="hero-large.jpg" alt="Our shop" style="max-width: 100%;">
</picture>4. Text That Scales
The vw unit is 1% of the viewport width, so 5vw grows and shrinks with the screen.
Wrap it in clamp() to keep it between a minimum and a maximum.
Example
<h1 style="font-size: clamp(1.4rem, 6vw, 3rem);">I resize with the screen</h1>
<p style="font-size: clamp(1rem, 2.5vw, 1.25rem);">So do I, within sensible limits.</p>5. Media Queries: Change the Layout
A media query applies CSS only when a condition is true, such as "the screen is at least 600px wide".
Example: three columns on wide screens, stacked on narrow ones (drag the corner):
<style>
.col { padding: 14px; margin: 4px 0; color: white; border-radius: 6px; }
.a { background: #1971c2; } .b { background: #2f9e44; } .c { background: #e8590c; }
@media (min-width: 600px) {
.row { display: flex; gap: 8px; }
.col { flex: 1; }
}
</style>
<div class="row">
<div class="col a">Left</div>
<div class="col b">Main</div>
<div class="col c">Right</div>
</div>6. Frameworks
Frameworks such as Bootstrap and Tailwind CSS come with ready-made responsive grids and components. They're popular, but learn the basics above first, so you understand what the framework does for you.
Common Beginner Mistakes
1. Forgetting the viewport tag. Nothing else works properly on phones without it.
2. Fixed widths. width: 900px can't fit a 390px phone. Use max-width and percentages.
3. Tiny tap targets. Buttons and links should be big enough to tap with a thumb.
4. Testing only on a laptop. Use your browser's device mode (DevTools), or a real phone.
Practice Questions
- Write the viewport meta tag.
- Which is usually better for images,
width: 100%ormax-width: 100%? Why? - What does
5vwmean? - What does a media query do?
- Write a media query for screens 768px wide and up.
- Name one responsive CSS framework.
Practice Answers
<meta name="viewport" content="width=device-width, initial-scale=1.0">max-width: 100%. It shrinks images but never stretches them beyond their real size.- 5% of the viewport (browser window) width.
- It applies CSS only when a condition, such as a minimum screen width, is true.
@media (min-width: 768px) { … }- Bootstrap or Tailwind CSS.
Key Takeaways
- Responsive = one page that adapts to every screen.
- Start with the viewport meta tag.
max-width: 100%images,vw/clamp()text, and media queries for layout.- CSS Lectures 053–055 go deeper.