Image width Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Image


The width property sets or gets the width attribute of an image.

Set the width property with the following Values

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

Return Value

A Number, representing the width of the image, in pixels

The following code shows how to change the width of an image to 300px:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>

<img id="myImg" src="" width="107" height="98">

<button onclick="myFunction()">Test</button>

function myFunction() {/*from   w  w  w . j  a  va  2 s .c  o  m*/
    document.getElementById("myImg").width = "300";


Related Tutorials