Cookie Banner Generator
Choose your cookie categories, position and colour, and copy three small files. The banner remembers each visitor's choice and only loads optional scripts after they agree.
Runs in your browser. Nothing you type is uploaded. This is a template, not legal advice. Check it with a lawyer for your country.
Result
Visitors can accept, reject or choose analytics. Nothing optional loads until they agree.
- Save the JS as cookie-consent.js at your site root, or change the src in the HTML to where you put it.
- Replace the example data-src addresses with your real analytics or ad scripts, and remove the normal script tags for them.
- Inline optional scripts also work: keep type="text/plain" and data-consent, and put the code inside. Your site's Content Security Policy must allow it.
- The choice is stored under the key cookie-consent. A cookie-consent event fires with the choices, so other code can react.
- Raise VERSION in the JS when you add a category, so returning visitors are asked again.
- Reject as easy as acceptEU and UK regulators expect a reject option as visible as accept. Both buttons here have the same style.
- Nothing before consentCheck your page source: tag managers and pixels must not load until the visitor agrees.
- Match your privacy policyList the same categories and tools in your privacy policy and link it from the banner.
- Other lawsCalifornia and other US states focus on opting out of sale or sharing, which may need a separate link. Check the rules where your visitors are.
<!-- Cookie banner: paste before the closing </body> tag -->
<div id="cc-banner" class="cc-banner cc-bottom" role="dialog" aria-label="Cookie preferences" hidden>
<p class="cc-text">Acme uses cookies to make this site work, and, if you agree, for analytics. <a href="/privacy">Privacy policy</a></p>
<div class="cc-prefs" data-cc-prefs hidden>
<label><input type="checkbox" checked disabled> <span>Necessary <small>Needed for the site to work. Always on.</small></span></label>
<label><input type="checkbox" data-cc-category="analytics"> <span>Analytics <small>Helps us understand how the site is used.</small></span></label>
<div class="cc-actions"><button type="button" class="cc-btn" data-cc-save>Save choices</button></div>
</div>
<div class="cc-actions">
<button type="button" class="cc-btn" data-cc-reject>Reject optional</button>
<button type="button" class="cc-btn" data-cc-accept>Accept all</button>
<button type="button" class="cc-btn cc-plain" data-cc-customize>Choose</button>
</div>
</div>
<script src="/cookie-consent.js" defer></script>
<!-- Optional scripts: give them type="text/plain" and data-consent so they wait for consent. -->
<script type="text/plain" data-consent="analytics" data-src="https://analytics.example.com/script.js"></script>
<!-- Put this link in your footer so visitors can change their choice later. -->
<a href="#" data-cookie-settings>Cookie settings</a>
/* Cookie banner */
.cc-banner {
--cc-accent: #2563eb;
position: fixed; bottom: 0; left: 0; right: 0;
z-index: 9999;
box-sizing: border-box;
padding: 16px 20px;
background: #ffffff;
color: #1f2328;
border: 1px solid #d8dee4;
box-shadow: 0 6px 24px rgba(0, 0, 0, 0.12);
font: 14px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
display: flex; flex-wrap: wrap; gap: 12px 20px; align-items: center;
}
.cc-banner[hidden] { display: none; }
.cc-text { margin: 0; flex: 1 1 320px; }
.cc-text a { color: var(--cc-accent); }
.cc-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.cc-prefs { flex-basis: 100%; display: grid; gap: 8px; }
.cc-prefs[hidden] { display: none; }
.cc-prefs label { display: flex; gap: 8px; align-items: flex-start; }
.cc-prefs small { display: block; color: #59636e; }
.cc-btn {
font: inherit; cursor: pointer;
padding: 8px 14px; border-radius: 6px;
border: 1px solid var(--cc-accent);
background: var(--cc-accent); color: #ffffff;
}
.cc-btn.cc-plain { background: transparent; color: var(--cc-accent); }
.cc-btn:focus-visible { outline: 2px solid var(--cc-accent); outline-offset: 2px; }
// Cookie consent for Acme. No dependencies.
// Saves the visitor's choice in their browser and loads optional scripts only after consent.
(function () {
var KEY = 'cookie-consent';
var VERSION = 1; // raise this when you add a new category, to ask everyone again
var CATEGORIES = ["analytics"];
var banner = document.getElementById('cc-banner');
if (!banner) return;
function on(selector, fn) {
var el = banner.querySelector(selector);
if (el) el.addEventListener('click', fn);
}
function read() {
try {
var saved = JSON.parse(localStorage.getItem(KEY) || 'null');
return saved && saved.version === VERSION ? saved : null;
} catch (e) { return null; }
}
function save(choices) {
try {
localStorage.setItem(KEY, JSON.stringify({ version: VERSION, date: new Date().toISOString(), choices: choices }));
} catch (e) {}
}
// Turn waiting <script type="text/plain" data-consent="..."> tags into real scripts.
function load(choices) {
document.querySelectorAll('script[type="text/plain"][data-consent]').forEach(function (old) {
if (!choices[old.getAttribute('data-consent')] || old.hasAttribute('data-loaded')) return;
var s = document.createElement('script');
if (old.getAttribute('data-src')) s.src = old.getAttribute('data-src');
else s.text = old.text;
old.setAttribute('data-loaded', '');
old.parentNode.insertBefore(s, old.nextSibling);
});
window.dispatchEvent(new CustomEvent('cookie-consent', { detail: choices }));
}
function every(value) {
var c = {};
CATEGORIES.forEach(function (k) { c[k] = value; });
return c;
}
function fromBoxes() {
var c = {};
CATEGORIES.forEach(function (k) {
var box = banner.querySelector('[data-cc-category="' + k + '"]');
c[k] = !!(box && box.checked);
});
return c;
}
function choose(choices) {
save(choices);
banner.hidden = true;
load(choices);
}
function open() {
var saved = read();
CATEGORIES.forEach(function (k) {
var box = banner.querySelector('[data-cc-category="' + k + '"]');
if (box) box.checked = !!(saved && saved.choices[k]);
});
banner.hidden = false;
}
on('[data-cc-accept]', function () { choose(every(true)); });
on('[data-cc-reject]', function () { choose(every(false)); });
on('[data-cc-save]', function () { choose(fromBoxes()); });
on('[data-cc-customize]', function () {
var prefs = banner.querySelector('[data-cc-prefs]');
if (prefs) prefs.hidden = !prefs.hidden;
});
document.querySelectorAll('[data-cookie-settings]').forEach(function (link) {
link.addEventListener('click', function (e) { e.preventDefault(); open(); });
});
var saved = read();
if (saved) load(saved.choices);
else banner.hidden = false;
})();
How it works
- Blocking scripts
- Optional scripts are written as type="text/plain", which browsers ignore. After consent, the JS swaps each one for a real script tag.
- Stored choice
- The choice is saved in the visitor's browser with a version number. Nothing is sent to a server, and the banner does not show again until you change the version.
- Changing a choice
- Any link with data-cookie-settings reopens the banner with the current choices ticked.
Questions
Do I need a cookie banner?
If you have EU or UK visitors and use analytics, ads or other non-essential cookies, you usually need consent first. A site with only necessary cookies often needs just a notice.
Can I skip the banner with privacy-friendly analytics?
Some cookieless analytics tools are designed to avoid consent rules, but the rules differ by country. Check your tool's documentation and your local regulator's guidance.
Does this banner make my site compliant?
No banner can promise that on its own. Compliance depends on what your scripts do, what your policy says and how you treat the choice. This gives you a fair, working base.
Get CapyLaunch Weekly
New indie launches and the week in AI tools and models, every Monday. Free, and you can unsubscribe anytime.
Your launch, this week. Free.
A reviewed listing, a permanent page with a link to your site, and feedback from makers who actually care.