{"id":280,"date":"2008-12-07T15:28:33","date_gmt":"2008-12-07T20:28:33","guid":{"rendered":"http:\/\/cssnewbie.com\/?p=280"},"modified":"2008-12-07T15:28:33","modified_gmt":"2008-12-07T20:28:33","slug":"equal-height-columns-with-jquery","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/equal-height-columns-with-jquery\/","title":{"rendered":"Equal Height Columns with jQuery"},"content":{"rendered":"<p><a href=\"http:\/\/cssnewbie.com\/example\/equal-heights\/\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2008\/12\/equalheight-main.jpg\" alt=\"\" title=\"Equal Height Columns\" width=\"579\" height=\"200\" class=\"alignnone size-full wp-image-281\" \/><\/a><\/p>\n<p class=\"note\"><strong>Update:<\/strong> This concept has been adapted into a full-fledged jQuery plugin. <a href=\"http:\/\/cssnewbie.com\/equalheights-jquery-plugin\/\">Check it out here.<\/a> And then read on to learn how this technique is done.<\/p>\n<p>Creating equal-height columns with CSS is sometimes a bear. But who needs the hassle of faux columns, \u201cclear\u201d divs and the rest? With this bit of jQuery, you can easily equalize the heights of any group of elements.<\/p>\n<p>This technique was born of frustration. I ran into two layout problems while working on <a href=\"http:\/\/cssnewbie.com\/css-newbie-gets-a-facelift\/\" title=\"CSS Newbie Gets a Facelift\">the redesign of CSS Newbie<\/a> one afternoon, and both were directly related to the varying heights of elements.<\/p>\n<p>The first problem was in the footer. If you look closely, you\u2019ll see that there is a nice subtle two-tone border in between each of the columns in the footer.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2008\/12\/equalheight-twotone.gif\" alt=\"\" title=\"two tone css border\" width=\"579\" height=\"150\" class=\"alignnone size-full wp-image-282\" \/><br \/>\n<!--more--><\/p>\n<p>That style wasn\u2019t as easy to create as it might look. At first, I was going to make the border part of the background image, and just edit my content to suit. But that\u2019s very un-web of me: the beauty of the web is we\u2019re not constrained by sizes nearly as much as print designers. <\/p>\n<p>So instead, I decided to make the borders the old-fashioned way, as legitimate CSS borders. Of course, to get it exactly the way I wanted I had to resort to a bit of a hack, matching light borders on the right side of one column with a dark border on the left side of the following column. The hack works beautifully&#8230; as long as all three of my columns are of equal height. Otherwise, the borders don\u2019t match:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2008\/12\/equalheight-badborder.gif\" alt=\"\" title=\"mismatched two-tone border problem\" width=\"579\" height=\"150\" class=\"alignnone size-full wp-image-284\" \/><\/p>\n<p>The second problem I ran into was in <a href=\"http:\/\/cssnewbie.com\/\">the \u201crecent articles\u201d section of the homepage.<\/a> I wanted to feature four articles, but I didn\u2019t want them to take up a ton of space. To accomplish that, I made the article blocks half as wide as my space and floated them into two columns with two rows. <\/p>\n<p>And as long as I\u2019m lucky and all my excerpts are the same length, that works fine. But the instant something is a line or two longer or shorter, one of the more annoying flaws with floats rears its ugly head. If I have a long excerpt in spot 1, followed by a short excerpt in spot 2, the excerpt in spot 3 won\u2019t float all the way to the left: it\u2019ll get caught on the edge of the first excerpt instead. <\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2008\/12\/equalheight-badfloat2.gif\" alt=\"\" title=\"css float problem\" width=\"578\" height=\"150\" class=\"alignnone size-full wp-image-288\" \/><\/p>\n<p>So again, I have a height-related problem. \u201cIf only there was an easy way to equalize heights without resorting to a table,\u201d I thought thusly.<\/p>\n<p>And so I set out to find an easy way. And after a bit of fiddling with jQuery, I was able to find a workable solution. Here\u2019s the function I ended up with:<\/p>\n<pre lang=\"javascript\" escaped=\"true\" line=\"1\">function equalHeight(group) {\n\tvar tallest = 0;\n\tgroup.each(function() {\n\t\tvar thisHeight = $(this).height();\n\t\tif(thisHeight > tallest) {\n\t\t\ttallest = thisHeight;\n\t\t}\n\t});\n\tgroup.height(tallest);\n}<\/pre>\n<p><a href=\"http:\/\/cssnewbie.com\/example\/equal-heights\/\">You can see it in action here.<\/a> What this function is doing is:<\/p>\n<ol>\n<li>Sets a variable, \u201ctallest,\u201d to zero.<\/li>\n<li>Loops through each of the items in the group we&#8217;ve defined.<\/li>\n<li>If the current item is taller then the previous \u201ctallest\u201d item, it becomes the new tallest item.<\/li>\n<li>Once we\u2019ve looked at all the items, they all get their height reset to be the same as the tallest of the group.<\/li>\n<\/ol>\n<p>So in order to equalize the heights of both my footer columns and my recent articles, all I needed to do was something like this:<\/p>\n<pre lang=\"javascript\" escaped=\"true\" line=\"1\">$(document).ready(function() {\n\tequalHeight($(\".recent-article\"));\n\tequalHeight($(\".footer-col\"));\n});<\/pre>\n<p>Which just waits until all my elements are done loading (so that the heights can be computed accurately), then sets both the heights.<\/p>\n<p><a href=\"http:\/\/cssnewbie.com\/example\/equal-heights\/\">That\u2019s all there is to it!<\/a> This function is good for fixing wily floats without clears, creating nice equal-height column designs without needing faux columns, and probably half a dozen things I haven\u2019t thought of yet. I\u2019m sure you\u2019ll be able to find a use for the script someday&#8230; after all, I managed to find two uses in a single afternoon!<\/p>\n<div class=\"wp-socializer wpsr-share-icons \" data-lg-action=\"show\" data-sm-action=\"show\" data-sm-width=\"768\" ><h3>Share and Enjoy !<\/h3><div class=\"wpsr-si-inner\"><div class=\"wpsr-counter wpsrc-sz-32px\" style=\"color:#000\"><span class=\"scount\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook,twitter,linkedin,pinterest,print,pdf\">0<\/span><\/span><small class=\"stext\">Shares<\/small><\/div><div class=\"socializer sr-popup sr-32px sr-circle sr-opacity sr-pad sr-count-1 sr-count-1\"><span class=\"sr-facebook\"><a rel=\"nofollow\" href=\"https:\/\/www.facebook.com\/share.php?u=\" target=\"_blank\"  title=\"Share this on Facebook\"  style=\"color: #ffffff\" ><i class=\"fab fa-facebook-f\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-twitter\"><a rel=\"nofollow\" href=\"https:\/\/twitter.com\/intent\/tweet?text=%20-%20%20\" target=\"_blank\"  title=\"Tweet this !\"  style=\"color: #ffffff\" ><i class=\"fab fa-twitter\"><\/i><\/a><\/span>\n<span class=\"sr-linkedin\"><a rel=\"nofollow\" href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=\" target=\"_blank\"  title=\"Add this to LinkedIn\"  style=\"color: #ffffff\" ><i class=\"fab fa-linkedin-in\"><\/i><\/a><\/span>\n<span class=\"sr-pinterest\"><a rel=\"nofollow\" href=\"https:\/\/www.pinterest.com\/pin\/create\/button\/?url=&amp;media=&amp;description=\" target=\"_blank\"  title=\"Submit this to Pinterest\"  style=\"color: #ffffff\" data-pin-custom=\"true\"><i class=\"fab fa-pinterest\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"pinterest\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-print\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Print this article \"  style=\"color: #ffffff\" ><i class=\"fa fa-print\"><\/i><\/a><\/span>\n<span class=\"sr-pdf\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Convert to PDF\"  style=\"color: #ffffff\" ><i class=\"fa fa-file-pdf\"><\/i><\/a><\/span><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Creating equal-height columns with CSS is sometimes a bear. But who needs the hassle of faux columns, \u201cclear\u201d divs and the rest? With this bit of jQuery, you can easily equalize the heights of any group of elements. [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/equal-height-columns-with-jquery\/\" title=\"Click to read 'Equal Height Columns with jQuery'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[165,184],"tags":[],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/280"}],"collection":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/comments?post=280"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/280\/revisions"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}