Dynamic Columns

I get a lot of emails asking how I output the comments in a column format. Here’s how.

Now the title is probably a little misleading. We're not really creating dynamic columns in the sense of something like this. I guess a more accurate title may be something like 'dynamic float clears', but that just didn't have the same ring to it.

The Problem

For those not sure what the issue is, let me explain. When you output information in floated columns (like my comments, for example), it's hard to clear each row when the content is dynamic and you don't know how many items you're going to have.

We need a way of making every fourth (of third, or sixth or whatever) item drop to a new row.

Making Columns

Now the css involved with making things float as columns is really pretty simple. Just specify a width, and float and you're good to go.

For example, here's a really simple 6-column layout. Now if you know you're only going to have 6 items, then there's really no problem. But if that number is dynamic (like the number of comments on a blog post), when we add some more items, each with varying amounts of text, things start to fall apart as you can see in our second example (I've included red outlines so you can see what's going on).

Simple solution

Well I've built it up to be something amazing, but it's really a very basic solution using PHP. So if our example was an output from WordPress, we declare the variable before the 'loop'.

<?php $postcounter = 0; ?>

And inside the loop (at the end) as the items are outputted, we add the following (disregard the comments):

<!-- LOOP BEGIN -->
<div class="column">Dynamic content</div>
<?php 
   $postcounter++;
   if ($postcounter % 6 == 0) {;
      echo '<div class="clearfix"></div>';
     }; 
?>
<!-- LOOP END -->

Basically the postcounter variable is keeping count of how many items are being outputted (like the 'count' variable does in Expression Engine). Then it's outputting a clearing div if the item is divisible by 6 (because we're using six columns in this eg). So on the 6th, 12th, 18th etc... items it will drop in a clearing div to start a new row.

Now our third and final example shows how things should have been. The rows will work regardless of the number and length of our dynamic items. There may be slicker ways of pulling this off, but this is how I'm achieving it on this site.

UPDATE 22/08/09
Expression Engine users can use the method below to achieve the same effect in a more elegant way. Thanks to Nick Kutateli's comment for the heads up.

<!-- LOOP BEGIN -->
<li class="{switch="newrow| | | "}">
Comment
</li>
<!-- LOOP END -->

And your CSS would be:

.newrow { clear:both; } 

Did this article tickle your fancy? If so, you can subscribe to receive new posts.

Comment

Nobody likes a lurker. Come join in the discussion.

Ignorance Avoidance   |   Flat Shadows

Back to Journal home

36 Comments

  1. Thank you Rob, I’ve been curious myself as to how you had accomplished this.

    1 StephenReply
    May 26th, 2009 at 12:48 AM
  2. That’s a fancy little trick, you’ve got there. I will try to get it to work in ExpressionEngine someday.

    You can also try to add a class “clearfix” to the div with the class “column”, instead of creating an extra div with no content in it. But that might cause an extra bunch of php mess inside the html. Oh well, it’s a derty job anyway.

    2 Thijs VisserReply
    May 26th, 2009 at 12:53 AM
  3. The simplest solutions are always the best. Thanks for sharing.

    3 isaninjaReply
    May 27th, 2009 at 05:08 PM
  4. Very cool. I think I’m going to to try this on my next layout (but not for comments… your uniqueness will remain).

    4 Ian P. HinesReply
    May 28th, 2009 at 02:20 AM
  5. I love your site!  Half the stuff about the software doo-hickeys is above me, but your site is dynamic and funky and very interesing!  You rock!

    5 StephanieReply
    May 28th, 2009 at 08:20 AM
  6. Really like the way you do things around here, keep it up :-)

    6 FernandaReply
    May 29th, 2009 at 10:11 PM
  7. Rob et al,

    If you’re using Expressionengine a simpler solution would be to add a class of “first” to the first “cell” of each row that is cleared to the left. Then in your EE template just add class=”{switch=“first| | | “}” to the “cell” element and voila. Same results using Expressionengine’s built in functionality with no hacking involved. Hope this helps.

    Cheers,
    Nick

    7 Nick KutateliReply
    June 2nd, 2009 at 04:38 AM
  8. While it may be simple, I don’t think it’s something I would’ve thought of if I was trying to accomplish this, thanks.

    8 PaulReply
    June 16th, 2009 at 07:23 AM
  9. just found a minor bug.  If you click “leave a Reply” the arrow whit which you can close the reply form doesn’t get changed.

    9 chrisReply
    June 23rd, 2009 at 10:32 PM
  10. Hey, like the columns, but it seems you’re wasting a lot of space under each comment. How would you push them up, together like a jigsaw? That would be cool. Anyone know how??

    10 MaxReply
    July 1st, 2009 at 10:26 PM
  11. So it’s done like that ? Great! I was sure it would be more complicated, but then again what do I know ? I’m just learning the ropes of PHP, and your blog has been a tremendous help. Thank you!

    11 Zegarki na rekeReply
    July 7th, 2009 at 06:56 PM
  12. This is an awesome tutorial! Comments like that are way easier to read. If you only were able to implement “tree comments” it’d be even better.

    12 Noclegi Gory StoloweReply
    July 20th, 2009 at 08:31 PM
  13. @Stephen:  Cool idea. I’m trying a nested comment to see how it looks

    13 CorbachoReply
    July 23rd, 2009 at 02:17 AM
  14. Very simple and useful. I saw it on “build internet” but I try it now and it works very well. Thanks.

    14 FredReply
    July 23rd, 2009 at 03:46 AM
  15. I have tried this with content, and I must say, it’s hard to have the columns centered (the last column will have ‘extra’ right padding, i.e. new class every X number of columns). . . Do you think there’s a better way to present the columns so that each row ends up centered? Thanks!

    15 ShukReply
    July 23rd, 2009 at 01:35 PM
  16. @Stephen: Yeah, this is very interesting. Thank you!

    16 Anna-LenaReply
    July 23rd, 2009 at 04:13 PM
  17. You did a great job on this! There is one more solution with the same result. Try using unordered list UL instead of #container and list items LI instead of column. On every 6th LI, just add a class, that is declared as clear:both. This class can also contain extra right padding as mentioned by Shuk above and even more things. This solution is more semantic and is using less XHTML code. If someone is interested, I can provide an example.
    Cheers!

    17 KrasiReply
    July 23rd, 2009 at 04:44 PM
  18. Hello, Rob
    The same layout can be achieved using the inline-block property, ofcourse with the appropriate browser hacks.

    18 Ionut PopaReply
    July 23rd, 2009 at 05:47 PM
  19. ehehe really nice and short code…

    19 haberlerReply
    July 24th, 2009 at 06:42 AM
  20. very great, i love your Site very much.
    These details are enormes. Thanks for the esplanation with the Comments

    but… some little bug i found..


    http://img5.imagebanana.com/view/w4zffrrh/bug.jpg

    im using macOSX (mac mini - intel) with firefox 3.5.1

    20 MichaelReply
    July 24th, 2009 at 02:57 PM
  21. @haberler: I must agree with you. Simplicity is everything.

    21 Ashraf AliReply
    July 26th, 2009 at 09:57 PM
  22. I would be hard not to agree with you. Dynamic columns would like much better and clear. They would give the site complete and professional look. I think that it is just one of those things that make a site fine. Taking first look at the site sometimes you can say: wow, that’s one fine piece of graphic. And some are just: man, that’s crap! So I think this is a step in right direction.

    22 Mieszkania GryfinoReply
    August 19th, 2009 at 02:10 PM
  23. i like the way the comments are set up in columns here. it soothes my artistic senses. it would be cool if someone could put comments in a linear or loop format, like threads/tree branches, so that we can see how like a uhm family tree, the entire format of all the comments.

    23 mertzReply
    August 20th, 2009 at 09:52 AM
  24. @Krasi: I’m interested in the code you mentioned. Please, give us some example. Thanks.

    24 Giraldi MaggioReply
    September 4th, 2009 at 01:20 AM
  25. @Giraldi: Here is the example: http://makaveev.com/test/dynamic_cols_eg4.html Just view source to see how it works. It is very similar to the original solution but it uses unordered lists and list items instead of DIV elements.

    25 KrasiReply
    September 4th, 2009 at 03:35 PM
  26. @Krasi: That’s good.. but I was wondering how the code/command looks to execute that.

    26 Giraldi MaggioReply
    September 4th, 2009 at 06:38 PM
  27. @Giraldi: Well, I’m not familiar with WordPress, so I can’t tell you! I was talking about only XHTML/CSS solution. Don’t know how to implement this in WordPress.

    27 KrasiReply
    September 4th, 2009 at 07:45 PM
  28. .commentlist li:nth-child(6n) { clear: left; }

    (not compatible with “some” browsers though)

    regards

    28 radoReply
    September 8th, 2009 at 01:34 AM
  29. Nice Design! Thanks

    29 RajReply
    September 12th, 2009 at 01:50 AM
  30. All your tutorials are great. Please would you do a tutorial on dynamic drop down list, would be awesome (at the current time its driving me crazy)

    30 MikeReply
    September 16th, 2009 at 04:44 AM
  31. Fantastic! Been admiring your site for some time now and the comments feature is a really awesome layout. Thanks for the breakdown of code.

    31 Dale NapierReply
    October 13th, 2009 at 03:45 PM
  32. @Thijs:  Nice.

    32 Mike thompsonReply
    October 30th, 2009 at 09:36 AM
  33. I was recently on your fantastic website and saw a reference of book you recommended for column design. This may not be pertinent to this discussion, but I cant seem to find it or remember what the book was called. Any help?? Thanks Rob. Probably 40th time Ive come to this site and it still rocks. Damn.

    33 Jeremy TaylorReply
    November 5th, 2009 at 04:14 AM
  34. @Jeremy: Any hints? There are a lot of books on grids out there! :)

    34 Rob MorrisReply
    November 5th, 2009 at 07:24 AM
  35. @Stephen: Just wondering how replying to a specific post affects the comment.

    35 Ogden EislerReply
    November 19th, 2009 at 01:17 AM
  36. Seems great!

    36 Cristian O. BalanReply
    December 23rd, 2009 at 02:04 AM

Leave a Reply +

So what do you think?

Reactions, responses or just feedback. Keep it short and sweet — you've got 100 words to do your thing.

Alternatively, if you're after instant contact, let me know your thoughts via twitter.

 (optional)

Recent Entries

  • Portfolio Building

    Saturday, November 14th, 2009

    So I’m being lazy by sharing emails instead of writing blog posts — but it’s been a busy time and with any luck there should be some exciting news at the end of it.

  • Ignorance Avoidance

    Friday, August 21st, 2009

    Well that was the title of an email I received last week. I thought I’d share it because I get a lot of emails asking this sort of stuff.

  • Dynamic Columns

    Monday, May 25th, 2009

    I get a lot of emails asking how I output the comments in a column format. Here’s how.

  • Flat Shadows

    Sunday, May 10th, 2009

    Another super-simple shadow trick in Photoshop for looking down on objects.

  • Get creative, you hack

    Tuesday, March 24th, 2009

    So you’re reading this thinking it may be slightly interesting. Maybe because the title seemed catchy, but not because this applies to you.

  • MacUser Magazine

    Monday, March 23rd, 2009

    So I just found out Digitalmash has been featured in last month’s MacUser ‘10 of the best online portfolios’. Huzzah!

  • ExpressionEngine:  Quick & dirty

    Monday, March 16th, 2009

    So as I recently mentioned, I’ve finally made the move from WordPress across to ExpressionEngine.

  • Back in business

    Thursday, March 12th, 2009

    Well after a wild couple of weeks, I’m back in action.

  • Swiss for WordPress

    Tuesday, February 24th, 2009

    Introducing a minimalist, image-free, grid-based theme for WordPress in three tasty flavours.

  • Stepping into web design

    Monday, February 16th, 2009

    I get a lot of people on either sides of a university degree asking me advice on how to establish themselves in the wide world of web design.

  • Navigation spacing in Photoshop

    Monday, February 2nd, 2009

    Here’s a tiny trick in Photoshop for having evenly spaced text items for things like horizontal navigation menus.

  • .NET mag fame

    Saturday, January 24th, 2009

    So the lovely folks at .NET magazine got in touch recently to ask if they could include Digitalmash.com in the gallery section of their mag.

  • Design by numbers

    Wednesday, January 7th, 2009

    I have a confession to make. Like quite a few designers, I’ve got a sprinkling of OCD about me.

  • Proper propaganda

    Friday, December 26th, 2008

    Well it’s that time of year again. Things are winding up and for most of us, we’ll have a little time away from the grindstone.

  • Stats: One month in

    Sunday, December 14th, 2008

    Nearly two-thirds of people know statistics are boring.

  • Pimped!

    Thursday, December 4th, 2008

    Isn’t it nice when people do nice things and ask for nothing in return?

  • The ever-watchful subconscious

    Tuesday, November 25th, 2008

    So I recently had a scary realisation when I noticed a very familiar diagram on the back of the shampoo bottle in my shower.

  • Designing for the web: 5 things I love & hate

    Tuesday, November 18th, 2008

    For anyone thinking about starting a career in web design, I’d like to share 5 things I’ve come to love and hate about designing for the web.

  • Ripped!

    Sunday, November 16th, 2008

    Well, a little over a week into the new site, we have our first rip of my design. Sigh…

  • Fun with a tablet

    Wednesday, November 12th, 2008

    No, I’m not talking about drugs. I picked up a cheap drawing tablet recently and thought I’d share my first experiments with it in Photoshop.

  • Incestuous design

    Friday, November 7th, 2008

    Ok, I admit — it’s a title that could turn some people off. It’s going to be a nightmare searching for it on google. But dammit, if nothing else, it’s original.

  • A New Beginning

    Saturday, November 1st, 2008

    So here we are. The first blog post on a fresh newly designed site. It’s like sliding into freshly pressed bed linen.

  • Logo marathon

    Wednesday, October 15th, 2008

    Sometimes (and I mean, only sometimes) the most grueling design jobs can be the most rewarding.

  • Making it real

    Monday, October 13th, 2008

    This is the story of how one idea can inform a whole campaign and visual identity.

  • What’s in a name?

    Sunday, October 12th, 2008

    What’s in a name? Well, I would argue that if your product’s good, then probably not that much.

  • Tippin’ the Balance

    Sunday, October 12th, 2008

    How do you turn a 200-page manual into something athletes will want to interact with?

1 / 82 / 83 / 84 / 85 / 86 / 87 / 88 / 8