[jquery] Setting equal heights for div's with jQuery

Answer to your specific question

Your code was checking all columns within any container, what you need to do is:

  • Loop through each container
    • Get the heights of each column within that container
    • Find the highest one
    • Apply that height to every column in that container before moving on to the next one.

Note: Try to provide an example jsfiddle of your issue, it enables us to more easily help you and understand the issue, you get to see the working solution straight away and it encourages quicker responses.

Quick (Rough) Example

_x000D_
_x000D_
$(document).ready(function(){_x000D_
_x000D_
    // Select and loop the container element of the elements you want to equalise_x000D_
    $('.container').each(function(){  _x000D_
      _x000D_
      // Cache the highest_x000D_
      var highestBox = 0;_x000D_
      _x000D_
      // Select and loop the elements you want to equalise_x000D_
      $('.column', this).each(function(){_x000D_
        _x000D_
        // If this box is higher than the cached highest then store it_x000D_
        if($(this).height() > highestBox) {_x000D_
          highestBox = $(this).height(); _x000D_
        }_x000D_
      _x000D_
      });  _x000D_
            _x000D_
      // Set the height of all those children to whichever was highest _x000D_
      $('.column',this).height(highestBox);_x000D_
                    _x000D_
    }); _x000D_
_x000D_
});
_x000D_
.container { border 1px solid red; }_x000D_
.column { border: 1px solid blue; float:left; width: 30%; text-align:center; }
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>_x000D_
_x000D_
<div class="container">_x000D_
    <div class="column">This is<br />the highest<br />column</div>_x000D_
    <div class="column">One line</div>_x000D_
    <div class="column">Two<br />lines</div>_x000D_
</div>_x000D_
<div class="container">_x000D_
    <div class="column">One line</div>_x000D_
    <div class="column">Two<br>lines</div>_x000D_
    <div class="column">One line</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_


Library!

Here's a library i found that looks promising if you want some more clever equalisation controls

http://brm.io/jquery-match-height-demo/



Addressing @Mem's question

Question: Will $(document).ready() work if you have images that need to load which in turn modify the heights of the containers?


Equalising heights after image load

If you are loading images, you will find that this solution doesn't always work. This is because document.ready is fired at the earliest possible time, which means it does not wait for external resources to be loaded in (such as images).

When your images finally load, they may distort the heights of their containers after the equalisation script has run, causing it too appear not too work.


Solving equalising heights with images

  1. Bind to image loading events

This is the best solution (at the time of writing) and involves binding to the img.load event. All you have to do is get a count of how many img's there are $('img').length and then for each load, -1 from that count until you hit zero, then fire the equaliser.

The caveat here is load may not fire in all browsers if the image is in the cache. Look around google/github, there should be a solution script out there. At the time of writing i found waitForImages from Alexander Dickson (untested, this is not an endorsement).

  1. Another more reliable solution is the window.load event. This should generally always work. However, if you check the docs out, you'll notice this event fires after ALL external resources are loaded. This means if your images are loaded but there's some javascript waiting to download it won't fire until that completes.

If you load the majority of your externals asyncronously and are clever with minimising, compressing and spreading the load you probably wont have any issues with this method, however a suddenly slow CDN (happens all the time) could cause issues.

In both cases, you could take a dumb approach and apply fallback timers. Have the equalise script run automatically after a set few seconds just in case the event doesn't fire or something is slow out of your control. It's not fool proof, but in reality you'll satisfy 99% of your visitors with this fallback if you tune the timer correctly.



Years later this is still getting upvotes, with flexbox widely supported these days you may want to consider just plain CSS for this, unless there's a specific reason you are using JS for it

_x000D_
_x000D_
.container {_x000D_
  display: flex;_x000D_
  border: 2px dashed red;_x000D_
  margin: 10px 0;_x000D_
}_x000D_
_x000D_
.container > .column { _x000D_
 padding: 10px;_x000D_
}_x000D_
_x000D_
.container.fill-width {_x000D_
  justify-content: stretch;_x000D_
}_x000D_
_x000D_
.container.fill-width>.column {_x000D_
  flex-grow: 1_x000D_
}_x000D_
_x000D_
.container>.column:nth-child(1) {_x000D_
  background: yellow;_x000D_
}_x000D_
_x000D_
.container>.column:nth-child(2) {_x000D_
  background: blue;_x000D_
}_x000D_
_x000D_
.container>.column:nth-child(3) {_x000D_
  background: green;_x000D_
}
_x000D_
<div class="container">_x000D_
  <div class="column">Some<br>text</div>_x000D_
  <div class="column">Some<br>more<br>text<br>here</div>_x000D_
  <div class="column">One line</div>_x000D_
</div>_x000D_
<div class="container">_x000D_
  <div class="column">Some<br>more<br>even<br>longer<br>text<br>here</div>_x000D_
  <div class="column">Some<br>text</div>_x000D_
  <div class="column">One line</div>_x000D_
</div>_x000D_
<div class="container fill-width">_x000D_
  <div class="column">Some<br>more<br>text<br>here</div>_x000D_
  <div class="column">Some<br>text</div>_x000D_
  <div class="column">One line</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to height

How to Determine the Screen Height and Width in Flutter How do I change UIView Size? Adjust UILabel height to text iFrame Height Auto (CSS) CSS height 100% percent not working What is the height of Navigation Bar in iOS 7? Relative div height How to fill 100% of remaining height? CSS div 100% height Change UITableView height dynamically

Examples related to equals

this in equals method Why do we have to override the equals() method in Java? Compare two objects with .equals() and == operator Check if bash variable equals 0 Setting equal heights for div's with jQuery Java, how to compare Strings with String Arrays How can I express that two values are not equal to eachother? How to override equals method in Java How do you say not equal to in Ruby? Getting an element from a Set