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.
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.
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).
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.
Nobody likes a lurker. Come join in the discussion.
Tweet This / StumbleUpon / Digg / Float / LinkedIn / Reddit / Technorati
Del.icio.us / Google / Live / Ma.gnolia / Yahoo
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.
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.
I get a lot of emails asking how I output the comments in a column format. Here’s how.
Another super-simple shadow trick in Photoshop for looking down on objects.
A nifty trick for shadows on upright objects that’s dead simple in Photoshop.
Lately I’ve been getting pretty used to having my designs ripped. Doesn’t mean I dislike it any less.
I’ve been thinking more about Scott Stevenson’s great post debating Google’s approach to designing by data.
TweetDeck is a great application with a pretty average dock icon. This morning I decided I’d whip up an pair of alternatives.
So you’re reading this thinking it may be slightly interesting. Maybe because the title seemed catchy, but not because this applies to you.
So I just found out Digitalmash has been featured in last month’s MacUser ‘10 of the best online portfolios’. Huzzah!
So as I recently mentioned, I’ve finally made the move from WordPress across to ExpressionEngine.
Well after a wild couple of weeks, I’m back in action.
Introducing a minimalist, image-free, grid-based theme for WordPress in three tasty flavours.
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.
Here’s a tiny trick in Photoshop for having evenly spaced text items for things like horizontal navigation menus.
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.
I have a confession to make. Like quite a few designers, I’ve got a sprinkling of OCD about me.
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.
Nearly two-thirds of people know statistics are boring.
Isn’t it nice when people do nice things and ask for nothing in return?
So I recently had a scary realisation when I noticed a very familiar diagram on the back of the shampoo bottle in my shower.
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.
Well, a little over a week into the new site, we have our first rip of my design. Sigh…
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.
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.
So here we are. The first blog post on a fresh newly designed site. It’s like sliding into freshly pressed bed linen.
Sometimes (and I mean, only sometimes) the most grueling design jobs can be the most rewarding.
This is the story of how one idea can inform a whole campaign and visual identity.
What’s in a name? Well, I would argue that if your product’s good, then probably not that much.
How do you turn a 200-page manual into something athletes will want to interact with?
Thank you Rob, I’ve been curious myself as to how you had accomplished this.
1 StephenReply
May 26th, 2009 at 12:48 AMThat’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 AMThe simplest solutions are always the best. Thanks for sharing.
3 isaninjaReply
May 27th, 2009 at 05:08 PMVery 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 AMI 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 AMReally like the way you do things around here, keep it up :-)
6 FernandaReply
May 29th, 2009 at 10:11 PMRob 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 AMWhile 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 AMjust 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 PMHey, 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 PMSo 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 PMThis 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@Stephen: Cool idea. I’m trying a nested comment to see how it looks
13 CorbachoReply
July 23rd, 2009 at 02:17 AMVery 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 AMI 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@Stephen: Yeah, this is very interesting. Thank you!
16 Anna-LenaReply
July 23rd, 2009 at 04:13 PMYou 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 PMHello, 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 PMehehe really nice and short code…
19 haberlerReply
July 24th, 2009 at 06:42 AMvery 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@haberler: I must agree with you. Simplicity is everything.
21 Ashraf AliReply
July 26th, 2009 at 09:57 PMI 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 PMi 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@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@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@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@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.commentlist li:nth-child(6n) { clear: left; }
(not compatible with “some” browsers though)
regards
28 radoReply
September 8th, 2009 at 01:34 AMNice Design! Thanks
29 RajReply
September 12th, 2009 at 01:50 AMAll 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 AMFantastic! 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@Thijs: Nice.
32 Mike thompsonReply
October 30th, 2009 at 09:36 AMI 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@Jeremy: Any hints? There are a lot of books on grids out there! :)
34 Rob MorrisReply
November 5th, 2009 at 07:24 AM@Stephen: Just wondering how replying to a specific post affects the comment.
35 Ogden EislerReply
November 19th, 2009 at 01:17 AMSeems great!
36 Cristian O. BalanReply
December 23rd, 2009 at 02:04 AM