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.