On this page11 sections
  1. Learning Objectives
  2. 1. The Viewport Tag
  3. 2. Images That Fit
  4. 3. Different Images for Different Screens
  5. 4. Text That Scales
  6. 5. Media Queries: Change the Layout
  7. 6. Frameworks
  8. Common Beginner Mistakes
  9. Practice Questions
  10. Practice Answers
  11. Key Takeaways

Lecture 024: Responsive Web Pages

Category4: Page Structure
Lecture024 of 048
Estimated video length12 min
TargetHTML5 · any modern browser
LevelBeginner

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

html
<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:

html
<img src="https://picsum.photos/id/1043/900/400" alt="Autumn forest" style="max-width: 100%; height: auto;">
RuleEffect
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:

html
<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

html
<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):

html
<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

  1. Write the viewport meta tag.
  2. Which is usually better for images, width: 100% or max-width: 100%? Why?
  3. What does 5vw mean?
  4. What does a media query do?
  5. Write a media query for screens 768px wide and up.
  6. Name one responsive CSS framework.

Practice Answers

  1. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  2. max-width: 100%. It shrinks images but never stretches them beyond their real size.
  3. 5% of the viewport (browser window) width.
  4. It applies CSS only when a condition, such as a minimum screen width, is true.
  5. @media (min-width: 768px) { … }
  6. 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.