5 Ways – Equal Height Columns or Parent Div to Maximum from Children

Sometimes we wish to create equal height columns in a webpage. Or it may require setting parent div height to the maximum from its children. Equalizing the height of divs assist designers to obtain a grid/column layout. Also, the same can fix the varying height issue due to rotation of sibling elements in a parent like a slider.

Usually, the height of each slide or child differs from their siblings in a slider with varying content. In such case, the parent div height either shrinks or grows when a slide changes. Which in turn, shifts elements below the parent up or down. This reflects a poor design, reduces smooth reading as well as leaves a bad impression on visitors.

Equal Height Columns

Also check: Convert Bootstrap carousel to full screen slider.

5 Ways to Set Equal Height Columns or Maximize Parent Div

So setting the height of all matching divs to equal can solve the problem stated. Alternatively, you can set the height of the parent equal to the tallest child for the same. While writing the post, we had variable height slider in mind initially. However, the article can resolve the following similar concerns as well:

  • Equal height columns in CSS
  • Match height of two or more divs
  • Setting minimum height from tallest child
  • Expanding a parent div to the largest height of its children
  • jQuery to get the largest height of children and apply to all
  • The element with the maximum height from a set of elements

Let’s discover the potential ways that can set the height either of the parent or children. But before that, here is the HTML markup:

You can treat each inner-container class as a column for a grid layout with divs of equal height. Or for a slider, it can be a slide with content inside. All divs stay visible in the grid while in the latter case, only one slide appears at a time. Let us start equalizing the height of divs using CSS.

1. CSS Overflow Technique

Setting  overflow: auto to the parent div works for some people. While few designers claim for less known  overflow: overlay. However, the overlay value doesn’t exist outside WebKit browsers.

2. CSS table-cell for Divs of Equal Height

Another solution to set equal height is displaying direct children as table-cell. Use the markup like this coupled with CSS rules:

If the rule doesn’t work then ensure that you don’t have set any float rule except the float: none.

3. Equal Height Columns or Divs – Flexbox

Browsers nowadays are supporting CSS flexbox. Similarly, most of the CSS frontend framework are using flexbox for responsive design, like Bootstrap. Use flexbox rules given below along with the HTML markup provided in the code block at the top.

These CSS rules will create a grid with equal height columns. If all children are spreading over one line then add the following two more rules to the outer-container class.

We’ve written a nice article to fetch data from database and display in responsive columns. This ready-to-use script is in PHP and can create the grid in a minute or two.

4. jQuery Loop for Equal Height Columns

This is a traditional approach. First, we create a variable to store adequate height and loop through all matching elements or children. Then update the variable if the height of the current element is bigger than the variable value. Finally, assign this value to the parent or children height if it is a slider. Or set to all matching divs for equal height columns.

5. Get Maximum Height and Assign to Parent/Children – jQuery

Parent Maximum Children Height

With the use of jQuery map() and Math.max you can retrieve the tallest element or the largest height.

The get() jQuery function returns the tallest element. While omitting it will give you the largest height from the set of matching elements or children. Now you can use this value as minimum height or just as the height for the parent.

You can also assign this height to divs in a grid to set them as equal height columns as given in the code above. However, we recommend to use CSS ways, specially Flexbox to create divs of equal height. The jQuery functions are better to set the height of parent div same as the tallest child.

So which way did you find suitable, CSS or jQuery to equalize the height? Or do you have an opinion? Also, you might be using some other method, would you let us know then in comments?

You Might Interested In


Leave a Reply

Enclose a code block like: <pre><code>Your Code Snippet</code></pre>.