Bootstrap Cookie Consent Offcanvas Panel — Free Snippet
Bootstrap Cookie Consent Offcanvas Panel · Modals · Plain HTML, CSS & JS · Live preview
Copy & paste
Source Code
<div class="container py-5 text-center">
<p class="text-muted small">This preview auto-opens the cookie panel on load. Click "Reset" to trigger it again.</p>
<button class="btn btn-sm btn-outline-dark" id="bscookieReset">Reset & show again</button>
</div>
<div class="offcanvas offcanvas-bottom bscookie-panel" tabindex="-1" id="bscookiePanel" data-bs-backdrop="false">
<div class="offcanvas-body d-flex flex-wrap align-items-center gap-3 justify-content-between">
<p class="mb-0 small">We use cookies to improve your experience and analyze traffic. See our <a href="javascript:void(0)">Cookie Policy</a>.</p>
<div class="d-flex gap-2 flex-shrink-0">
<button class="btn btn-sm btn-outline-secondary" id="bscookieCustom">Customize</button>
<button class="btn btn-sm btn-outline-dark" id="bscookieReject">Reject all</button>
<button class="btn btn-sm btn-dark" id="bscookieAccept">Accept all</button>
</div>
</div>
<div class="bscookie-prefs d-none" id="bscookiePrefs">
<div class="offcanvas-body pt-0">
<div class="form-check form-switch"><input class="form-check-input" type="checkbox" checked disabled><label class="form-check-label small">Essential (always on)</label></div>
<div class="form-check form-switch"><input class="form-check-input" type="checkbox" id="bscookieAnalytics"><label class="form-check-label small">Analytics</label></div>
<div class="form-check form-switch"><input class="form-check-input" type="checkbox" id="bscookieMarketing"><label class="form-check-label small">Marketing</label></div>
<button class="btn btn-sm btn-dark mt-2" id="bscookieSave">Save preferences</button>
</div>
</div>
</div>.bscookie-panel { max-height: none; }
.bscookie-prefs { border-top: 1px solid #eceef1; }const panelEl = document.getElementById('bscookiePanel');
const panel = bootstrap.Offcanvas.getOrCreateInstance(panelEl, { backdrop: false });
const prefs = document.getElementById('bscookiePrefs');
function close(choice) {
console.log('cookie choice:', choice);
panel.hide();
}
document.getElementById('bscookieAccept').addEventListener('click', () => close('accept-all'));
document.getElementById('bscookieReject').addEventListener('click', () => close('reject-all'));
document.getElementById('bscookieCustom').addEventListener('click', () => prefs.classList.toggle('d-none'));
document.getElementById('bscookieSave').addEventListener('click', () => close('custom'));
document.getElementById('bscookieReset').addEventListener('click', () => { prefs.classList.add('d-none'); panel.show(); });
// A cookie banner should appear without requiring a click first — Bootstrap's
// offcanvas only opens on request, so this shows it once automatically.
panel.show();Extend with AI
Take This Snippet Further With AI
The AI prompt for this one is for Patreon supporters ($3 a month, 7 days free), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.
What's included
Features
About this UI Snippet
Bootstrap Cookie Consent Offcanvas Panel — HTML, CSS & JavaScript

A cookie banner shouldn't block the page behind a dark backdrop the way a real modal does — visitors need to keep reading while deciding. This snippet uses real Bootstrap 5.3's Offcanvas component with data-bs-backdrop="false" (also set in its JavaScript options), anchored to offcanvas-bottom so it reads as a banner rather than a full dialog, opened automatically once via panel.show() on load rather than waiting for a click.
Clicking "Customize" reveals a second section with per-category toggle switches (Essential, permanently on and disabled; Analytics; Marketing) using Bootstrap's real form-switch component — a common two-tier consent pattern: accept or reject everything in one click, or drill into specifics only if you choose to.
Step by step
How to Use
- 1Load the snippetClick the snippet in the sidebar Library tab. The panel opens automatically from the bottom, with no dark backdrop dimming the page.
- 2Click "Accept all" or "Reject all"The panel closes; check the browser console to see the logged choice.
- 3Click "Customize" insteadA preferences section expands below with per-category toggle switches.
- 4Adjust toggles and saveToggle Analytics/Marketing and click "Save preferences" — the panel closes with your custom choice.
- 5Trigger it againClick "Reset & show again" to see the panel appear fresh, preferences collapsed.
Real-world uses
Common Use Cases
Got questions?
Frequently Asked Questions
data-bs-backdrop="false" (set both as an attribute and in the JavaScript initialization options) tells Bootstrap's Offcanvas component not to render its usual dark backdrop, so the rest of the page stays fully visible and interactive.
This demo logs the choice to the console. Replace that with a real implementation — setting a cookie or localStorage flag, and only calling panel.show() on load if no prior choice is recorded.
Clicking it toggles a d-none class on a second offcanvas-body section containing three Bootstrap form-switch toggles, one of which (Essential) is permanently checked and disabled since essential cookies typically can't be opted out of under most cookie laws.
No — this is a UI pattern only. Real cookie-consent enforcement requires actually gating your analytics/marketing scripts behind the recorded consent choice, which is outside the scope of this front-end snippet.
Yes — change offcanvas-bottom to offcanvas-top on the panel element; the JavaScript logic is unaffected by which edge it's anchored to.