Dofactory.com
Dofactory.com

HTML <img> width Attribute

The width attribute on an <img> tag sets the width of the image.

The width is specified in pixels -- without the ‘px‘ unit.

Example

#

A width attribute on an <img> element.
The width attribute overrides the original image width.

Poppies, by Van Gogh
<img src="/img/html/poppies.jpg" width="315" 
     alt="Poppies, by Van Gogh" />

Using width

The width attribute specifies the width of the image element.

The width is defined in pixels, without the 'px' unit.

To define the width in % or other unit, use the CSS width property.

The browser will maintain the correct aspect ratio of the image if the height attribute is not set.

Note:  Image width can also be defined with CSS. If both the width attribute and CSS width property are specified, CSS takes precedence (wins).

Tip:  The browser must perform an image resize if the specified width is different from the original image. To speed up the page, it is best to specify the real image size.


Syntax

<img width="pixels" />

Values

#

Value Description
pixels A number (e.g. 200).

Browser support

Here is when width support started for each browser:

Chrome
1.0 Sep 2008
Firefox
1.0 Sep 2002
IE/Edge
1.0 Aug 1995
Opera
1.0 Jan 2006
Safari
1.0 Jan 2003

You may also like

 Back to <img>

Author: Jack Poorte
Published: Jun 20 2021
Last Reviewed: Sep 30 2023


Quick question: what's your favorite/least favorite part of Dofactory?


Guides