How To Change Image Size with JavaScript

In this tutorial we will learn How to Change Image Size with JavaScript. For this we can use JavaScript style property with which we can change both height and width of the image.

style property

style property returns the CSSStyleDeclaration object which contains all CSS inline properties of an HTML element.

In this example it is used to change the size of img tag.

HTML Code

HTML Code is given below, in this code we have img tag and a button tag with onclick event which is used to execute a JavaScript function.

<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript Change image Size</title>
<style>
.responsive
{
width: 100%;    
}
</style>    
</head>             
<body>
    
<img src="car.jpg" id="image" class="responsive">
<button onclick='changeImageSize()'>Change Image Size</button>    
    
<script src="script.js"></script>    
</body>
</html>

JavaScript Code

Take a look at the JavaScript code, in this code getElementById() method is used to select or target the image.

In the end style property changes the size of image.

<script>
function changeImageSize()
{
    var image = document.getElementById('image');
    image.style.height ='100px';
    image.style.width = '200px;'
}
</script>

We can also change only height or width of the image.

<script>
function changeImageSize()
{
    var image = document.getElementById('image');
    image.style.width = '200px;'
}
</script>

Demo

Video Tutorial

Watch video tutorial on How to change Image size with JavaScript.

Change font-size using JavaScript Get Font Size of any Tag using JavaScript Change Position of HTML Element using JavaScript How to Change Image on Hover with JavaScript How to Disable Button by Class Name in JavaScript How To Change Image Size with JavaScript How to change Image opacity with JavaScript How to Change image src on click with JavaScript How to get the lang attribute value in JavaScript How to Get attribute Value using JavaScript How To Check if Attribute Exists or not with JavaScript How To Count number of links on Page with JavaScript How To Scroll Page to Bottom using JavaScript How To Detect Shift Key Press with JavaScript Change Text Color On Hover with JavaScript Hide and Show div using JavaScript Get Button text with JavaScript Get textarea value with JavaScript Get table row Height with JavaScript Auto Increase width of input field with JavaScript Set Textarea maxLength with JavaScript Set Textarea Value with JavaScript JavaScript Count list items JavaScript set input field value Count Button Clicks with JavaScript Scroll Page to the Top on Click with JavaScript