Categories
JavaScript Answers

How to change image size with JavaScript?

Spread the love

Sometimes, we want to change image size with JavaScript

In this article, we’ll look at how to change image size with JavaScript.

How to change image size with JavaScript?

To change image size with JavaScript, we set its width and height properties.

For instance, we write

<img src="https://picsum.photos/200/300" id="yourImgId" />

to add an img element.

Then we write

const image = document.getElementById("yourImgId");
if (image?.style) {
  image.style.height = "100px";
  image.style.width = "200px";
}

to select the img element with getElementById.

Then we set its style.width and style.height properties to set its width and height respectively.

Conclusion

To change image size with JavaScript, we set its width and height properties.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *