├── jquery.equalheightcolumns.min.js ├── README.md ├── jquery.equalheightcolumns.js └── index.html /jquery.equalheightcolumns.min.js: -------------------------------------------------------------------------------- 1 | /* equalHeightColumns.js 1.2, https://github.com/PaulSpr/jQuery-Equal-Height-Columns */ 2 | (function(e){e.fn.equalHeightColumns=function(t){defaults={minWidth:-1,maxWidth:99999,setHeightOn:"min-height",defaultVal:0,equalizeRows:false,checkHeight:"height"};var n=e(this);t=e.extend({},defaults,t);var r=function(){var r=e(window).width();var i=Array();if(t.minWidthr){var s=0;var o=0;var u=0;n.css(t.setHeightOn,t.defaultVal);n.each(function(){if(t.equalizeRows){var n=e(this).position().top;if(n!=u){if(i.length>0){e(i).css(t.setHeightOn,o);o=0;i=[]}u=e(this).position().top}i.push(this)}s=e(this)[t.checkHeight]();if(s>o){o=s}});if(!t.equalizeRows){n.css(t.setHeightOn,o)}else{e(i).css(t.setHeightOn,o)}}else{n.css(t.setHeightOn,t.defaultVal)}};r();e(window).resize(r);n.find("img").load(r);if(typeof t.afterLoading!=="undefined"){n.find(t.afterLoading).load(r)}if(typeof t.afterTimeout!=="undefined"){setTimeout(function(){r();if(typeof t.afterLoading!=="undefined"){n.find(t.afterLoading).load(r)}},t.afterTimeout)}}})(jQuery) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | jQuery-Equal-Height-Columns 2 | =========================== 3 | 4 | A simple jQuery plugin that makes the matched elements of equal height, based on the tallest element. Very useful if you have multiple horizontally aligned elements with a background that need to be of the same height. Every elements height is usually very unpredictable, especially if the page is responsive. Fixed heights are not the answer. 5 | 6 | Usage of this plugin is simple. You just match the elements that need to be of the same height, and the plugin does the rest. If you make a responsive website that starts as a single column layout but changes into a multi column one from a certain viewport width, you can use the minWidth option to be sure that only than the plugin is applied. 7 | 8 | 9 | Example call: 10 | 11 | $('.blocks li').equalHeightColumns({ 12 | minWidth: 500, 13 | maxWidth: 1000 14 | }); 15 | 16 | 17 | Example HTML: 18 | 19 | 38 | 39 | 40 | Example CSS: 41 | 42 | .blocks{ 43 | list-style: none; 44 | width: 50%; 45 | margin: 0px auto; 46 | overflow: hidden; 47 | margin-bottom: 20px; 48 | } 49 | .blocks li{ 50 | background: #eee; 51 | margin: 0 1%; 52 | padding: 0 2%; 53 | width: 27.33333%; 54 | float: left; 55 | } 56 | 57 | 58 | New 1.1 options 59 | --------------- 60 | 61 | afterLoading: 'iframe' 62 | 63 | 'afterLoading' attaches a load event to the selector in 'afterLoading' and fires the resize code. 64 | 65 | afterTimeout: 500 66 | 67 | 'afterTimeout' takes time in miliseconds and fires the resize code after that timeout. 68 | 69 | If you set both 'afterTimeout' and 'afterLoading' the 'afterLoading' code will also be fired after the timeout. This way you can make sure that dynamically added content (like social media related iframes) are present in the DOM. 70 | 71 | New 1.2 options 72 | --------------- 73 | 74 | equalizeRows: false 75 | 76 | Set this option to 'true' to equalize every row individually. This way every colum is set to the tallest column in the row which can result in every row having a different height. 77 | 78 | checkHeight: 'height' 79 | 80 | This option determines what method to use to determine a columns height. If you use box-sizing: border-box, it might be better to set this option to innerHeight. This might become the default in a future version since it seems to work well in most cases. -------------------------------------------------------------------------------- /jquery.equalheightcolumns.js: -------------------------------------------------------------------------------- 1 | /*global jQuery */ 2 | /*! 3 | * equalHeightColumns.js 1.2 4 | * https://github.com/PaulSpr/jQuery-Equal-Height-Columns 5 | * 6 | * Copyright 2014, Paul Sprangers http://paulsprangers.com 7 | * Released under the WTFPL license 8 | * http://www.wtfpl.net 9 | * 10 | * Date: Sat Dec 13 11:30:00 2014 +0100 11 | */ 12 | 13 | (function( $ ){ 14 | 15 | $.fn.equalHeightColumns = function( options ) { 16 | 17 | defaults = { 18 | minWidth: -1, // Won't resize unless window is wider than this value 19 | maxWidth: 99999, // Won't resize unless window is narrower than this value 20 | setHeightOn: 'min-height', // The CSS attribute on which the equal height is set. Usually height or min-height 21 | defaultVal: 0, // Default value (for resetting columns before calculation of the maximum height) for the CSS attribute defined via setHeightOn, e.g. 'auto' for 'height' or 0 for 'minHeight' 22 | equalizeRows: false, // Give every column in indiviual rows even height. Every row can have a different height this way 23 | checkHeight: 'height' // Which height to check, using box-sizing: border-box, innerHeight is probably more appropriate 24 | }; 25 | 26 | var $this = $(this); // store the object 27 | options = $.extend( {}, defaults, options ); // merge options 28 | 29 | // Resize height of the columns 30 | var resizeHeight = function () { 31 | 32 | // Get window width 33 | var windowWidth = $(window).width(); 34 | var currentElements = Array(); 35 | 36 | // Check to see if the current browser width falls within the set minWidth and maxWidth 37 | if( options.minWidth < windowWidth && options.maxWidth > windowWidth ){ 38 | var height = 0; 39 | var highest = 0; 40 | var yPos = 0; 41 | 42 | // Reset heights 43 | $this.css( options.setHeightOn, options.defaultVal ); 44 | 45 | // Figure out the highest element 46 | $this.each( function(){ 47 | 48 | if( options.equalizeRows ){ 49 | // Check if y position of the element is bigger, if so, it's on another row. 50 | // Make sure that the height is only set relative to elements in the same row. 51 | var elYPos = $(this).position().top; 52 | 53 | if( elYPos != yPos ){ 54 | // new row, so set the height of the elements of the previous row 55 | if( currentElements.length > 0 ) { 56 | $(currentElements).css(options.setHeightOn, highest); 57 | // clear the array and reset values for the new row 58 | highest = 0; 59 | currentElements = []; 60 | } 61 | // get element elYPos again since it might have changed because of the resize 62 | yPos = $(this).position().top; 63 | 64 | } 65 | 66 | currentElements.push(this); 67 | } 68 | 69 | // do the height check and if it's the highest, set it as such 70 | height = $(this)[options.checkHeight](); 71 | 72 | if( height > highest ){ 73 | highest = height; 74 | } 75 | 76 | } ); 77 | 78 | if( !options.equalizeRows ){ 79 | // Set that height on the elements at once 80 | $this.css( options.setHeightOn, highest ); 81 | } 82 | else{ 83 | // set height on elements in last row 84 | $(currentElements).css( options.setHeightOn, highest ); 85 | } 86 | 87 | } 88 | else{ 89 | // Add check so this doesn't have to happen everytime 90 | $this.css(options.setHeightOn, options.defaultVal); 91 | } 92 | }; 93 | 94 | // Call once to set initially 95 | resizeHeight(); 96 | 97 | // Call on resize. Opera debounces their resize by default. 98 | $(window).resize(resizeHeight); 99 | 100 | // Also check if any images are present and recalculate when they load 101 | // there might be an optimization opportunity here 102 | $this.find('img').load( resizeHeight ); 103 | 104 | // If afterLoading is defined, add a load event to the selector 105 | if ( typeof options.afterLoading !== 'undefined' ) { 106 | $this.find(options.afterLoading).load( resizeHeight ); 107 | } 108 | 109 | // If afterTimeout is defined use it a the timeout value 110 | if ( typeof options.afterTimeout !== 'undefined' ) { 111 | setTimeout(function(){ 112 | resizeHeight(); 113 | 114 | // check afterLoading again, to make sure that dynamically added nodes are present 115 | if ( typeof options.afterLoading !== 'undefined' ) { 116 | $this.find(options.afterLoading).load( resizeHeight ); 117 | } 118 | }, options.afterTimeout); 119 | } 120 | 121 | }; 122 | 123 | })( jQuery ); 124 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 14 | 15 | EqualHeightColumns test 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 81 | 82 | 83 | 84 | 85 | 115 | 116 | 117 | 118 | 150 | 151 | 152 | 153 | 185 | 186 | 187 | 188 | 217 | 218 | 219 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 279 | 280 | 281 | 282 | 283 | --------------------------------------------------------------------------------