HTML <img> widthAttribute

HTML img 标签参考手册HTML <img> Tag

Examples

An image with a height and width of 42 pixels:

<img src="smiley.gif.html" alt="Smiley face" height="42" width="42">

Try it Yourself »

Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the width attribute.


The width attribute specifies the width of an image, in pixels.

Tip:It is a good practice to specify the height and width attributes for an image. If these attributes are set, space can be reserved for the image when the page loads. Without these attributes, the browser cannot know the dimensions of the image and cannot reserve appropriate space for it, so the page layout will change when the image loads.

Tip:Do not use the height and width attributes to scale images. If you reduce an image using the height and width attributes, the user must download a large image (even if the image looks small on the page). The correct approach is to process the image to the appropriate size with software before using it on a web page.


Differences Between HTML 4.01 and HTML5

In HTML 4.01, the width should be defined in pixels or as a percentage of the containing element. In HTML5, the width value must be in pixels.


Syntax

<img width="pixels">

Attribute Values

Value Description
pixels The width in pixels (e.g., width="100")


HTML img 标签参考手册HTML <img> Tag Other Extensions