Login

Limiting indentations for threaded comments in Drupal

stplanken — January 28, 2009 - 19:32

Drupal

(This works for Drupal 5.x and 6.x.)

Threaded comments in Drupal are fine, but after a certain number of replies, you really need to limit the amount of indentation or the comment space will get too small to be useful.

At first, I hacked the comment module and added a variable to define the maximum number of nested comments. Since I am drastically reducing the number of hacks in favor of more appropriate solutions, I decided to solve this with CSS.

Take a look at the result. In comments, the left indentation (margin-left) is taken from comment.css.

Main post
Simulated main post to which comments have been added.
First comment. No indentation here yet.
Second comment, first indentation.
Third comment, second indentation.
Fourth comment, third indentation.
Fifth comment, fourth indentation.
Sixth comment, fifth indentation.
Seventh comment, no further indentations.
Eighth comment, no further indentations.

By default, comment.css defines the left margin for each indentation:

/* Default from comment.css */
.indented {
  margin-left: 25px;
}

To limit the maximum number of indentations, I added the following to the theme style sheet:

/* Limit max nbr of indentations */
.indented .indented .indented .indented .indented .indented {
  margin-left: 0;
}

The maximum level of nested comments in this example is 6, so the class is repeated that many times. This works regardless of the actual number of nested comments. Perhaps not the most elegant solution, but it is easy to implement.

#1

Nice ! Thanks !

#2

You're welcome.

#3

Thanks, from me too. Fixes a major annoyance.

#4

Good! I am still using this in my CSS myself too, although on this site I switched to flat comments.

#5

Hello, Do you know how to change the way threaded comments are displayed? Cos when I post a threaded comment it comes on top instead of on the bottom. Which is very annoying. Is there a way to do this?

#6

You can change that via /admin/content/node-type, then select the node type you wish to modify the comments for. Look at the comments section; you will want to change the default display order.

— Stephan

Post new comment

Image CAPTCHA
Enter the characters shown in the image.