Equal height of all the sibling element in a row

Posted By : Vinay Tiwari | 30-May-2017

In this blog there was a similar problem in every row, all div's are unequal height. To eliminate this problem, I have used custom jquery function. With help of jquery, we can find the maximum height of each row and give all the div's are fixed height. So that every single row gets equal height.

This blog explains us how to give equal height to the elements present at the same level of DOM tree and having a same parent.

 

    

 

Now we will define the code structure

 

(HTML)


<div class="provider-main-page">
    <div class="panel panel-default">
        <div class="panel-body"></div>
    </div>
    <div class="panel panel-default">
        <div class="panel-body"></div>
    </div>
    <div class="panel panel-default">
        <div class="panel-body"></div>
    </div>
</div>
<div class="provider-main-page">
    <div class="panel panel-default">
        <div class="panel-body"></div>
    </div>
    <div class="panel panel-default">
        <div class="panel-body"></div>
    </div>
    <div class="panel panel-default">
        <div class="panel-body"></div>
    </div>
</div>
<div class="provider-main-page">
    <div class="panel panel-default">
        <div class="panel-body"></div>
    </div>
    <div class="panel panel-default">
        <div class="panel-body"></div>
    </div>
    <div class="panel panel-default">
        <div class="panel-body"></div>
    </div>
</div>

 

 

 

(JQUERY)

function makeProviderRowEqual(){
    $(".provider-main-page").each(function(){
        $(this).find(".panel-body").height('auto');
        var c = [];
        for(var i=0; i < $(this).find(".panel-body").length; i++){
            var height = $(this).find(".panel-body").eq(i).outerHeight();
            c.push(height)
        }
        $(this).find(".panel-body").outerHeight( Math.max.apply(Math,c));
    });
}

 

 

Declaration

 

In above code eachfunction Iterate over a jQuery object, executing a function for each matched element. The .each() method is designed to make DOM looping constructs concise and less error-prone. When called it iterates over the DOM elements that are part of the jQuery object. 

 

This function is executed on each and every row and find the maximum height of div(column in that row) and push the maximum height for each row in an array. Here, outerHeight function is used which give maximum height. It's inbuilt function in jquery. Get the current computed outer height (including padding, border, and optionally margin) for the first element in the set of matched elements or set the outer height of every matched element.

 

Math.max.apply(Math,c) function is used to find the maximum height of a portion(row-column).

 

 

Thanks

 

About Author

Author Image
Vinay Tiwari

Vinay is a bright UI developer, having knowledge of HTML, CSS, Bootstrap, Jquery and AngularJs. His hobbies are interacting with people, listening music etc.

Request for Proposal

Name is required

Comment is required

Sending message..