Bootstrap 5 Collapsible Comment Section
A discussion post header with a collapsible comment list and an add-comment bar. Toggling the comment count expands the thread and new comments are appended instantly.
<section class="wdw-stage">
<div class="container" style="max-width:860px">
<article class="wdw-card overflow-hidden">
<header class="d-flex gap-3 p-4">
<span class="avatar avatar-lg" aria-hidden="true">JH</span>
<div class="flex-grow-1">
<div class="d-flex flex-wrap align-items-center gap-2 mb-1">
<h1 class="h5 wdw-heading mb-0">Is Sketch 3.9.1 stable?</h1>
<span class="badge rounded-pill bg-primary-subtle text-primary-emphasis">Video</span>
</div>
<div class="small wdw-muted d-flex flex-wrap align-items-center column-gap-2">
<span>by <strong class="text-body">Jesshead</strong></span>
<span class="sep" aria-hidden="true">·</span>
<span>6 hours ago</span>
</div>
</div>
</header>
<div class="d-flex flex-wrap align-items-center justify-content-between gap-2 px-4 py-2 border-top">
<button class="btn btn-link btn-sm p-0 fw-semibold text-decoration-none comment-toggle" type="button" data-bs-toggle="collapse" data-bs-target="#commentList" aria-expanded="true" aria-controls="commentList">
<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M21 12a8 8 0 0 1-11.6 7.1L3 20l1-4.6A8 8 0 1 1 21 12z"/></svg>
<span id="commentCount" aria-live="polite">3 comments</span>
<svg class="chev" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.25" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m6 9 6 6 6-6"/></svg>
</button>
<div>
<label for="commentSort" class="visually-hidden">Sort comments</label>
<select id="commentSort" class="form-select form-select-sm rounded-pill sort-select">
<option value="new">Newest</option>
<option value="old">Oldest</option>
</select>
</div>
</div>
<div class="collapse show" id="commentList">
<ul class="list-group list-group-flush px-4 mb-0" id="commentItems" aria-label="Comments">
<li class="comment-item d-flex gap-3 py-3" data-ts="2026-10-28T09:00">
<span class="avatar" aria-hidden="true">MO</span>
<div class="flex-grow-1 min-w-0">
<div class="bubble"><div class="small mb-1"><strong>Maya Ortiz</strong> <span class="wdw-muted ms-1">5 hours ago</span></div><p class="mb-0">Stable for me since Monday. Symbols finally stopped resetting overrides.</p></div>
<div class="d-flex gap-1 mt-1 ms-2">
<button type="button" class="btn btn-sm rounded-pill act-btn like-btn" aria-pressed="false" aria-label="Like comment by Maya Ortiz"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M20.8 4.6a5.5 5.5 0 0 0-7.8 0L12 5.7l-1-1.1a5.5 5.5 0 0 0-7.8 7.8l1 1.1L12 21l7.8-7.5 1-1.1a5.5 5.5 0 0 0 0-7.8z"/></svg><span class="like-n">12</span></button>
<button type="button" class="btn btn-sm rounded-pill act-btn reply-btn" aria-label="Reply to Maya Ortiz"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M9 14 4 9l5-5"/><path d="M20 20v-7a4 4 0 0 0-4-4H4"/></svg>Reply</button>
</div>
</div>
</li>
<li class="comment-item d-flex gap-3 py-3" data-ts="2026-10-28T10:00">
<span class="avatar" aria-hidden="true">TR</span>
<div class="flex-grow-1 min-w-0">
<div class="bubble"><div class="small mb-1"><strong>Tom Reyes</strong> <span class="wdw-muted ms-1">4 hours ago</span></div><p class="mb-0">Export slices are still slow on big files, otherwise no complaints.</p></div>
<div class="d-flex gap-1 mt-1 ms-2">
<button type="button" class="btn btn-sm rounded-pill act-btn like-btn" aria-pressed="false" aria-label="Like comment by Tom Reyes"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M20.8 4.6a5.5 5.5 0 0 0-7.8 0L12 5.7l-1-1.1a5.5 5.5 0 0 0-7.8 7.8l1 1.1L12 21l7.8-7.5 1-1.1a5.5 5.5 0 0 0 0-7.8z"/></svg><span class="like-n">4</span></button>
<button type="button" class="btn btn-sm rounded-pill act-btn reply-btn" aria-label="Reply to Tom Reyes"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M9 14 4 9l5-5"/><path d="M20 20v-7a4 4 0 0 0-4-4H4"/></svg>Reply</button>
</div>
</div>
</li>
<li class="comment-item d-flex gap-3 py-3" data-ts="2026-10-28T12:00">
<span class="avatar" aria-hidden="true">PN</span>
<div class="flex-grow-1 min-w-0">
<div class="bubble"><div class="small mb-1"><strong>Priya N.</strong> <span class="wdw-muted ms-1">2 hours ago</span></div><p class="mb-0">Would love a short changelog video, thanks for sharing this one.</p></div>
<div class="d-flex gap-1 mt-1 ms-2">
<button type="button" class="btn btn-sm rounded-pill act-btn like-btn" aria-pressed="false" aria-label="Like comment by Priya N."><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M20.8 4.6a5.5 5.5 0 0 0-7.8 0L12 5.7l-1-1.1a5.5 5.5 0 0 0-7.8 7.8l1 1.1L12 21l7.8-7.5 1-1.1a5.5 5.5 0 0 0 0-7.8z"/></svg><span class="like-n">7</span></button>
<button type="button" class="btn btn-sm rounded-pill act-btn reply-btn" aria-label="Reply to Priya N."><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M9 14 4 9l5-5"/><path d="M20 20v-7a4 4 0 0 0-4-4H4"/></svg>Reply</button>
</div>
</div>
</li>
</ul>
</div>
<form class="p-4 border-top bg-body-tertiary" id="commentForm">
<div class="d-flex align-items-center gap-3">
<span class="avatar d-none d-sm-inline-flex" aria-hidden="true">You</span>
<div class="input-group flex-grow-1 composer">
<label for="commentInput" class="visually-hidden">Add a comment</label>
<input id="commentInput" class="form-control" type="text" placeholder="Add a comment" autocomplete="off" required>
<button type="submit" class="btn btn-primary" aria-label="Post comment">
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m22 2-7 20-4-9-9-4z"/><path d="M22 2 11 13"/></svg>
<span class="d-none d-sm-inline">Comment</span>
</button>
</div>
</div>
</form>
</article>
</div>
</section>
/* Web Designer Wall library style — shared base for every Bootstrap 5 snippet.
Indigo accent, Plus Jakarta Sans, soft cards, pill buttons. Works in light and dark. */
@import url("https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&display=swap");
:root,
[data-bs-theme="light"] {
--wdw-font: "Plus Jakarta Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
--bs-body-font-family: var(--wdw-font);
--bs-primary: #4f46e5;
--bs-primary-rgb: 79, 70, 229;
--bs-primary-text-emphasis: #3730a3;
--bs-primary-bg-subtle: #eef2ff;
--bs-primary-border-subtle: #c7d2fe;
--bs-link-color: #4f46e5;
--bs-link-color-rgb: 79, 70, 229;
--bs-link-hover-color: #4338ca;
--bs-link-hover-color-rgb: 67, 56, 202;
--bs-focus-ring-color: rgba(79, 70, 229, 0.25);
--bs-border-radius: 0.75rem;
--bs-border-radius-lg: 1rem;
--bs-border-radius-xl: 1.25rem;
--wdw-radius-card: 1.25rem;
--wdw-shadow: 0 1px 2px rgba(16, 24, 40, 0.05), 0 16px 40px -16px rgba(16, 24, 40, 0.22);
--wdw-shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06), 0 4px 12px -4px rgba(16, 24, 40, 0.12);
--wdw-stage: #f5f6fb;
}
[data-bs-theme="dark"] {
--bs-primary: #818cf8;
--bs-primary-rgb: 129, 140, 248;
--bs-primary-text-emphasis: #c7d2fe;
--bs-primary-bg-subtle: rgba(129, 140, 248, 0.14);
--bs-primary-border-subtle: rgba(129, 140, 248, 0.35);
--bs-link-color: #a5b4fc;
--bs-link-color-rgb: 165, 180, 252;
--bs-link-hover-color: #c7d2fe;
--bs-link-hover-color-rgb: 199, 210, 254;
--bs-focus-ring-color: rgba(129, 140, 248, 0.35);
--wdw-shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 16px 40px -16px rgba(0, 0, 0, 0.7);
--wdw-shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 12px -4px rgba(0, 0, 0, 0.5);
--wdw-stage: #0f1117;
}
body { font-family: var(--wdw-font); -webkit-font-smoothing: antialiased; }
/* Bootstrap compiles its blue into these components; point them at the accent. */
.btn-primary {
--bs-btn-bg: var(--bs-primary); --bs-btn-border-color: var(--bs-primary);
--bs-btn-hover-bg: #4338ca; --bs-btn-hover-border-color: #4338ca;
--bs-btn-active-bg: #3730a3; --bs-btn-active-border-color: #3730a3;
--bs-btn-disabled-bg: var(--bs-primary); --bs-btn-disabled-border-color: var(--bs-primary);
--bs-btn-focus-shadow-rgb: var(--bs-primary-rgb);
}
[data-bs-theme="dark"] .btn-primary { --bs-btn-color: #0f1117; --bs-btn-hover-bg: #a5b4fc; --bs-btn-hover-border-color: #a5b4fc; --bs-btn-hover-color: #0f1117; --bs-btn-active-color: #0f1117; }
.btn-outline-primary {
--bs-btn-color: var(--bs-primary); --bs-btn-border-color: var(--bs-primary);
--bs-btn-hover-bg: var(--bs-primary); --bs-btn-hover-border-color: var(--bs-primary);
--bs-btn-active-bg: var(--bs-primary); --bs-btn-active-border-color: var(--bs-primary);
--bs-btn-focus-shadow-rgb: var(--bs-primary-rgb);
}
.btn { --bs-btn-font-weight: 600; min-height: 44px; display: inline-flex; align-items: center; justify-content: center; gap: .5rem; }
.btn-sm { min-height: 36px; }
.form-control, .form-select { min-height: 44px; border-radius: .75rem; }
.form-control:focus, .form-select:focus, .form-check-input:focus { border-color: rgba(var(--bs-primary-rgb), .6); box-shadow: 0 0 0 .25rem var(--bs-focus-ring-color); }
.form-check-input:checked { background-color: var(--bs-primary); border-color: var(--bs-primary); }
.nav-pills { --bs-nav-pills-link-active-bg: var(--bs-primary); }
.nav { --bs-nav-link-color: var(--bs-secondary-color); --bs-nav-link-hover-color: var(--bs-emphasis-color); }
.pagination { --bs-pagination-active-bg: var(--bs-primary); --bs-pagination-active-border-color: var(--bs-primary); --bs-pagination-color: var(--bs-body-color); --bs-pagination-focus-box-shadow: 0 0 0 .25rem var(--bs-focus-ring-color); }
.progress, .progress-stacked { --bs-progress-bar-bg: var(--bs-primary); }
.list-group { --bs-list-group-active-bg: var(--bs-primary); --bs-list-group-active-border-color: var(--bs-primary); }
.dropdown-menu { --bs-dropdown-link-active-bg: var(--bs-primary); border-radius: .875rem; box-shadow: var(--wdw-shadow-sm); }
.accordion { --bs-accordion-active-bg: var(--bs-primary-bg-subtle); --bs-accordion-active-color: var(--bs-primary-text-emphasis); --bs-accordion-btn-focus-box-shadow: 0 0 0 .25rem var(--bs-focus-ring-color); }
.badge { font-weight: 600; }
/* Library building blocks — use these instead of re-inventing them per snippet. */
.wdw-stage { min-height: 100vh; display: flex; align-items: center; background: var(--wdw-stage); padding-block: 3rem; }
.wdw-card { background: var(--bs-body-bg); border: 1px solid var(--bs-border-color-translucent); border-radius: var(--wdw-radius-card); box-shadow: var(--wdw-shadow); }
.wdw-card-sm { background: var(--bs-body-bg); border: 1px solid var(--bs-border-color-translucent); border-radius: 1rem; box-shadow: var(--wdw-shadow-sm); }
.wdw-aurora { position: relative; color: #fff; background: radial-gradient(60% 55% at 100% 0, #818cf8 0, transparent 70%), radial-gradient(65% 60% at 0 100%, #ec4899 0, transparent 65%), #1e1b4b; }
.wdw-eyebrow { display: inline-block; font-size: .75rem; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--bs-primary); }
.wdw-heading { font-weight: 800; letter-spacing: -.02em; color: var(--bs-emphasis-color); }
.wdw-muted { color: var(--bs-secondary-color); }
.wdw-icon { width: 2.75rem; height: 2.75rem; flex: none; display: inline-flex; align-items: center; justify-content: center; border-radius: .875rem; background: var(--bs-primary-bg-subtle); color: var(--bs-primary); }
/* Component */
.comment-toggle { min-height: 44px !important; display: inline-flex; gap: .35rem; }
.comment-toggle .chev { transition: transform .25s ease; }
.comment-toggle:not(.collapsed) .chev { transform: rotate(180deg); }
.min-w-0 { min-width: 0; }
.avatar { width: 2.5rem; height: 2.5rem; flex: none; display: inline-flex; align-items: center; justify-content: center; border-radius: 50%; background: var(--bs-primary-bg-subtle); color: var(--bs-primary-text-emphasis); font-size: .75rem; font-weight: 800; }
.avatar-lg { width: 3.5rem; height: 3.5rem; font-size: .95rem; }
.bubble { background: var(--bs-tertiary-bg); border-radius: 1rem; border-top-left-radius: .25rem; padding: .75rem 1rem; overflow-wrap: anywhere; }
.act-btn { --bs-btn-color: var(--bs-secondary-color); --bs-btn-hover-color: var(--bs-primary); border: 0; min-height: 44px; padding-inline: .75rem; font-size: .8125rem; }
.act-btn[aria-pressed="true"] { color: var(--bs-primary); }
.act-btn[aria-pressed="true"] svg { fill: currentColor; }
.sort-select { width: auto; min-height: 44px; padding-inline: 1rem 2rem; }
.composer .form-control { border-radius: 50rem 0 0 50rem; background: var(--bs-body-bg); padding-inline: 1.25rem; }
.composer .btn { border-radius: 0 50rem 50rem 0; padding-inline: 1.1rem; }
#commentList .list-group-flush > .comment-item { border: 0; }
#commentList .comment-item + .comment-item { border-top: 1px solid var(--bs-border-color-translucent); }
#commentItems li.is-new { animation: cmtIn .4s ease; }
@keyframes cmtIn { from { opacity: 0; transform: translateY(-6px); } to { opacity: 1; transform: none; } }
@media (max-width: 575.98px) { .sep { display: none; } header .flex-wrap > span:nth-child(2) { flex-basis: 100%; } }
@media (prefers-reduced-motion: reduce) { .comment-toggle .chev { transition: none; } #commentItems li.is-new { animation: none; } }
(function () {
var form = document.getElementById('commentForm');
var list = document.getElementById('commentItems');
var count = document.getElementById('commentCount');
var panel = document.getElementById('commentList');
var input = document.getElementById('commentInput');
var sort = document.getElementById('commentSort');
if (!form || !list || !count || !panel || !input) return;
function updateCount() {
var n = list.children.length;
count.textContent = n + (n === 1 ? ' comment' : ' comments');
}
function initLikes() {
list.addEventListener('click', function (e) {
var like = e.target.closest('.like-btn');
if (like) {
var on = like.getAttribute('aria-pressed') !== 'true';
var n = like.querySelector('.like-n');
like.setAttribute('aria-pressed', on);
n.textContent = (+n.textContent) + (on ? 1 : -1);
return;
}
var reply = e.target.closest('.reply-btn');
if (reply) {
input.value = '@' + reply.getAttribute('aria-label').replace('Reply to ', '') + ' ';
input.focus();
}
});
}
function initSort() {
if (!sort) return;
sort.addEventListener('change', function () {
var items = Array.prototype.slice.call(list.children);
items.sort(function (a, b) {
var d = a.getAttribute('data-ts') < b.getAttribute('data-ts') ? -1 : 1;
return sort.value === 'new' ? -d : d;
});
items.forEach(function (li) { list.appendChild(li); });
});
sort.dispatchEvent(new Event('change'));
}
function initComments() {
form.addEventListener('submit', function (e) {
e.preventDefault();
var text = input.value.trim();
if (!text) return;
var li = document.createElement('li');
li.className = 'comment-item d-flex gap-3 py-3 is-new';
li.setAttribute('data-ts', new Date().toISOString().slice(0, 16));
li.tabIndex = -1;
var av = document.createElement('span');
av.className = 'avatar'; av.setAttribute('aria-hidden', 'true'); av.textContent = 'You';
var box = document.createElement('div');
box.className = 'flex-grow-1 min-w-0';
var bubble = document.createElement('div');
bubble.className = 'bubble';
bubble.innerHTML = '<div class="small mb-1"><strong>You</strong> <span class="wdw-muted ms-1">just now</span></div>';
var p = document.createElement('p');
p.className = 'mb-0';
p.textContent = text;
bubble.appendChild(p);
box.appendChild(bubble);
li.appendChild(av); li.appendChild(box);
if (sort && sort.value === 'new') list.insertBefore(li, list.firstChild); else list.appendChild(li);
updateCount();
input.value = '';
bootstrap.Collapse.getOrCreateInstance(panel, { toggle: false }).show();
li.focus({ preventScroll: false });
});
}
initLikes();
initSort();
initComments();
})();
Built on Bootstrap 5.3.8 with plain JavaScript — no jQuery. Supports light and dark mode via data-bs-theme.