Performance: Image sizing basics
This affects how quickly images load and how smoothly the page renders, impacting the page's loading speed and visual stability.
<img src="photo-200w.jpg" width="200" height="150" alt="Description">
<img src="large-photo.jpg" style="width: 200px; height: auto;">
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Large image resized with CSS only | Low | Multiple reflows as image loads and resizes | High due to repaint | [X] Bad |
| Image sized with width and height attributes | Low | Single layout pass | Low paint cost | [OK] Good |
width attribute is used in the <img> tag to set the image width in pixels or percentage.width is a valid HTML attribute for image sizing.width or height attributes for quick sizing [OK]size with widthscale which is not validwidth attribute expects a number representing pixels without units.<img src='photo.jpg' width='200px'> uses '200px' which is invalid for the width attribute. <img src='photo.jpg' width='200'> correctly uses '200' without units. <img src='photo.jpg' style='width: 200'> uses CSS syntax incorrectly inside an attribute. <img src='photo.jpg' height='200px'> sets height, not width.<img src='flower.jpg' width='150' height='100'>
width and height are set, the image is forced to those exact dimensions, ignoring original proportions.<img src='cat.png' width='300' height='auto'>
width attribute correctly uses the number '300' without units.<img> needs closing slash in HTML5width: 100% makes the image fill the container's width.height: auto lets the browser calculate height to keep the image's original proportions.Pass the test and this pattern moves unknown