Posted By

o0110o on 12/06/13


Tagged

javascript style columns


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

Beppoi
o0110o


Rock-Solid Equal Height Columns


 / Published in: JavaScript
 

URL: http://jsfiddle.net/LmbmR/13/

Dynamic equal height columns that account for padding and borders.

  1. //Dynamic Equal Height Columns *Pure JS
  2. function eqHeight(parent_id) {
  3. var child = document.getElementById(parent_id).childNodes, childAmount = child.length, boxHeight = 0;
  4. // Find the greatest height
  5. for(var i = childAmount - 1; i >= 0; i--) {
  6. if(child[i].offsetHeight && child[i].offsetHeight > boxHeight) {
  7. child[i].style.height = '';
  8. boxHeight = child[i].offsetHeight;
  9. }
  10. }
  11. // Apply the greatest height to all child elements while accounting for padding and borders
  12. for(var i = childAmount - 1; i >= 0; i--) {
  13. if(child[i].offsetHeight) {
  14. child[i].style.height = boxHeight + 'px';
  15. }
  16. if(child[i].offsetHeight > boxHeight) {
  17. child[i].style.height = boxHeight - (child[i].offsetHeight - child[i].clientHeight) + 'px';
  18. child[i].style.height = boxHeight - ((child[i].offsetHeight - boxHeight) + (child[i].offsetHeight - child[i].clientHeight)) + 'px';
  19. }
  20. }
  21. }
  22. window.onload = function() { eqHeight('object_group'); }
  23. window.onresize = function() { eqHeight('object_group'); }

Report this snippet  

You need to login to post a comment.