2

I'm interested in making the comments that appear on my resource content type nodes show with alternating background colors. Anyone know how to make this happen?

I tried something like this with CSS but I'm not seeing how I can make this work as the comments don't seem to have anything regular I can target (as the class is quite a bit different if the comment is made by a logged in user vs. an anonymous user for instance):

.comments-by-anonymous .field-item:nth-child(even) {
    background: #fcfc33;
}

Then I thought, well I could try and do some PHP if/then hackery to my comment.tpl.php template but I'm unfamiliar enough with that template to know if its a) even the correct one to edit, and b) if perhaps there might just be a better way to do it.

Anyone have any good ideas?

2

Looks like you can use the $zebra class in the comment template.

Add it in the preprocess hook as a class:

/**
 * Implements hook_preprocess_comment
 */
function THEME_preprocess_comment(&$variables) {
  // Zebra classes are added in the default template_preprocess function
  if (!empty($variables['zebra'])) {
    $variables['classes_array'][] = $variables['zebra'];
  }
}

You can then style the 'odd' and 'even' comments with CSS

e.g, something like:

.comment.odd{
  background-color: #efefef;
}
.comment.even{
  background-color: #ffffff;
}
  • Brilliant. Thank you @David Thomas. I'd heard about $zebra before but hadn't connected the dots until now. Thanks again that worked great. – Sage Mar 1 '15 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.