Skip to content

Cookie Preferences Modal

Bootstrap 5 cookie consent banner with a preferences modal: category switches, collapsible cookie tables, accept, reject and save actions, stored safely in localStorage.

Also known as: cookie consent banner, gdpr cookie popup, cookie settings dialog, cookie preferences panel

Cookie Preferences Modal live demo

Open in new tab

Bootstrap 5 code (HTML, CSS, JS)

A Bootstrap 5 cookie consent pattern made of a floating banner and a full preferences modal. Visitors can accept everything, reject optional cookies in one click or open a dialog with per-category switches and cookie tables, which is the layout most privacy regulations expect.

How it works

The root .ckp-consent contains a short demo page with a Cookie settings button, the fixed .ckp-banner in the bottom-left corner and the #ckp-modal Bootstrap modal. Inside the modal each .ckp-cat row has an icon, a description and a form-switch whose input carries data-ckp-category. The "Strictly necessary" switch is checked and disabled. Each optional category has a .ckp-more toggle that opens a Bootstrap collapse with a .ckp-table listing cookie names, purposes and lifetimes.

Every button with data-ckp-action calls apply(): accept turns all categories on, reject turns them off and save reads the current switches. The result is stored as JSON under the key from data-ckp-key, the banner is hidden, .ckp-status shows a summary and a ckp:change event is dispatched with the choices in event.detail. When the modal opens (show.bs.modal), the switches are reset to the saved state so unsaved toggles are discarded. On the next visit a stored choice hides the banner straight away. Storage access is wrapped in try/catch, so the component still works where localStorage is blocked.

Customization

  • --ckp-cocoa and --ckp-cream on .ckp-consent in style.css: change the button color and the modal and page background.
  • --ckp-accent and --ckp-accent-rgb: recolor the active switches, links and focus rings.
  • data-ckp-key on .ckp-consent: change the storage key, for example to reset consent after you update your policy.
  • Add a category by copying a .ckp-cat item with a new data-ckp-category value and adding a label for it to LABELS in script.js.
  • max-width: 520px in .ckp-banner: widen the banner, or set right: 1.5rem in the min-width: 576px media query to stretch it into a full-width bar.
  • Listen for ckp:change on the page and load analytics or marketing scripts only when event.detail.analytics or event.detail.marketing is true.
  • Remove the .ckp-mock placeholder block when you drop the component into a real page.

Use cases

  • EU-facing websites: visitors accept all or reject optional cookies in one click
  • Privacy-focused sites: a preferences modal has per-category switches and collapsible cookie tables
  • Online stores: saved choices in localStorage keep the banner from reappearing on every visit
  • Blogs and publishers: a floating banner stays compact until the visitor opens detailed settings

Accessibility

The banner is a labelled role="region". The modal uses aria-labelledby and aria-describedby, and Bootstrap traps focus and restores it on close; if the banner trigger has disappeared, focus moves to the Cookie settings button. Switches use role="switch", real labels and aria-describedby pointing to their descriptions. The .ckp-more toggles expose aria-expanded and aria-controls, and the tables have hidden captions and scope="col" headers. .ckp-status is a live region. The banner slide-in and chevron rotation are disabled under prefers-reduced-motion.

Usage notes

Requires Bootstrap 5 with its JS bundle (added automatically) and Bootstrap Icons. If you use more than one instance, give each modal a unique id and update the matching data-bs-target values.

Cookie Preferences Modal at a glance

Framework
Bootstrap 5
Code
HTML, CSS, JavaScript
JavaScript
Vanilla JavaScript, no jQuery
Dependencies
Bootstrap 5, Bootstrap Icons
Layout
Responsive, works on mobile
Category
Cookie Banners
License
Free for personal and commercial use

Questions about this template

Can I use the Cookie Preferences Modal 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?

Load Bootstrap 5 and Bootstrap Icons from a CDN (the links are under Requirements). 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

Add these to your page (already included in the download):

  • <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
  • <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
  • <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
Cookie Preferences Modal – free Bootstrap 5 cookie banner template
Cookie Preferences Modal · free Bootstrap 5 cookie banner template

More Cookie Banners

Cookie Consent Banner – free Tailwind CSS cookie banner template

Cookie Consent Banner

Tailwind CSS cookie consent banner with accept, reject and a settings dialog with category switches, saved to localStorage with a consent event.