How to set width and height of an element using jQuery?

jQueryWeb DevelopmentFront End Technology

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:

Live Demo

<!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:

Live Demo

<!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>
raja
Published on 17-Dec-2017 13:24:54
Advertisements