How to create alternate comment classes in Drupal

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

Last week I was trying to figure out how to add a class to my comments in drupal so I could add alternate background colours. This is documented for table rows but I wasn't able to find anything about how to do it with comments.

Well, it turns out that the same $zebra function used for table rows can also be used for comments. Simply open the comment.tpl.php file (copy one from a default template if there isn't one in your theme), and look for the <div class="comment <?php print $comment_classes;?>" > line. (this might be a bit different in Drupal 5 - I'm using 6).

Next, add the $zebra function to the class attribute. This will create alternating "odd" and "even" classes on your comments:

<div class="comment <?php print $comment_classes; print $zebra ?>" >

Now you can style these classes in your CSS.

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

and I spent a couple of hours making zebra stripes for the posts at Hudson County Self-Help Center with javascript!

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.