On this page11 sections
Lecture 054: Responsive Grid View, Images & Videos
| Category | 9: Responsive Design |
| Lecture | 054 of 057 |
| Estimated video length | 12 min |
| Target | CSS3 · any modern browser |
| Level | Intermediate |
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
srcsetandsizes - 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:
<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>| Breakpoint | Typical devices |
|---|---|
| under 480px | Phones |
| 480–767px | Large phones, small tablets |
| 768–1023px | Tablets |
| 1024px and up | Laptops and desktops |
Choose breakpoints where your layout starts to look wrong, rather than for particular devices.
2. Flexible Images
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
<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
.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:
<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>withwidth: 100%; height: autoscales naturally.- An
<iframe>has no natural shape, so give itaspect-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
- Write a grid that's 1 column on phones and 3 columns from 700px.
- Which two properties make an image flexible?
- What does the
wmean in800w? - What does
sizestell the browser? - Keep an iframe at 16:9 at any width.
- Why do phones benefit from
srcset?
Practice Answers
.grid { display: grid; } @media (min-width: 700px) { .grid { grid-template-columns: repeat(3, 1fr); } }max-width: 100%;andheight: auto;- The image file is 800 pixels wide.
- How wide the image will be displayed at different screen sizes.
iframe { width: 100%; aspect-ratio: 16 / 9; }- 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 / 9keeps videos and embeds in shape.- Frameworks and templates are shortcuts built on these same ideas.