On this page11 sections
  1. Learning Objectives
  2. 1. A Grid View With Breakpoints
  3. 2. Flexible Images
  4. 3. The Right File for the Screen: srcset and sizes
  5. 4. Background Images
  6. 5. Responsive Videos and Embeds
  7. 6. Frameworks and Templates
  8. Common Beginner Mistakes
  9. Practice Questions
  10. Practice Answers
  11. Key Takeaways

Lecture 054: Responsive Grid View, Images & Videos

Category9: Responsive Design
Lecture054 of 057
Estimated video length12 min
TargetCSS3 · any modern browser
LevelIntermediate

A responsive page needs three more pieces: a grid that changes its columns at each screen size, images that resize and load the right file, and videos that keep their shape. This lecture puts them together.


Learning Objectives

  • Build a responsive grid with breakpoints for phone, tablet and desktop
  • Make images flexible, and serve the right size with srcset and sizes
  • Keep videos and embeds in proportion with aspect-ratio
  • Know what frameworks and templates offer

1. A Grid View With Breakpoints

Example: drag the preview corner from narrow to wide:

html
<style>
  .grid { display: grid; gap: 10px; grid-template-columns: 1fr; }                     /* phones */
  @media (min-width: 480px) { .grid { grid-template-columns: repeat(2, 1fr); } }      /* tablets */
  @media (min-width: 768px) { .grid { grid-template-columns: repeat(4, 1fr); } }      /* desktops */
  .grid div { background: #d0ebff; padding: 20px; border-radius: 8px; text-align: center; }
</style>

<div class="grid">
  <div>1</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div><div>7</div><div>8</div>
</div>
BreakpointTypical devices
under 480pxPhones
480–767pxLarge phones, small tablets
768–1023pxTablets
1024px and upLaptops and desktops

Choose breakpoints where your layout starts to look wrong, rather than for particular devices.


2. Flexible Images

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

max-width: 100% shrinks images to fit; height: auto keeps their shape.


3. The Right File for the Screen: srcset and sizes

A phone shouldn't download a 2000px photo. srcset lists files and their widths; sizes says how wide the image will be shown. The browser then picks the smallest file that looks sharp.

Example

html
<img
  src="https://picsum.photos/id/1018/800/500"
  srcset="https://picsum.photos/id/1018/400/250 400w,
          https://picsum.photos/id/1018/800/500 800w,
          https://picsum.photos/id/1018/1600/1000 1600w"
  sizes="(min-width: 768px) 50vw, 100vw"
  alt="Mountain lake"
  style="max-width: 100%; height: auto;">

sizes reads: "on screens 768px and wider the image is half the viewport; otherwise full width."

For a different crop on phones, use <picture> (HTML Lecture 012).


4. Background Images

css
.hero { background: url("hero-small.jpg") center / cover; }
@media (min-width: 768px) { .hero { background-image: url("hero-large.jpg"); } }

5. Responsive Videos and Embeds

aspect-ratio keeps the right shape at any width.

Example: drag the corner:

html
<style>
  .video { width: 100%; aspect-ratio: 16 / 9; border: 0; border-radius: 10px; background: #000; }
</style>

<iframe class="video" src="https://www.youtube-nocookie.com/embed/NrO0CJCbYLA" title="Introduction to Dart" allowfullscreen></iframe>

<video controls style="width: 100%; height: auto; margin-top: 10px; border-radius: 10px;">
  <source src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4" type="video/mp4">
</video>
  • <video> with width: 100%; height: auto scales naturally.
  • An <iframe> has no natural shape, so give it aspect-ratio: 16 / 9.

6. Frameworks and Templates

Bootstrap, Tailwind CSS and others ship ready-made responsive grids and components (Lecture 057). Free templates give you a whole responsive page to start from. They save time, but now that you know the techniques underneath, you can adapt them, or do without them.


Common Beginner Mistakes

1. One huge image for every screen. Use srcset so phones download a small file.

2. Fixed-height iframes. They stretch or letterbox. Use aspect-ratio.

3. Breakpoints for specific phones. Devices change every year; set breakpoints where your layout needs them.


Practice Questions

  1. Write a grid that's 1 column on phones and 3 columns from 700px.
  2. Which two properties make an image flexible?
  3. What does the w mean in 800w?
  4. What does sizes tell the browser?
  5. Keep an iframe at 16:9 at any width.
  6. Why do phones benefit from srcset?

Practice Answers

  1. .grid { display: grid; } @media (min-width: 700px) { .grid { grid-template-columns: repeat(3, 1fr); } }
  2. max-width: 100%; and height: auto;
  3. The image file is 800 pixels wide.
  4. How wide the image will be displayed at different screen sizes.
  5. iframe { width: 100%; aspect-ratio: 16 / 9; }
  6. They download a smaller file, which saves data and loads faster.

Key Takeaways

  • Mobile-first grids add columns at each breakpoint.
  • max-width: 100%; height: auto + srcset/sizes = flexible, efficient images.
  • aspect-ratio: 16 / 9 keeps videos and embeds in shape.
  • Frameworks and templates are shortcuts built on these same ideas.