Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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() methods in jQuery. These methods allow you to dynamically modify element dimensions with pixel values or other CSS units.
Setting Width of an Element
The You can try to run the following code to learn how to set the width of an element in jQuery ? The You can try to run the following code to learn how to set the height of an element in jQuery ? jQuery's width() and height() methods provide an easy way to dynamically modify element dimensions, making them essential for responsive web design and interactive user interfaces.width()
Example
<!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>
Setting Height of an Element
height() method works similarly to width(), accepting numeric values to modify element height dynamically.Example
<!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>
Conclusion
