Skip to content

Nested Comment Replies with Thread Lines

Reddit-style nested comment thread with curved CSS thread lines, collapsible branches, voting and inline replies in vanilla JS.

Also known as: nested comments, threaded comments, reddit style comments, comment replies thread

Nested Comment Replies with Thread Lines live demo

Open in new tab

Pure CSS code (HTML, CSS, JS)

A Reddit-style comment thread with nested replies, built with plain HTML, CSS and a small vanilla JavaScript file. Curved thread lines connect every reply to its parent, and each branch can be collapsed. Use it under blog posts, forum topics, product Q&A pages or changelogs.

How it works

The root section.nr-thread holds a header with the comment count (.nr-count-num) and a ul.nr-list. Every comment is an li.nr-comment containing an article.nr-card and, if it has answers, a nested ul.nr-replies. The card is a two-column grid: .nr-rail with the initials avatar and the .nr-stem line, and .nr-main with the .nr-head, the .nr-body (text and .nr-actions) and a .nr-note that appears when the branch is collapsed.

The lines are pure CSS. .nr-stem grows under the avatar to the bottom of the card. Each reply draws a rounded elbow with ::before (a left and bottom border with border-bottom-left-radius), and every reply except the last draws a straight ::after line so the parent line continues to the next sibling. All positions are calculated from --nr-avatar, --nr-indent and --nr-line-width, so the lines stay aligned at any size. Hovering a line highlights the whole branch through a :has() rule.

The script uses one delegated click listener per .nr-thread, so replies added later work too. It collapses and expands branches (.is-collapsed, the hidden attribute and a "3 replies hidden" note), toggles up and down votes on .nr-vote, opens an inline .nr-form for replies and appends the new comment to the right ul.nr-replies. Posting is simulated; send the text to your API with fetch() in the form's submit handler.

Customization

  • --nr-indent on .nr-thread: horizontal step per nesting level; the elbows resize automatically.
  • --nr-avatar: avatar size, which also moves the thread lines to the avatar center.
  • --nr-line and --nr-line-hover: thread line color and the highlight color while a line is hovered.
  • --nr-up and --nr-down: colors of the active upvote and downvote arrows and the score.
  • .nr-avatar--amber, .nr-avatar--teal and the other avatar modifiers set --nr-avatar-bg; add your own or replace the initials with an image.
  • Add is-collapsed to an li.nr-comment (with hidden on its .nr-body and .nr-replies) to render a branch collapsed, as on the sam_okafor comment.
  • .nr-badge marks the original poster; remove it or reuse it for moderator labels.

Use cases

  • Blog post comments: readers reply to each other and collapse long branches with curved thread lines
  • Forum topics: nested replies, voting and inline reply forms keep discussions organized
  • Product Q&A pages: customers and staff answer questions in clearly connected threads
  • Changelogs and docs: users discuss updates under an entry, with the original poster badged

Accessibility

Each article is labelled by its author name. The .nr-toggle button uses aria-expanded and an aria-label such as "Collapse comment by maya_k", and the vote buttons use aria-pressed inside a labelled role="group", with the score in an aria-live region. The clickable .nr-stem line is a mouse shortcut only, so it is aria-hidden and out of the tab order. The reply textarea has a visually hidden label, focus moves into the form and onto the new comment after posting, and all buttons show a focus ring. Transitions are disabled with prefers-reduced-motion.

Usage notes

Several threads can live on one page. Below 480px the indent and avatar shrink and the Share button is hidden to keep deep threads readable.

Nested Comment Replies with Thread Lines at a glance

Framework
Pure CSS
Code
HTML, CSS, JavaScript
JavaScript
Vanilla JavaScript, no jQuery
Dependencies
None
Layout
Responsive, works on mobile
Category
Comments
License
Free for personal and commercial use

Questions about this template

Can I use the Nested Comment Replies with Thread Lines code for free?

Yes. You can copy or download the code and use it in personal and commercial projects, including client work. No sign-up is needed. Reselling it as part of a component library or template collection is not allowed; see the license.

How do I add it to my website?

There is nothing to install. Paste the HTML where the component should appear, add the CSS to your stylesheet and load the JavaScript before the closing </body> tag. Download HTML gives you a complete page with everything already linked.

Does it need jQuery or a JavaScript framework?

No. The script is plain vanilla JavaScript, so it works without jQuery, React or Vue. You can still use the markup inside those frameworks.

Does it work on mobile?

Yes. The layout adapts to small screens. Use the Mobile and Tablet buttons above the live demo to check it at phone and tablet width.

Requirements

No dependencies. Pure HTML, CSS and JavaScript.

Nested Comment Replies with Thread Lines – Pure CSS comments component screenshot
Nested Comment Replies with Thread Lines · Pure CSS Comments

More Comments

Comments with Likes and User Photos – Bootstrap 5 comments preview

Comments with Likes and User Photos

Dark Bootstrap 5 comment section with user photos, timestamps, like buttons, inline reply forms, nested replies and Top/Newest sorting.

Comment Section with Reply Form – Bootstrap 5 comments preview

Comment Section with Reply Form

Bootstrap 5 comment section with threaded replies, collapsible reply forms, like buttons, a new comment form and live comment count.

Comment Thread – Tailwind CSS comments preview

Comment Thread

Nested Tailwind CSS comment thread with threaded replies, like toggles, inline reply forms and a new comment form, built with accessible vanilla JavaScript.