Bootstrap 5 Comment Form with Star Rating
A review form with an interactive five-star rating, a floating-label comment box with a character counter and an inline confirmation after posting.
<section class="cr-section wdw-stage">
<div class="container" style="max-width:980px">
<div class="wdw-card overflow-hidden">
<div class="row g-0">
<aside class="col-md-5 p-4 p-lg-5 bg-body-tertiary" aria-label="Rating summary">
<span class="wdw-eyebrow mb-2">Customer reviews</span>
<div class="d-flex align-items-end gap-2 mb-1"><span class="display-3 wdw-heading lh-1">4.8</span><span class="text-body-secondary mb-2">out of 5</span></div>
<div class="d-flex gap-1 text-warning mb-1" role="img" aria-label="4.8 out of 5 stars"><svg class="" width="22" height="22" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="m12 2 3.1 6.3 6.9 1-5 4.9 1.2 6.9L12 17.8 5.8 21l1.2-6.9-5-4.9 6.9-1z"/></svg><svg class="" width="22" height="22" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="m12 2 3.1 6.3 6.9 1-5 4.9 1.2 6.9L12 17.8 5.8 21l1.2-6.9-5-4.9 6.9-1z"/></svg><svg class="" width="22" height="22" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="m12 2 3.1 6.3 6.9 1-5 4.9 1.2 6.9L12 17.8 5.8 21l1.2-6.9-5-4.9 6.9-1z"/></svg><svg class="" width="22" height="22" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="m12 2 3.1 6.3 6.9 1-5 4.9 1.2 6.9L12 17.8 5.8 21l1.2-6.9-5-4.9 6.9-1z"/></svg><svg class="" width="22" height="22" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="m12 2 3.1 6.3 6.9 1-5 4.9 1.2 6.9L12 17.8 5.8 21l1.2-6.9-5-4.9 6.9-1z"/></svg></div>
<p class="small text-body-secondary mb-4">Based on 1,284 reviews</p>
<ul class="list-unstyled d-grid gap-2 small mb-0">
<li class="d-flex align-items-center gap-2"><span class="cr-bar-l">5</span><div class="progress flex-grow-1" style="height:6px" role="progressbar" aria-label="5 stars" aria-valuenow="82" aria-valuemin="0" aria-valuemax="100"><div class="progress-bar bg-warning" style="width:82%"></div></div><span class="cr-bar-r text-body-secondary">82%</span></li>
<li class="d-flex align-items-center gap-2"><span class="cr-bar-l">4</span><div class="progress flex-grow-1" style="height:6px" role="progressbar" aria-label="4 stars" aria-valuenow="12" aria-valuemin="0" aria-valuemax="100"><div class="progress-bar bg-warning" style="width:12%"></div></div><span class="cr-bar-r text-body-secondary">12%</span></li>
<li class="d-flex align-items-center gap-2"><span class="cr-bar-l">3</span><div class="progress flex-grow-1" style="height:6px" role="progressbar" aria-label="3 stars" aria-valuenow="4" aria-valuemin="0" aria-valuemax="100"><div class="progress-bar bg-warning" style="width:4%"></div></div><span class="cr-bar-r text-body-secondary">4%</span></li>
<li class="d-flex align-items-center gap-2"><span class="cr-bar-l">2</span><div class="progress flex-grow-1" style="height:6px" role="progressbar" aria-label="2 stars" aria-valuenow="1" aria-valuemin="0" aria-valuemax="100"><div class="progress-bar bg-warning" style="width:1%"></div></div><span class="cr-bar-r text-body-secondary">1%</span></li>
<li class="d-flex align-items-center gap-2"><span class="cr-bar-l">1</span><div class="progress flex-grow-1" style="height:6px" role="progressbar" aria-label="1 star" aria-valuenow="1" aria-valuemin="0" aria-valuemax="100"><div class="progress-bar bg-warning" style="width:1%"></div></div><span class="cr-bar-r text-body-secondary">1%</span></li>
</ul>
</aside>
<div class="col-md-7 p-4 p-lg-5 bg-body">
<div class="d-flex align-items-center gap-3 mb-4">
<img src="https://i.pravatar.cc/96?img=47" width="48" height="48" alt="Your profile photo" class="rounded-circle">
<div>
<h2 class="h5 wdw-heading mb-0">Leave a review</h2>
<p class="small text-body-secondary mb-0">Posting as Emma Larsen</p>
</div>
</div>
<form id="crForm" novalidate>
<fieldset class="mb-3">
<legend class="form-label small fw-semibold mb-1">Your rating</legend>
<div class="cr-rating d-flex gap-1">
<input type="radio" class="btn-check" name="rating" value="1" id="cr1"><label for="cr1" class="cr-star-btn" title="1 star"><svg class="cr-star text-body-tertiary" width="32" height="32" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="m12 2 3.1 6.3 6.9 1-5 4.9 1.2 6.9L12 17.8 5.8 21l1.2-6.9-5-4.9 6.9-1z"/></svg><span class="visually-hidden">1 star</span></label>
<input type="radio" class="btn-check" name="rating" value="2" id="cr2"><label for="cr2" class="cr-star-btn" title="2 stars"><svg class="cr-star text-body-tertiary" width="32" height="32" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="m12 2 3.1 6.3 6.9 1-5 4.9 1.2 6.9L12 17.8 5.8 21l1.2-6.9-5-4.9 6.9-1z"/></svg><span class="visually-hidden">2 stars</span></label>
<input type="radio" class="btn-check" name="rating" value="3" id="cr3"><label for="cr3" class="cr-star-btn" title="3 stars"><svg class="cr-star text-body-tertiary" width="32" height="32" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="m12 2 3.1 6.3 6.9 1-5 4.9 1.2 6.9L12 17.8 5.8 21l1.2-6.9-5-4.9 6.9-1z"/></svg><span class="visually-hidden">3 stars</span></label>
<input type="radio" class="btn-check" name="rating" value="4" id="cr4"><label for="cr4" class="cr-star-btn" title="4 stars"><svg class="cr-star text-body-tertiary" width="32" height="32" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="m12 2 3.1 6.3 6.9 1-5 4.9 1.2 6.9L12 17.8 5.8 21l1.2-6.9-5-4.9 6.9-1z"/></svg><span class="visually-hidden">4 stars</span></label>
<input type="radio" class="btn-check" name="rating" value="5" id="cr5"><label for="cr5" class="cr-star-btn" title="5 stars"><svg class="cr-star text-body-tertiary" width="32" height="32" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="m12 2 3.1 6.3 6.9 1-5 4.9 1.2 6.9L12 17.8 5.8 21l1.2-6.9-5-4.9 6.9-1z"/></svg><span class="visually-hidden">5 stars</span></label>
</div>
<span class="small fw-semibold text-body-secondary d-block mt-1" id="crRatingText" aria-live="polite">Rate it</span>
</fieldset>
<div class="mb-3">
<label for="crTitle" class="form-label small fw-semibold">Review title</label>
<input type="text" id="crTitle" class="form-control" placeholder="Sum it up in a few words" maxlength="80">
</div>
<div class="mb-3 position-relative">
<label for="crText" class="form-label small fw-semibold">Your review</label>
<textarea id="crText" class="form-control pb-4" rows="4" placeholder="What did you like or dislike?" maxlength="300" required></textarea>
<span class="cr-count small text-body-secondary"><span id="crCount">0</span>/300</span>
</div>
<fieldset class="mb-3">
<legend class="form-label small fw-semibold mb-1">Would you recommend it?</legend>
<div class="d-flex gap-2">
<input type="radio" class="btn-check" name="recommend" id="crRecYes" value="Yes" checked><label class="btn btn-outline-primary btn-sm rounded-pill px-3" for="crRecYes">Yes</label>
<input type="radio" class="btn-check" name="recommend" id="crRecNo" value="No"><label class="btn btn-outline-primary btn-sm rounded-pill px-3" for="crRecNo">No</label>
</div>
</fieldset>
<div class="small mb-2" style="min-height:1.25rem"><span id="crError" class="text-danger" role="alert"></span></div>
<div class="d-flex justify-content-end align-items-center gap-2">
<button type="reset" class="btn btn-link link-body-emphasis fw-semibold text-decoration-none px-3">Cancel</button>
<button type="submit" class="btn btn-primary rounded-pill px-4">Post review</button>
</div>
</form>
<div id="crPosted" class="d-none mt-4 p-3 rounded-3 bg-body-tertiary border" role="status">
<div class="d-flex gap-1 text-warning mb-1" id="crPostedStars" aria-hidden="true"></div>
<p class="fw-semibold small mb-0" id="crPostedTitle"></p>
<p class="mb-0 small" id="crPostedText"></p>
</div>
</div>
</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 */
.cr-star-btn { cursor: pointer; margin: 0; padding: 2px; line-height: 0; border-radius: .5rem; }
.cr-star { transition: color .15s, transform .15s; }
.cr-star.cr-on { color: var(--bs-warning) !important; }
.cr-star-btn:hover .cr-star { transform: scale(1.1); }
.btn-check:focus-visible + .cr-star-btn { outline: 2px solid var(--bs-primary); outline-offset: 2px; }
.cr-bar-l { width: 1rem; }
.cr-bar-r { width: 2.5rem; text-align: right; }
.cr-section textarea.form-control { min-height: 0; }
.cr-count { position: absolute; right: .9rem; bottom: .5rem; pointer-events: none; }
@media (prefers-reduced-motion: reduce) { .cr-star { transition: none; } }
function initReviewForm() {
var form = document.getElementById('crForm');
if (!form) return;
var text = document.getElementById('crText');
var count = document.getElementById('crCount');
var label = document.getElementById('crRatingText');
var err = document.getElementById('crError');
var stars = form.querySelectorAll('.cr-star');
var names = ['', 'Poor', 'Fair', 'Good', 'Very good', 'Excellent'];
var STAR = '<svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor"><path d="m12 2 3.1 6.3 6.9 1-5 4.9 1.2 6.9L12 17.8 5.8 21l1.2-6.9-5-4.9 6.9-1z"/></svg>';
function paint(n) {
stars.forEach(function (s, i) { s.classList.toggle('cr-on', i < n); });
}
function current() {
var c = form.querySelector('input[name="rating"]:checked');
return c ? +c.value : 0;
}
text.addEventListener('input', function () { count.textContent = text.value.length; });
form.querySelectorAll('.cr-star-btn').forEach(function (btn, i) {
btn.addEventListener('mouseenter', function () { paint(i + 1); });
btn.addEventListener('mouseleave', function () { paint(current()); });
});
form.addEventListener('change', function (e) {
if (e.target.name !== 'rating') return;
paint(current());
label.textContent = names[current()];
label.classList.replace('text-body-secondary', 'text-body-emphasis');
});
form.addEventListener('reset', function () {
paint(0);
label.textContent = 'Rate it';
label.classList.replace('text-body-emphasis', 'text-body-secondary');
count.textContent = '0';
err.textContent = '';
});
form.addEventListener('submit', function (e) {
e.preventDefault();
var n = current();
if (!n || !text.value.trim()) {
err.textContent = !n ? 'Please choose a rating.' : 'Please write a review.';
return;
}
err.textContent = '';
document.getElementById('crPostedStars').innerHTML = STAR.repeat(n);
document.getElementById('crPostedTitle').textContent = document.getElementById('crTitle').value.trim();
document.getElementById('crPostedText').textContent = text.value.trim();
document.getElementById('crPosted').classList.remove('d-none');
form.reset();
});
}
initReviewForm();
Built on Bootstrap 5.3.8 with plain JavaScript — no jQuery. Supports light and dark mode via data-bs-theme.