In this article, we will explore how to dynamically set the height of a DIV element using jQuery.

Sometimes, you may need to adjust the height of a DIV based on various factors, such as screen size or content changes. jQuery offers a simple and effective way to accomplish this.

Adjusting Height with .height() method #

In this method, we will use jQuery's .height() method to change the height of a DIV element dynamically.

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<div id="myDiv" style="background-color: lightblue;"></div>

<script>
   $(document).ready(function() {
      $("#myDiv").height(200);
   });
</script>

Explanation of the above example

  1. We include the jQuery library.
  2. Create a DIV element with the id myDiv.
  3. In the jQuery script, we use the .height() method to set the height of myDiv to 300 pixels.