Javascript DOM HTML Image width Property set

Introduction

Change the width of an image to 150px:

Click the button to change the width of the image to 150px.

View in separate window

<!DOCTYPE html>
<html>
<body>

<img id="myImg" src="image1.png" width="107" height="98">
<button onclick="myFunction()">Test</button>

<script>
function myFunction() {//from ww w .  ja v  a  2s .  co  m
  document.getElementById("myImg").width = "150";
}
</script>

</body>
</html>

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

The width attribute specifies the width of an image.

This property can return the width of an image that has been styled with CSS.

The width property accepts and returns a number type value.

Value Description
pixels The width in pixels (e.g. width="100")
Return Value: A Number, representing the width of the image, in pixels



PreviousNext

Related