Change size of an image with JavaScript/jQuery
This post will discuss how to change the size of an image in JavaScript and jQuery.
1. Using JavaScript
In plain JavaScript, you can directly modify the CSS width and height property of the image.
The following example demonstrates this by altering the CSS width property by specifying the absolute value in pixels and leave the browser to calculate the corresponding height while maintaining the aspect ratio.
JS
|
1 2 3 4 5 |
document.querySelector('button').onclick = function() { var image = document.getElementById('container'); image.style.width = '156px'; image.style.height = 'auto'; } |
HTML
|
1 2 |
<img id="container" src='https://avatars0.githubusercontent.com/u/70142' /> <button>Resize</button> |
You can either provide an absolute value or value in percentage. The following example changes the size of an image relative to its original size.
JS
|
1 2 3 4 5 |
document.querySelector('button').onclick = function() { var image = document.getElementById('container'); image.style.width = '50%'; image.style.height = 'auto'; } |
HTML
|
1 2 |
<img id="container" src='https://avatars0.githubusercontent.com/u/70142' /> <button>Resize</button> |
Alternatively, you can directly modify the actual height and width attributes of the image.
JS
|
1 2 3 4 5 |
document.querySelector('button').onclick = function() { var image = document.getElementById('container'); image.height = 156; image.width = 156; } |
HTML
|
1 2 |
<img id="container" src='https://avatars0.githubusercontent.com/u/70142' /> <button>Resize</button> |
2. Using jQuery
With jQuery, you can use the .css() method to set CSS width and height property.
jQuery
|
1 2 3 4 5 6 7 |
$(document).ready(function() { $('button').click(function() { $('#container') .css('width', '156px') .css('height', 'auto'); }); }); |
HTML
|
1 2 |
<img id="container" src='https://avatars0.githubusercontent.com/u/70142' /> <button>Resize</button> |
The .css() method can also take a single object of key-value pairs. So, the code can be shortened to:
|
1 2 3 4 5 |
$(document).ready(function() { $('button').click(function() { $('#container').css({'width': '156px', 'height': 'auto'}); }); }); |
Another plausible solution is to specify the width and height in a CSS class and apply that class to the image element. This can be easily done using the jQuery’s .addClass() method. To overwrite an existing class, don’t forget to include !important declarations.
jQuery
|
1 2 3 4 5 |
$(document).ready(function() { $('button').click(function() { $('#container').addClass('small'); }); }); |
CSS
|
1 2 3 4 5 6 7 8 9 10 |
#container { width: 200px; height: 200px; border: 1px solid; } .small { width: 156px !important; height: auto !important; } |
HTML
|
1 2 |
<img id="container" src='https://avatars0.githubusercontent.com/u/70142' /> <button>Resize</button> |
That’s all about changing the size of an image in JavaScript and jQuery.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)