How to animate div width and height on mouse hover using jQuery ? Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report In order to animate the div width and height on mouse hover, we can use the jQuery animate() method along with the mouseenter() and mouseleave() methods. .animate() method: The animate() method changes the state of the element with CSS style. Syntax: $(selector).animate({styles}, para1, para2, para3); .mouseenter() method: The mouseenter() method works when mouse pointer moves over the selected element. Syntax: $(selector).mouseenter(function) .mouseleave() method: The mouseleave() method works when the mouse pointer leaves the selected element. Syntax: $(selector).mouseleave(function) Approach: Store the actual width and height of the div element on which animation is to be done using $(selector).width() method. When the mouse pointer event is handle the .mouseenter() and .mouseleave() methods. When mouse pointer is over the div element, change the width or height style property to new value of div element using .animate() method. Change the width or height style property of div element to previously stored values. Example 1: Animating the div width on hover using jQuery. html <!DOCTYPE html> <html> <head> <title> How to animate div width and height on mouse hover in jQuery ? </title> <script src= "https://code.jquery.com/jquery-1.12.4.min.js"> </script> <style type="text/css"> .box { float:center; overflow: hidden; background: #32a852; width: 400px; padding: 0px; } /* Add padding and border to inner content for better animation effect */ .box-inner { width: 400px; padding: 0px; border: 1px solid #000000; } </style> </head> <body> <center> <h1 style = "color:green;" > GeeksForGeeks </h1> <h3> jQuery | How to animate div width on mouse hover? </h3> <hr> <div class="box"> <div class="box-inner"> <h4>.animate() method is used</h4> <p> GEEKSFORGEEKS - A computer science portal for geeks. </p> </div> </div> <hr> <script type="text/javascript"> $(document).ready(function() { var divWidth = $(".box").width(); $(".box").mouseenter(function(){ $(this).animate({ width: "300" }); }).mouseleave(function(){ $(this).animate({ width: divWidth }); }); }); </script> </center> </body> </html> Output: When pointer is on the div element: When pointer is not on the div element: Example 2: Animating the div height on Hover using jQuery. html <!DOCTYPE html> <html> <head> <title> jQuery | How to animate div width and height on mouse hover? </title> <script src= "https://code.jquery.com/jquery-1.12.4.min.js"> </script> <style type="text/css"> .box{ float:center; overflow: hidden; background: #32a852; width: 400px; padding: 0px; } /* Add padding and border to inner content for better animation effect */ .box-inner{ width: 400px; padding: 0px; border: 1px solid #000000; } </style> </head> <body> <center> <h1 style = "color:green;" > GeeksForGeeks </h1> <h3> jQuery | How to animate div height on mouse hover? </h3> <hr> <div class="box"> <div class="box-inner"> <h4>.animate() method is used</h4> <p> GEEKSFORGEEKS - A computer science portal for geeks. </p> </div> </div><hr> <script type="text/javascript"> $(document).ready(function(){ var divHeight = $(".box").height(); $(".box").mouseenter(function(){ $(this).animate({ height: "250" }); }).mouseleave(function(){ $(this).animate({ height: divHeight }); }); }); </script> </center> </body> </html> Output: When pointer is on the div element: When pointer is not on the div element: Comment More infoAdvertise with us Next Article How to dynamically set the height and width of a div element using jQuery ? S SHUBHAMSINGH10 Follow Improve Article Tags : JQuery Similar Reads How to play video on hover on a div using jQuery? Given a video on the website and the task is to play it while hovering the cursor on the video and stop the video play on removing the cursor from the video div using jQuery events.Example: Suppose you are in a hurry and you don't have time to view the whole video of the article, then you can just h 2 min read How to dynamically set the height and width of a div element using jQuery ? Set the height of a div element using jQueryThe content height of a div can be dynamically set or changed using height(), innerHeight(), and outerHeight() methods depending upon the user requirement. If the user wants to change the content height of a div dynamically, it includes changing the actual 7 min read How to create a pop-up div on mouse over and stay when click using jQuery ? In this article, we will learn how to create a pop-up div on mouseover and stay when click using jQuery. Approach: First, we create an HTML div element that we want to pop up when we mouse over on an element and set its display property to none in CSS style.display:none;In the script tag, we create 2 min read How to get the width and height of an element in jQuery ? In this article, we will find the width and height of an element in jQuery. There are three ways available in jQuery that we can use to find the height and width of an element. Table of Content Using the width() and height() methodsUsing the innerWidth() and innerHeight() methodsUsing the outerWidth 3 min read How to animates div's left property with a relative value using jQuery ? In this article, we will learn how to animate a division element's left property with a relative value using jQuery. This can be used in situations where a division element has to be animated using only one property. Approach: We will be using the jQuery click() and animate() methods. The division e 2 min read How to run a code on hover event in jQuery ? In this article, we will see how to change the style of elements on hover event using jQuery. To change the style of hover event, hover() method is used. The hover() method is used to specify two functions to start when the mouse pointer moves over the selected element. Syntax: $(selector).hover(Fun 1 min read Like