Responsive Images and object-fit
Make an image scale down with max-width: 100% and height: auto. It never grows beyond its natural size and keeps its proportions. object-fit controls how an image fills a box of a different shape: cover crops to fill, contain shows it whole, fill stretches it.
aspect-ratio reserves a shape (16 / 9, 1) before the picture loads, which prevents layout jumps. The demo uses an inline SVG as the picture.
Example
<style>
.frame { width: 100%; max-width: 320px; aspect-ratio: 16 / 9; border: 2px solid #64748b; margin-bottom: 10px; }
.frame img { width: 100%; height: 100%; display: block; }
.cover img { object-fit: cover; }
.contain img { object-fit: contain; background: #e2e8f0; }
</style>
<div class="frame cover"><img alt="Cover" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100' height='100'%3E%3Crect width='100' height='100' fill='%2314b8a6'/%3E%3Ccircle cx='50' cy='50' r='30' fill='%23fde047'/%3E%3C/svg%3E"></div>
<div class="frame contain"><img alt="Contain" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100' height='100'%3E%3Crect width='100' height='100' fill='%2314b8a6'/%3E%3Ccircle cx='50' cy='50' r='30' fill='%23fde047'/%3E%3C/svg%3E"></div>Exercise
Crop the image so it fills the box.
img { object-fit: ; }
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.