Bootstrap 5 Collapsible Chat Widget
A support chat widget with an agent header, scrolling message history, quick replies, typing indicator and message input, which can be minimized, closed and reopened.
<section class="wdw-stage chat-stage">
<div class="container" style="max-width:1040px">
<div class="row align-items-center gx-4 gy-5 mx-0">
<div class="col-lg-5 text-center text-lg-start">
<span class="wdw-eyebrow">Live support</span>
<h1 class="wdw-heading display-6 mt-2 mb-3">Talk to a real person, right from the page</h1>
<p class="text-body-secondary mb-4">A collapsible chat widget with message history, typing indicator and quick replies. Minimize it, close it, or bring it back from the launcher.</p>
<button type="button" class="btn btn-primary rounded-pill" id="chatReopen">Open chat</button>
</div>
<div class="col-lg-7 d-flex flex-column align-items-center align-items-lg-end chat-slot">
<div class="chat-widget wdw-card" id="chatWidget" role="region" aria-label="Chat with support">
<header class="chat-head wdw-aurora d-flex align-items-center gap-3 p-3">
<div class="position-relative"><img src="https://i.pravatar.cc/80?img=47" alt="" width="40" height="40" class="rounded-circle"><span class="chat-dot border border-2 border-dark-subtle"></span></div>
<div class="flex-grow-1 lh-sm"><div class="fw-bold">Sofia from Support</div><div class="small opacity-75">Online · replies in minutes</div></div>
<button type="button" class="btn btn-sm rounded-circle p-0 chat-ico" id="chatMin" aria-label="Minimize chat" aria-expanded="true" aria-controls="chatBody"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" aria-hidden="true"><path d="M5 12h14"/></svg></button>
<button type="button" class="btn btn-sm rounded-circle p-0 chat-ico" id="chatClose" aria-label="Close chat"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" aria-hidden="true"><path d="M6 6l12 12M18 6 6 18"/></svg></button>
</header>
<div id="chatBody">
<div class="chat-msgs p-3 d-flex flex-column gap-3" id="chatMsgs" role="log" aria-live="polite">
<div class="chat-day text-center small text-body-secondary">Today</div>
<div class="chat-row"><img src="https://i.pravatar.cc/60?img=47" alt="" width="28" height="28" class="rounded-circle"><div><div class="bubble in">Hi there! How can we help you today?</div><div class="meta">Sofia · 10:02</div></div></div>
<div class="chat-row out"><div><div class="bubble out">I'd like to change my plan before the next billing date.</div><div class="meta text-end">You · 10:03</div></div></div>
<div class="chat-row"><img src="https://i.pravatar.cc/60?img=47" alt="" width="28" height="28" class="rounded-circle"><div><div class="bubble in">Sure thing. You can switch any time from Settings, or I can do it for you right now.</div><div class="meta">Sofia · 10:03</div></div></div>
</div>
<div class="px-3 pb-2 d-flex gap-2 flex-wrap" id="chatQuick">
<button type="button" class="btn btn-sm btn-outline-primary rounded-pill">Switch for me</button>
<button type="button" class="btn btn-sm btn-outline-primary rounded-pill">Show pricing</button>
</div>
<form class="chat-form d-flex gap-2 p-3 pt-2" id="chatForm" autocomplete="off">
<input type="text" class="form-control rounded-pill" id="chatInput" placeholder="Write a message…" aria-label="Message">
<button type="submit" class="btn btn-primary rounded-circle p-0 chat-send" aria-label="Send"><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 11 13M22 2l-7 20-4-9-9-4z"/></svg></button>
</form>
</div>
</div>
<button type="button" class="btn btn-primary rounded-circle p-0 chat-fab position-relative d-none" id="chatFab" aria-label="Open chat, 1 unread message"><svg width="26" height="26" 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.1L4 20l1-4.6A8 8 0 1 1 21 12z"/></svg><span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger border border-2 border-body" aria-hidden="true">1</span></button>
</div>
</div>
</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 */
.chat-widget { width: 100%; max-width: 440px; overflow: hidden; transition: opacity .2s, transform .2s; }
.chat-widget.is-closed { display: none; }
.chat-head { border-radius: 0; }
.chat-head img { object-fit: cover; }
.chat-dot { position: absolute; right: -1px; bottom: -1px; width: 12px; height: 12px; border-radius: 50%; background: #22c55e; }
.chat-ico { width: 36px; height: 36px; min-height: 0; border: 0; background: rgba(255,255,255,.12); color: #fff; display: inline-flex; align-items: center; justify-content: center; }
.chat-ico:hover { background: rgba(255,255,255,.24); }
#chatBody[hidden] { display: none; }
.chat-msgs { height: 360px; overflow-y: auto; scroll-behavior: smooth; }
.chat-row { display: flex; gap: .5rem; align-items: flex-end; max-width: 88%; }
.chat-row.out { align-self: flex-end; }
.chat-row img { object-fit: cover; flex: none; }
.bubble { padding: .6rem .9rem; border-radius: 1.1rem; font-size: .9rem; line-height: 1.4; }
.bubble.in { background: var(--bs-tertiary-bg); color: var(--bs-body-color); border-bottom-left-radius: .3rem; }
.bubble.out { background: var(--bs-primary); color: #fff; border-bottom-right-radius: .3rem; }
[data-bs-theme="dark"] .bubble.out { color: #0f1117; }
.meta { font-size: .72rem; color: var(--bs-secondary-color); margin-top: .2rem; padding-inline: .4rem; }
.chat-send { width: 44px; height: 44px; flex: none; }
.chat-fab { width: 60px; height: 60px; min-height: 0; box-shadow: var(--wdw-shadow); }
.chat-fab:focus-visible, .chat-ico:focus-visible { outline: 2px solid var(--bs-primary); outline-offset: 3px; }
.typing { display: inline-flex; gap: 4px; padding: .8rem .9rem; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--bs-secondary-color); animation: blink 1s infinite; }
.typing i:nth-child(2) { animation-delay: .15s; } .typing i:nth-child(3) { animation-delay: .3s; }
@keyframes blink { 0%, 60%, 100% { opacity: .25; } 30% { opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .typing i { animation: none; } .chat-msgs { scroll-behavior: auto; } }
function initChatWidget() {
var widget = document.getElementById('chatWidget');
if (!widget) return;
var body = document.getElementById('chatBody'), msgs = document.getElementById('chatMsgs');
var min = document.getElementById('chatMin'), form = document.getElementById('chatForm');
var input = document.getElementById('chatInput'), quick = document.getElementById('chatQuick');
function time() { var d = new Date(); return d.getHours() + ':' + String(d.getMinutes()).padStart(2, '0'); }
function esc(t) { var s = document.createElement('span'); s.textContent = t; return s.innerHTML; }
function add(text, out) {
var row = document.createElement('div');
row.className = 'chat-row' + (out ? ' out' : '');
row.innerHTML = (out ? '' : '<img src="https://i.pravatar.cc/60?img=47" alt="" width="28" height="28" class="rounded-circle">') +
'<div><div class="bubble ' + (out ? 'out' : 'in') + '">' + esc(text) + '</div><div class="meta' + (out ? ' text-end' : '') + '">' + (out ? 'You' : 'Sofia') + ' · ' + time() + '</div></div>';
msgs.appendChild(row); msgs.scrollTop = msgs.scrollHeight;
}
function reply() {
var t = document.createElement('div');
t.className = 'chat-row';
t.innerHTML = '<img src="https://i.pravatar.cc/60?img=47" alt="" width="28" height="28" class="rounded-circle"><div class="bubble in typing" aria-hidden="true"><i></i><i></i><i></i></div><span class="visually-hidden">Sofia is typing</span>';
msgs.appendChild(t); msgs.scrollTop = msgs.scrollHeight;
setTimeout(function () { t.remove(); add('Got it, give me a second to take care of that.', false); }, 1300);
}
function send(text) { add(text, true); if (quick) quick.remove(); quick = null; reply(); }
form.addEventListener('submit', function (e) {
e.preventDefault();
var v = input.value.trim(); if (!v) return;
input.value = ''; send(v);
});
if (quick) quick.addEventListener('click', function (e) { var b = e.target.closest('button'); if (b) send(b.textContent); });
min.addEventListener('click', function () {
var open = body.hidden; body.hidden = !open; min.setAttribute('aria-expanded', open);
});
var fab = document.getElementById('chatFab'), reopen = document.getElementById('chatReopen');
function openChat() {
widget.classList.remove('is-closed'); fab.classList.add('d-none');
body.hidden = false; min.setAttribute('aria-expanded', 'true'); input.focus();
}
document.getElementById('chatClose').addEventListener('click', function () {
widget.classList.add('is-closed'); fab.classList.remove('d-none'); fab.focus();
});
fab.addEventListener('click', openChat);
reopen.addEventListener('click', openChat);
}
initChatWidget();
Built on Bootstrap 5.3.8 with plain JavaScript — no jQuery. Supports light and dark mode via data-bs-theme.