jQuery: Setting the division height

The example below will loop through a set of div elements and set the height for them to the height of the tallest element.  In this case, 200px.

I have also used setTimeOut function so that you could see the before and after effects of implementing the div height changes.

<!DOCTYPE html>
<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
        <title>Set Division Height</title>      
        <style>
            #header {
                background-color:black;
                color:white;
                text-align:center;
                padding:5px;
            }
            #section {
                background-color:lightgrey;
                width:350px;
                height:200px;
                float:left;
                padding:10px;	 	 
            }
        </style>
        <script>
            setTimeout(function () {
                var $div = $('.division');
                var height = 0;
                $div.each(function () {
                    if ($(this).height() > height) {
                        height = $(this).height();
                    }
                });
                $div.height(height);
            },3000)
        </script>
    </head>
    <body>
        <div id="header" class="division">
            <h1>City Gallery</h1>
        </div>
        <div id="section" class="division">
            <h1>London</h1>
            <p>London is the capital city of England.</p>
            <p>Standing on the River Thames, London has been a major settlement for two millennia.</p>
        </div>
    </body>
</html>

button

Leave a comment

Leave a Reply