Responsive images with CSS max-width and height
Use max-width: 100% to keep an image within its parent’s content width. Add height: auto so that when the width shrinks, the height follows the original aspect ratio.
Prepare a small test image
Save the following markup as sample.svg in UTF-8. The image is 640×360, with a 16:9 ratio. This test SVG uses preserveAspectRatio="none" so the yellow circle visibly stretches when its display box changes proportions.
<svg xmlns="http://www.w3.org/2000/svg" width="640" height="360"
viewBox="0 0 640 360" preserveAspectRatio="none">
<rect width="640" height="360" fill="#dceefa" />
<circle cx="490" cy="90" r="50" fill="#f6bd4f" />
<path d="M0 360 L210 120 L400 360 Z" fill="#168b85" />
<path d="M260 360 L450 180 L640 360 Z" fill="#215d8a" />
</svg>
Apply responsive styles in HTML
Save the HTML as responsive-image.html and open it from the same folder as sample.svg. If the image is missing, check the file name and extension. Some editors append .txt, which would break the relative path shown here.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Keep the image in proportion</title>
<style>
body { margin: 20px; font: 16px/1.6 system-ui, sans-serif; color: #203348; }
* { box-sizing: border-box; }
h1 { font-size: 24px; }
.frame { width: 100%; max-width: 900px; padding: 12px;
border: 2px solid #6687a5; }
.responsive { display: block; max-width: 100%; height: auto; }
</style>
</head>
<body>
<h1>Keep the image in proportion</h1>
<div class="frame">
<img class="responsive" src="sample.svg" width="640" height="360"
alt="A yellow circle and green and blue triangles on a light blue background">
</div>
</body>
</html>
If the parent’s content width is at least 640px, the image is 640×360px. At a content width of 320px, it becomes 320×180px. Inspect the img element to see its rendered dimensions. Compare against the parent’s content box, since its outer width also includes padding and borders.
The HTML attributes width="640" and height="360" match the file’s ratio. This lets the browser reserve space before the file loads. CSS height: auto still controls the displayed height. See WHATWG HTML · img and CSS 2.2 · Minimum and maximum widths.
Compare capping the width with filling it
With a wide window, add width: 100% to .responsive. At a parent content width of 800px, the image grows to 800×450px. Remove that declaration to cap the display at the original size. Enlarging a small raster photo may make it look soft.
Start from the original rule and temporarily remove only height: auto. With the width constrained to 320px, the height attribute still supplies 360px and the test circle stretches vertically. Restore the declaration afterwards. display: block removes the baseline gap under an inline image and serves a separate purpose.
Troubleshoot distortion and horizontal overflow
If an image stretches, inspect its computed height. If a fixed inline declaration such as style="height: 360px" wins, remove it at the source or change it to auto. For a fixed-ratio card thumbnail that should fill a box and accept cropping, size the box and consider object-fit: cover separately.
If the image shrinks but the page still overflows, check the image’s min-width, fixed ancestor widths, padding, and borders. CSS changes the displayed size while the download remains the same. To offer smaller downloads, provide smaller image candidates and let the browser pick with srcset and sizes. See WHATWG HTML · Adaptive images for how candidates relate to display width.




