{"id":292,"date":"2008-12-10T20:09:44","date_gmt":"2008-12-11T01:09:44","guid":{"rendered":"http:\/\/cssnewbie.com\/?p=292"},"modified":"2008-12-10T20:09:44","modified_gmt":"2008-12-11T01:09:44","slug":"equalheights-jquery-plugin","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/equalheights-jquery-plugin\/","title":{"rendered":"The EqualHeights jQuery Plugin"},"content":{"rendered":"<p><a href=\"http:\/\/cssnewbie.com\/example\/equal-heights\/plugin.html\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2008\/12\/eh-plugin-main.gif\" alt=\"\" title=\"equalHeights Plugin\" width=\"579\" height=\"200\" class=\"alignnone size-full wp-image-294\" \/><\/a><\/p>\n<p>After I wrote and published my last article on writing a function to <a href=\"http:\/\/cssnewbie.com\/equal-height-columns-with-jquery\/\" title=\"Equal Height Columns with jQuery\">equalize heights in jQuery,<\/a> I realized that the function would probably make a really useful jQuery plugin. This is that plugin.<\/p>\n<p>This is also my first attempt at a jQuery plugin, so I apologize in advance if I\u2019ve done something painfully stupid (and painfully obvious) to any and all plugin veterans out there. Luckily, the functionality is extremely simple.<\/p>\n<p>You can download the plugin here (right-click and save the link).<\/p>\n<p><a href=\"http:\/\/cssnewbie.com\/example\/equal-heights\/plugin.html\">You can see a demo of the plugin here.<\/a><\/p>\n<p>The plugin\u2019s functionality is so simple because it\u2019s designed to do only one thing: set all elements you specify to the same height.<!--more--><\/p>\n<p>This plugin is slightly more sophisticated than the function I wrote earlier this week. Instead of being stuck setting all your columns to the height of the tallest element, you can also (optionally) specify a minimum and maximum height you want the element to be.<\/p>\n<p>For example:<\/p>\n<pre lang=\"javascript\" escaped=\"true\" line=\"1\">$(\".columns\").equalHeights(100,300);<\/pre>\n<p>This would set all your elements to be at least 100px tall, but no more than 300px tall. If your tallest elements contain more than 300px worth of content, the containing element will still be resized, and a scroll bar will appear on the side of the element. If you only specify one number, that number will be treated like the minimum height, meaning all elements will be at least that number of pixels tall.<\/p>\n<p>You can also nestle the function inside of a chain, to keep your code minimalistic:<\/p>\n<pre lang=\"javascript\" escaped=\"true\" line=\"1\">$(\".columns\").equalHeights().css(\"color\",\"blue\");<\/pre>\n<h3>Detailed Usage<\/h3>\n<p>For those not familiar with using jQuery plugins, here\u2019s what you need to know.<\/p>\n<ol>\n<li>Load the jQuery library in your document\u2019s head. That\u2019ll look something like this:<\/li>\n<pre lang=\"html4strict\" escaped=\"true\" line=\"1\">&lt;script language=&quot;javascript&quot; type=&quot;text\/javascript&quot; src=&quot;jquery.js&quot;&gt;&lt;\/script&gt;<\/pre>\n<\/li>\n<li>Load the equalHeights plugin the same way. Be sure the plugin comes <em>after<\/em> (i.e., below) the main jQuery script&#8230; the function relies on jQuery already being loaded:\n<pre lang=\"html4strict\" escaped=\"true\" line=\"1\">&lt;script language=&quot;javascript&quot; type=&quot;text\/javascript&quot; src=&quot;jquery.js&quot;&gt;&lt;\/script&gt;\n&lt;script language=&quot;javascript&quot; type=&quot;text\/javascript&quot; src=&quot;jquery.equalheights.js&quot;&gt;&lt;\/script&gt;<\/pre>\n<\/li>\n<li>In order for the function to be able to calculate heights accurately, you\u2019ll need to make sure it waits until the page is done loading before running. So wrap the function call (just like you should most all of your jQuery functions) in a $(docment).ready() function, like so:\n<pre lang=\"javascript\" escaped=\"true\" line=\"1\">$(document).ready(function() {\n\t$(\".columns\").equalHeights(100,300);\n});<\/pre>\n<\/li>\n<\/ol>\n<p>And that\u2019s about all there is to it!<\/p>\n<h3>Known Limitations<\/h3>\n<ul>\n<li>Sometimes this script has trouble accurately calculating heights in Internet Explorer if you\u2019re trying to find the height of an element that contains images that don\u2019t have heights set. Unless the height has been specified, the script seems to assume a height of zero.<\/li>\n<li>If you resize the text in your browser window, the blocks will not resize automatically. In some browsers, you would be able to fix this by calling the script again on resize, with something like:\n<pre lang=\"javascript\" escaped=\"true\" line=\"1\">$(\".columns\").resize(function(){\n \t$(\".columns\").equalHeights();\n});<\/pre>\n<p>However, I\u2019m given to understand that this doesn\u2019t work on Firefox, as the browser doesn\u2019t report text resizes as a \u201creal\u201d resize. I&#8217;m currently not aware of a good workaround.<\/li>\n<\/ul>\n<h3>Worth Mentioning<\/h3>\n<p>I should point out that there is <a href=\"http:\/\/plugins.jquery.com\/project\/equal-height-elements\">a similar jQuery plugin<\/a> out there already called equalHeight which has similar functionality. However, that plugin requires significantly more code and the author warns that it doesn\u2019t work in Internet Explorer. I feel my contribution, which is very small and cross-browser compatible, is thus still worthwhile.<\/p>\n<p>Further, CSS Newbie reader Jared Mellentine posted a similar jQuery function in <a href=\"http:\/\/cssnewbie.com\/equal-height-columns-with-jquery\/#comment-9042\">the comments<\/a> of <a href=\"http:\/\/cssnewbie.com\/equal-height-columns-with-jquery\/\" title=\"Equal Height Columns with jQuery\">the previous Equal Heights article.<\/a> His functionality is slightly different than mine (his looks specifically for child elements, whereas mine assumes you\u2019re specifying the elements you want to resize directly), but he did give me the idea of adding additional functionality to my plugin. So thanks, Jared!<\/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>After I wrote and published my last article on writing a function to equalize heights in jQuery, I realized that the function would probably make a really useful jQuery plugin. This is that plugin. This plugin is slightly more sophisticated than the function I wrote earlier this week. [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/equalheights-jquery-plugin\/\" title=\"Click to read 'The EqualHeights jQuery Plugin'\">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":[176,184],"tags":[249,55,291,319],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/292"}],"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=292"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/292\/revisions"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}