Latest web development tutorials
 

Image width Property

< Image Object

Example

Change the width of an image to 300px:

document.getElementById("myImg").width = "300";
Try it Yourself »

Definition and Usage

The width property sets or returns the value of the width attribute of an image.

The width attribute specifies the width of an image.

This property can also return the width of an image that has been styled with CSS (See More Examples).

Tip: Use the height property to set or return the value of the height attribute of an image.


Browser Support

Property
width Yes Yes Yes Yes Yes

Syntax

Return the width property:

imageObject.width

Set the width property:

imageObject.width=pixels

Property Values

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

Technical Details

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

More Examples

Example

Return the width of an image:

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

The result of x will be:

107
Try it Yourself »

Example

Change the height and width of an image to 300px:

document.getElementById("myImg").height="300";
document.getElementById("myImg").width="300";
Try it Yourself »

Example

Return the width of an image that has been styled with CSS:

var x = document.getElementById("myImg").width;
Try it Yourself »

Related Pages

HTML reference: HTML <img> width attribute


< Image Object