November 25, 2008

How to make columns of equal height in Blogger

An issue of blog design is that CSS based layouts cannot produce columns of equal heights. For example, when designing a two or three column template, it is almost impossible to make the sidebar(s) stretch to the length of the main posts column. This is particularly noticeable when a background color (or image) is used for the sidebar which does not stretch to the length of the page.



This issue is also relevant for those who display their blog posts side-by-side on the home page. Posts which are of equal height would appear in a harmonious grid-like format which is more pleasant to the eyes, whereas posts of unequal length disrupt the grid-like pattern and can often cause disarray in the layout.

In this tutorial, I'll explain how to use JavaScript to ensure columns of your choosing (whether the sidebars and main column, or individual posts) appear at equal heights to ensure a pleasant and harmonious layout for your blog design.


The "Matching Columns Script" and how it works


In this tutorial, we will be using an adaptation of the "Matching Columns" JavaScript by Alejandro Gervasio. which was modified to work in FireFox by Stefan Mischook for Killersites.com. You can read the original article and even view a video explaining how this script works over at the Killersites blog.

This JavaScript matches the height of all columns which use a specific CSS "class". It finds the length of the longest columns which uses this class name, then creates extra CSS declarations to ensure all other columns using this class are lengthened to the same height.

For example, if we use the class of "column" for both the main posts section and sidebars in our blog template, the script will ensure that the main column and both sidebars appear of equal length when viewed in a web browser.

The original Matching Columns JavaScripts use external files which are linked to in the head section of the web-page. However, I have adapted this method for use in Blogger templates so we can use "inline" JavaScript, meaning we do not need to host a JavaScript file on an external host.


How to add the Matching Columns function to your Blogger layout


The first step to creating columns of matching height in your Blogger layout is to add the required JavaScript code to the head section of your template.

This is very simple! All you need to do is copy the following section of code and paste this just before the closing <head> tag in your Blogger template (it is quite long so be sure to copy the entire section of code to your clipboard!):


&lt;!-- Equal Columns JavaScript Start --&gt;


&lt;script type='text/javascript'&gt;
/*
Enables columns to be of the same height for better blog designs.
Derived from a script by Alejandro Gervasio.
Modified to work in FireFox by Stefan Mischook for Killersites.com
Adapted for inline use with Blogger blogs by Amanda Fazani of BloggerBuster.com


How it works: just apply the CSS class of &#39;column&#39; to your pages&#39; main columns.
*/
function matchColumns(classname){


     var divs,contDivs,maxHeight,divHeight,d;
   
     // get all &lt;div&gt; elements in the document


     divs=document.getElementsByTagName(&#39;div&#39;);


     contDivs=[];


     // initialize maximum height value


     maxHeight=0;


     // iterate over all &lt;div&gt; elements in the document


     for(var i=0;i&lt;divs.length;i++){


          // make collection with &lt;div&gt; elements with class attribute &#39;container&#39;


          if(new RegExp(&quot;\\b&quot; + classname + &quot;\\b&quot;).test(divs[i].className)){


                d=divs[i];


                contDivs[contDivs.length]=d;


                // determine height for &lt;div&gt; element


                if(d.offsetHeight){


                     divHeight=d.offsetHeight;                    


                }


                else if(d.style.pixelHeight){


                     divHeight=d.style.pixelHeight;                   


                }


                // calculate maximum height


                maxHeight=Math.max(maxHeight,divHeight);


          }


     }


     // assign maximum height value to all of container &lt;div&gt; elements


     for(var i=0;i&lt;contDivs.length;i++){


          contDivs[i].style.height=maxHeight + &quot;px&quot;;


     }


}


// Runs the script when page loads


window.onload=function(){


     if(document.getElementsByTagName){


          matchColumns(&#39;crosscol&#39;); // class=column       
          matchColumns(&#39;column&#39;); // class=maincolumn   


     }


}
&lt;/script&gt;


&lt;!-- Equal Columns JavaScript End --&gt;

Once you have pasted this section of code in your template, proceed to save it. If for some reason you have made any errors when pasting the code, you will receive an error message and be unable to save.

Now to make sections of your Blogger layout have equal heights, we need to apply the class of "column" to those sections in the actual template code. I will offer two variations of how this can be achieved in this tutorial: making the sidebar and main-wrapper equal height, and making all posts equal height (for use when making posts appear side-by-side).


Make the sidebar(s) and main posts column of equal length


This method works best for layouts including at least one colored sidebar (whether this feature a background color or background images for effect). There are many different non-standard templates using this style of layout, so it's likely that the identifiers of the divisions referenced here could be different in your own template.


To make the sidebar(s) and main column of equal length, you will need to add the class of "column" to these divisions in the template.

For the main-wrapper (the section which holds your blog posts) you should look for a line like this in your template:


<div id='main-wrapper'>

Add the code highlighted in red to this line:



<div id='main-wrapper' class='column'>

If you cannot find <div id='main-wrapper'> in your template, this may be called 'main-wrap' or 'main-section' instead. Use your discretion to find the appropriate code tag; you can always change things later!

Next, you need to add the class of column to your sidebar or sidebars. If your template features more than one sidebar, this could become a little complicated though I will do my best to explain!

Most Blogger templates identify the sidebar-wrapper like this:


<div id='sidebar-wrapper'>

If you find this in your template code, simply add the section in red:


<div id='sidebar-wrapper' class='column'>

The main sidebar could also be called 'sidebar-wrap', 'left-sidebar' or even 'left-bar' depending on the template you are working with. Again, use your discretion and add class='column' in the place you think most appropriate for your template.

This also applies in templates where you have a second sidebar. It may be named <div id='sidebar-right'> ; 'right-sidebar-wrapper', 'new-sidebar-wrapper' or something entirely different. Simply add class='column' inside the division tag which you think references the correct section in your template.

Now to check if your modifications have been successful, attempt to preview your template. Since the JavaScript we are using is contained within the template (not referenced from an external host) you should be able to see the difference straight away. This means that your blog sidebar should appear as long as your main posts column.

If all looks well, you can proceed to save your template and enjoy your new matching columns. If your sidebar and main column do not appear at equal heights, clear your edits and begin again being sure to check the placement of the class='column' sections you have added.


To make posts of equal height


This method is particularly useful when using my recently posted customization to display posts side-by-side on non-item pages. Using this method ensures your posts appear in a grid-like fashion with spaces beneath shorter posts so all headings are correctly aligned.

For this example, you will need to ensure you have checked the "Expand widget templates" box on the Edit HTML page, as we will be adding the class of "column" inside the main posts widget.

Firstly, ensure you have added the JavaScript before the closing </head> tag in your Blogger template.

Next, search for the following line of code (or similar):


<div class='post'>

If you cannot find this line, search for the following instead:


<div class='post hentry'>

Or any other division tag which begins with <div class='post

You do not need to add the entire class='column' phrase here as this div already has class attributes. Instead, we simply need to add the class identifier, like this:


<div class='post column'>

or alternatively,


<div class='post hentry column'>  

This is because divisions can have more than one "class" (though they can only have one "id"!).

Once you have added this extra class to the posts section, preview your template to see that your blog posts are now all of equal height.

Important information when making posts of equal height

If you choose to make your posts of equal height, you must consider that all of your posts will appear as long as your longest post. So if you have one particularly long post on your home-page when all others are relatively short, there will be long gaps beneath all of your shortened posts!

Other uses for the matching columns script


You can also adapt this script and methods to add columns of equal height to your blog footer (in conjunction with my three column footer hack, perhaps!), or to any other areas of your blog template which you would like to be of matching heights.

Simply apply the class of "column" to all divisions which you would like to appear at the same height, and ensure you have pasted the JavaScript before the closing </head> tag in your Blogger template!

Your thoughts?


I hope you have found this tutorial to be a useful addition to the arsenal of Blogger customizations and tutorials posted here on Blogger Buster! Please feel free to leave your comments or let us know how you have used the matching columns script in your own designs by typing your message below.

Save as PDF

View blog reactions


I'm reading: How to make columns of equal height in BloggerTweet this!


Related Posts by Categories



Post a Comment 70 Comments:

26 November 2008 02:43 Sharing post said...

I will try..... thanks

26 November 2008 03:15 proAMATEUR said...

Permission for translate on this article to Vietnamese to upload on my blog is http://phattrienso.tk

26 November 2008 11:24 Daniel said...

Dear Amanda,
I have pasted the script , but failed to get the rest as they were giving the usual parse xml errors, I wonder if you could help further

kind regards

is there any platform I could send you my template please

26 November 2008 11:53 Anthony Harman said...

Your tutorial really help a newbie like me
Thanks

26 November 2008 13:16 Syamsul Alam said...

WOW! It just too good! Amanda still the best blogger hacker ever..... xixixixixi......

26 November 2008 15:19 Pocket said...

Finally! Thank you....I've tried to do this for some time now...

Smile!

27 November 2008 02:15 Drajat Ahmad G said...

good and good idea amanda :)

27 November 2008 04:24 Ir. hydir said...

thank you Amanda! I'll try it ... btw..I'm using your template :)

28 November 2008 08:09 Michigan Website Design said...

Thanks for sharing the code...

28 November 2008 14:09 thinchhia said...

i followed your instructions still my footer widgets are of not equal heights..please take a look at my blog

28 November 2008 14:13 thinchhia said...

by the way is it possible to add twitter updates on the Navbar/ Menu bar?

29 November 2008 04:29 Is said...

Amanda, is the script cross browser compatible?

30 November 2008 06:33 kyu said...

great article, i'm very interesting in it

30 November 2008 16:59 AMH615 said...

It let me paste the script with no problems, but when I add the class='columns' it gives me the 'could not be parsed, not well-formed..."div" must be followed by either attribute specifications, ">" or "/>"' error.

Any suggestions? I tried putting it in a few different spots besides where you suggested, but it just didn't like it.

My blog is My-Junk-Drawer.com. I went ahead and left the script hoping you'll know where I should put the class or a closing div.

Thanks!

30 November 2008 17:25 AMH615 said...

I figured it out!! I couldn't add class='column' after div id='main-wrapper', instead I had to add it before id='main-wrapper'.

So it looks like this on my site:

div class='column' id='main-wrapper' and
div class='column' id='sidebar-wrapper'

Don't know why that made the difference, but it did. Hope this helps someone else!

Thanks so much for the instructions... my site looks much better now!

01 December 2008 15:36 asm said...

tq, how to make background themes in blog???

02 December 2008 02:02 .:sanDORA:. said...

Amanda..
if i have a variety of sidebar..
first i have a widebar..
then this two sidebar..
then i add another widebar below..

can i used this code to remain same height my two sidebar in the middle??

02 December 2008 12:40 Blogger-Holic said...

hi amanda..
i think we can make it equal using CSS code
clear:both
but i'm not sure about it.. maybe there's a way to using it w/o script but i dnt know how..

mabe u can explain us :)

02 December 2008 13:26 thinchhia said...

If you guys want your widgets(sidebar,footer etc.) be equal in heights.simply add 'column' at every 'div class=widget-content'.

03 December 2008 05:50 anoj said...

appreciate your work.. really very usuful for blogger community.

I have used jQuery to achieve the same.You just need to add two lines of javascript. Find details below:
http://www.thetechhub.com/2008/12/blogger-trick-make-sidebar-column.html

04 December 2008 15:14 Ruth said...

Maybe my template is already tweaked too much, because I tried this and everything went haywire. Ah well...at least I tried. :(

04 December 2008 16:28 Anonymous said...

Someone has copied your post exactly .. must report to blogger

http://blogvietshow.blogspot.com/2008/11/how-to-make-columns-of-equal-height-in.html

04 December 2008 22:57 Kent said...

Is there anyway to moddify this to work with Spiffy Corners? Or Vice Versa?

I inserted a border to see if this code was working, and it is, but the spiffy only goes to the bottom of the widget, not the page, any help?

By the way, this blog is freakin' amazing. Thanks for all the work.

05 December 2008 21:47 العاب said...

Very nice post! I like it very much.

06 December 2008 07:02 Jane said...

Hi Amanda. I followed your instructions carefully but i still get error codes everytime i preview my blog. I'm using a modified 3-column rounder template. Pls help me. Thanks.

07 December 2008 12:56 zhixin said...

Hello Amanda, I had a same problem with Jane. Using 3 column template.

07 December 2008 20:23 Helmi Razali said...

hi amanda, i'm having the same problem too. My pseudoblog address is http://altartemplate.blogspot.com/
can you help? thanks

08 December 2008 20:03 ISpySteph said...

great! it works, but unfortunaltly with my blog layout it kinda backfires because my 'header' is on the left. have a look..
www.ispysteph.blogspot.com

13 December 2008 02:04 Chung Nguyen said...

This worked beautifully. Thank you very much for posting this.

You're certifiably awesome! (;

13 December 2008 05:20 STACI said...

This worked great on my test blog when I view it in Firefox, but it doesn't seem to work when viewed on IE 8 Beta. Do you know a fix so that it will work with IE 8?

14 December 2008 01:57 Erik Grobelny said...

Well...this works great in firefox, but not in internet explorer. Any chance of a script that works in both browsers?

14 December 2008 10:19 Gini said...

Thank you! I implemented this on my blog and it works like a dream.

17 December 2008 18:32 ألعاب said...

thanks for this . So thanks again and in the spirit of community - I’m following you up!

31 December 2008 05:46 Jason Murray Wong said...

Hi Amanda, I really need ur help this one. I tried for a couple of times on everything u mentioned. And I can't get any changes on my blog template. Appreciate ur aid.

31 December 2008 05:47 Jason Murray Wong said...

Hi Amanda, I really need your help. I've tried a couple of times on everything you mentioned here and to no avail. Appreciate if you can aid me in this one.

01 January 2009 03:35 abhi said...

hi it works great in firefox but doesnt work with IE, any suggestions

01 January 2009 03:43 abhi said...

hi Amanda
i am using 3 column rounders template. the java script works ok with firefox, but not IE.
You got any suggestions to make it work on IE.

08 January 2009 03:30 Cypher said...

Um... when I save it, it says that div has to be followed by a specific attribute > or />. Could you help me with that?

08 January 2009 22:20 DirtDigger (Tessa) said...

Has anyone found a fix for IE? I'm not sure I want to do this if it doesn't work in IE, as I'm sure some people that read my blog use IE

14 January 2009 02:12 maiaT said...

I implemented it on two column blogs, it doesn't work in IE but you can play with the background (colors) so it doesn't look bad either. Have a look on my blogs.
Does anyone know how to implement this together with the three column footer?
I implemented both on the same site but the footer columns fall down with the same length as the difference between the two others.
Any suggestions?

18 January 2009 09:45 sharenow said...

Yes, mine only works for FF as the article has claimed. Even though it is not cross-browser, it still gives me some limited usage. Hope there is an improvement for all brower. Thank Amanda.

24 January 2009 13:36 TheLabRat said...

Having a heck of a time getting this to work here:

http://diagnosisrodentia.blogspot.com/

I have done it no problem on a simpler template but no combinations of main-wrapper and side-wrapper (I have two kinds of the latter) is making this work for me. Suggestions?

04 February 2009 04:22 Radith Prawira said...

How to add the Matching Columns function to your Blogger layout
..This is very simple! All you need to do is copy the following section of code and paste this just before the closing [head> tag in your Blogger template ..

To make posts of equal height
..Firstly, ensure you have added the JavaScript before the closing [/head> tag in your Blogger template...

i don't really get it, where should i paste the js script? before the [head> or [/head>, or there's no difference?

04 February 2009 04:39 Radith Prawira said...

about my previous comment, i think it's has no difference wether you put the js script before the [head> or [/head>, i put it before the [head> and it still worked fine with me.. tq so much Amanda! ^^

05 February 2009 03:21 Salman said...

Thank you Amanda,
Its just work nicely in my blog...keep up a good work. I really appreciate it. Now all my sibling happy with the blog.

TQ again.

16 February 2009 16:13 +@roCk said...

vry hard cript
my head blank lol

08 March 2009 17:08 Anonymous said...

Doesn't work in IE or in FF. It looks *almost* right in FF, but not quite.

21 March 2009 13:45 fitnesswannabe said...

Thank you, thank you, thank you...one for each of my equal-height columns :)

22 March 2009 04:35 peenkfrik said...

Thank you so much. 8-)

03 April 2009 17:35 aldy said...

thanks alot...nice work.

09 April 2009 00:03 babyrocasmama said...

I did the first part with the code and that worked fine, but when I try to do the "class=column", I get this message:

,Your template could not be parsed as it is not well-formed. Please make sure all XML elements are closed properly.
XML error message: Element type "div" must be followed by either attribute specifications, ">" or "/>".


And I am using your Super Custom Template, but I do not have anything that tell me what sidebars are which. This is what is in my HTML:


"div id='main-wrapper' div id='main-wrapper2' div id='main-wrapper3'"


And:

"div id='main-sidebar-wrapper'

div class='snap_noshots' id='sidebar-wrapper'"

AND:

"div class='snap_noshots' id='sidebar-wrapper2'
b:section class='sidebar2' id='sidebar-right' preferred='yes'"


I do not know where to post the code. Please help!!!

10 April 2009 17:31 Ms Unreliable said...

No matter how many times I run through this, every time I get my two sidebars matching each other but extending well beyond my main body. I can't find any setting that seems to be doing this either...any hints?

13 April 2009 18:59 Anonymous said...

worked like a charm! . . . thanks soooo much!

21 April 2009 20:14 Shantharam Shenoy K said...

Dear Amanda,
This worked for me well except it shows only 1 post now when i am having 3 posts to be dispalyed..Does the sript does this or is this an error??
my site is http://oneunitedmanunited.blogspot.com

21 April 2009 20:17 Shantharam Shenoy K said...

just checked am not able to even see the comments. will study the code and then add it...

05 May 2009 20:08 Jill said...

Hi Amanda, I'm trying to use the equal-height columns in my content/sidebar and the three-column footer. Blogger wants to make the footer columns the same height as the content columns, which as you can guess looks pretty silly. See here: http://jillietest.blogspot.com/

Any ideas for a workaround? I was thinking of creating a class called "column2" or "footercolumn". Would that work?

06 May 2009 11:06 ERS said...

hi, amanda!

the code worked out for me. thanks a lot. but is it possible to code it in a way that the columns will follow whichever column is the longest? i only have 1 post in my main page and it's shorter than my sidebars so the sidebars get cut...

just wondering if that's possible... thanks a lot again! hope to hear from you soon.

09 May 2009 15:00 @LiYaNNuaR said...

can't do it.. plz help me!

09 May 2009 15:31 @LiYaNNuaR said...

thanks... lastly... i can do it!

10 May 2009 23:18 Nabeel said...

Thank you so much. I applied it and it works fine.
http://nabeelzeeshan.blogspot.com

12 May 2009 08:08 thesikaleon said...

Amanda first of all THANKS for your help!!!
You are the best.

I used Spiffy Corners following your instructions here http://www.bloggerbuster.com/2008/03/how-to-add-css-rounded-corners-to-your.html
But unfortunately the script here has no result for equal column height.

I can see the result for equal column height just in case I don't use Spiffy Corners.

Any way to make them work together?
this is my blog http://greekschat.net

thanks again and sorry for my pooor English

21 May 2009 06:30 search engine placement said...

These tips are wonderful. You just gave me a reason to stick with blogger. ^%^

28 May 2009 02:30 Ben said...

Hey Amanda,

I have my posts side by side, but I'd like to cut down on the white space - is there a way I can make post appear end-to-end, instead of lined up together.

If you visit my site www.benspictureoftheday.com you'll see what I'm getting at! The first two are great, but I'm trying to get the rest bunched up regardless of their size.

Cheers

Ben

28 May 2009 18:16 Jennifer Williams "Blueskysunburn" said...

I'm having the same issue with IE. Is there a solution?

31 May 2009 01:58 cmongood said...

My blog only shows 2 posts when it should be displaying ten!!!

17 June 2009 07:39 technary said...

@Amanda
Thank you for the tip regarding making columns of equal height. I have successfully used it in my blogs after making some changes that my template (Sand Dollar) required. Noting them here for the larger audience.
(Note: These changes might not apply to atemplate that you use)

1> In my case adding class attribute (class='column') to 'main-wrapper' and 'sidebar-wrapper' did not work.
2> I had to add the class attribute (class='column') to the 'b:section' tags inside 'main-wrapper' and 'sidebar-wrapper' respectively.
3> Since there were already existing class attributes for those, I had to append the value "column" to them. The two section tags looked like below:
<b:section class='sidebar column' id='sidebar' preferred='yes'>
<b:section class='main column' id='main' showaddelement='no'>

After this change, everything worked perfectly.

19 June 2009 15:26 Anonymous said...

Thanks heaps for that tip on matching the column heights. Worked like a charm !

25 June 2009 09:13 Ronboe said...

I think this is a great tutorial. Trying to learn this as a springboard to my personal website. Thank You.

Post a Comment