Image width attribute

Definition and Usage

width Attribute to set or return the width of the image width property .value.

The HTML width attribute specifies the width of the image.

This property can also return the width of the image using CSS styles (see the example below).

Tip:Please use height property Set or return the height of the image height property .value.

Example

Example 1

Change the width of the image to 450px:

document.getElementById("myImg").width = "450";

Try it yourself

Example 2

Return the width of the image:

var x = document.getElementById("myImg").width;

Try it yourself

Example 3

Change the height and width of the image to 300px and 450px respectively:

document.getElementById("myImg").height = "300";
document.getElementById("myImg").width = "450";

Try it yourself

Example 4

Return the width of the image using CSS styles:

var x = document.getElementById("myImg").width;

Try it yourself

Syntax

Return the width property:

imageObject.width

Set the width property:

imageObject.width = pixels

Attribute Value

Value Description
pixels Width in pixels (for example, width="100").

Technical Details

Return Value: A number, representing the width of the image in pixels.

Browser Support

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Support Support Support Support Support

Related Pages

HTML Reference Manual:HTML <img> width Attribute