On this page11 sections
  1. Learning Objectives
  2. 1. The Viewport Tag Comes First
  3. 2. Media Queries
  4. 3. Mobile-First
  5. 4. Flexible Images
  6. 5. Text That Scales: clamp()
  7. Code Examples
  8. Common Beginner Mistakes
  9. Practice Questions
  10. Practice Answers
  11. Key Takeaways

Lecture 053: Responsive Design & Media Queries

Category9: Responsive Design
Lecture053 of 057
Estimated video length14 min
TargetCSS3 · any modern browser
LevelIntermediate

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-width and max-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):

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

css
@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:

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

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

css
img { max-width: 100%; height: auto; }

Example

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

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

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

  1. Which <meta> tag must a responsive page have?
  2. Write a media query for screens 768px wide and up.
  3. What does "mobile-first" mean?
  4. Which CSS rule stops images from overflowing their container?
  5. What does clamp(1rem, 4vw, 2rem) do?
  6. Make .grid one column by default and two columns from 600px up.

Practice Answers

  1. <meta name="viewport" content="width=device-width, initial-scale=1">
  2. @media (min-width: 768px) { … }
  3. Write the phone styles first, then add min-width media queries for larger screens.
  4. img { max-width: 100%; height: auto; }
  5. It sets a size of 4% of the window width, but never below 1rem or above 2rem.
  6. css
    .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 and clamp() for text.
  • Next: build a fluid grid and make images and videos responsive too.