Image width and height

suggest change

Note: The width and height attributes are not deprecated on images and never have been. Their use has been made much stricter though.

The dimensions of an image can be specified using the width and height attributes of the image tag:

<img src="images/200x200-img.png" width="200" height="200" alt="A 200x200 image">

By specifying the width and height of an image, your structure gives the browser a hint as to how the page should be laid out even if you are just specifying the actual image size. If the image dimensions are not specified, the browser will need to recalculate the layout of the page after the image is loaded, which may cause the page to “jump around” while it’s loading.

In HTML 4.01 you can give the image a width and height in either CSS pixels or a percentage of the image’s actual dimensions.

These examples are all valid:

<img src="images/50x50-img.png" width="50" height="50" alt="A 50x50 image">
<img src="images/50x50-img.png" width="200" height="200" alt="A 200x200 image">
<img src="images/200x200-img.png" width="50" height="50" alt="A 50x50 image">
<img src="images/200x200-img.png" width="50%" height="50%" alt="A 100x100 image">

In HTML5 the width and height must be non-negative integers in CSS pixels; a percentage is not a valid value. An older HTML5 draft also required both attributes, when set, to match one of three aspect-ratio formulas. Current HTML does not make a mismatched ratio invalid, but you still should not use these attributes to stretch an image.

These examples are valid:

<img src="images/50x50-img.png" width="50" height="50" alt="A 50x50 image">
<img src="images/200x200-img.png" width="50" height="50" alt="A 50x50 image">
<img src="images/50x50-img.png" width="0" height="0" alt="A hidden tracking image">

This example is not recommended:

<img src="images/50x50-img.png" width="200" height="200" alt="A 200x200 image">

A percentage is invalid in HTML5:

<img src="images/200x200-img.png" width="50%" height="50%" alt="A 100x100 image">

A ratio that does not match the image is valid in current HTML, but it stretches the image and is not recommended:

<img src="images/200x200-img.png" width="1" height="200" alt="A 1x200 image">

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents