- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to set width and height of an element using jQuery?
To set the width and height of an element using jQuery, use the width() and height() in jQuery.
Width of an element
Example
You can try to run the following code to learn how to set the width of an element in jQuery:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("#button1").click(function(){ $("div").width(300); }); $("#button2").click(function(){ $("div").width(400); }); }); </script> </head> <body> <div style="height:150px;width:200px;border:2px solid gray;background-color:lightblue;"></div><br> <button id="button1">Width of div: 300</button> <button id="button2">Width of div: 400</button><br> </body> </html>
Height of an element
Example
You can try to run the following code to learn how to set the height of an element in jQuery:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("#button1").click(function(){ $("div").height(250); }); $("#button2").click(function(){ $("div").height(350); }); }); </script> </head> <body> <div style="height:150px;width:400px;border:2px solid gray;background-color:lightblue;"></div><br> <button id="button1">Height of div: 250</button> <button id="button2">Height of div: 350</button><br> </body> </html>
- Related Articles
- How to get the width of an element using jQuery?
- How to get the height of an element using jQuery?
- Set min-width and max-width of an element using CSS
- How to set HTML content of an element using jQuery?
- Set min-height and max-height of an element using CSS
- How to animate div width and height on mouse hover using jQuery?
- Set the height and width of an image using percent with CSS
- Change the width and height of element using CSS
- How to get the width of a div element using jQuery?
- How to set the width of an element in JavaScript?
- How to get the height of a div element using jQuery?
- How to set viewport height and width in CSS
- How to set cell width and height in HTML?
- How to set the maximum width of an element with JavaScript?
- How to set the minimum width of an element with JavaScript?

Advertisements