Cookie consent banner generator

Copy-paste HTML, CSS and JS for a cookie banner that blocks analytics until visitors agree.

FreeRuns in your browserNo signupUpdated 2026-10-29

Not legal advice

This is a starting template, not legal advice. Laws differ by country and change; have a lawyer review the text before you publish it.

Cookie categories

Accept and Reject get identical buttons, so neither choice is pushed.

Text contrast

17.7:1

Readable: meets the 4.5:1 guideline.

Optional categories

1

Analytics

Live preview

Click Accept to see the example script run. The preview cannot keep your choice between reloads; your site will.

cookie-banner.html
<!-- Cookie consent banner. Paste before </body>. Optional scripts must be marked
     type="text/plain" data-consent="analytics" (or "marketing") so they wait for consent. -->
<style>
#cc-banner{position:fixed;z-index:2147483000;box-sizing:border-box;left:0;right:0;bottom:0;display:flex;flex-wrap:wrap;align-items:center;gap:12px 24px;padding:16px 20px;background:#111827;color:#ffffff;font:14px/1.5 system-ui,-apple-system,"Segoe UI",Roboto,sans-serif;box-shadow:0 4px 24px rgba(0,0,0,.2)}
#cc-banner[hidden]{display:none}
#cc-banner p{margin:0;flex:1 1 320px}
#cc-banner a{color:inherit;text-decoration:underline}
#cc-banner .cc-cats{display:flex;flex-wrap:wrap;gap:4px 16px;}
#cc-banner .cc-cats input{accent-color:#2563eb;margin:0 6px 0 0;vertical-align:middle}
#cc-banner .cc-actions{display:flex;flex-wrap:wrap;align-items:center;gap:8px;}
#cc-banner .cc-btn{flex:1 0 auto;min-width:110px;padding:9px 18px;border:0;border-radius:8px;background:#2563eb;color:#ffffff;font:inherit;font-weight:600;cursor:pointer}
#cc-banner .cc-save{padding:9px 4px;border:0;background:none;color:inherit;font:inherit;text-decoration:underline;cursor:pointer}
#cc-banner button:focus-visible{outline:2px solid currentColor;outline-offset:2px}
</style>
<div id="cc-banner" role="region" aria-label="Cookie consent" hidden>
  <p>We use cookies to understand how visitors use this site and to improve it. You can accept or reject optional cookies, and change your mind at any time. <a href="/cookie-policy">Cookie policy</a></p>
  <div class="cc-cats">
    <label><input type="checkbox" checked disabled> Necessary</label>
    <label><input type="checkbox" data-cat="analytics"> Analytics</label>
  </div>
  <div class="cc-actions">
    <button type="button" class="cc-btn" data-cc="reject">Reject all</button>
    <button type="button" class="cc-btn" data-cc="accept">Accept all</button>
    <button type="button" class="cc-save" data-cc="save">Save choices</button>
  </div>
</div>
<!-- Move this link into your footer. Any element with data-cc-open re-opens the banner. -->
<a href="#" data-cc-open>Cookie settings</a>
<script>
(function () {
  var KEY = "cookie-consent", VERSION = 1, CATS = ["analytics"], mem = null;
  var banner = document.getElementById("cc-banner");
  function read() {
    var c;
    try { c = JSON.parse(localStorage.getItem(KEY)); } catch (e) { c = mem; }
    return c && c.version === VERSION ? c : null;
  }
  function activate(c) {
    document.querySelectorAll('script[type="text/plain"][data-consent]').forEach(function (old) {
      if (!c[old.getAttribute("data-consent")]) return;
      var s = document.createElement("script");
      for (var i = 0; i < old.attributes.length; i++) if (old.attributes[i].name !== "type") s.setAttribute(old.attributes[i].name, old.attributes[i].value);
      s.text = old.text;
      old.parentNode.replaceChild(s, old);
    });
  }
  function open() {
    var c = read() || {};
    banner.querySelectorAll("[data-cat]").forEach(function (box) { box.checked = !!c[box.getAttribute("data-cat")]; });
    banner.hidden = false;
  }
  function save(choice) {
    var prev = read(), c = { version: VERSION, date: new Date().toISOString(), necessary: true };
    CATS.forEach(function (k) {
      var box = banner.querySelector('[data-cat="' + k + '"]');
      c[k] = choice === "accept" || (choice === "save" && !!box && box.checked);
    });
    mem = c;
    try { localStorage.setItem(KEY, JSON.stringify(c)); } catch (e) {}
    banner.hidden = true;
    // Scripts that already ran cannot be stopped, so reload when consent is withdrawn.
    if (prev && CATS.some(function (k) { return prev[k] && !c[k]; })) return location.reload();
    activate(c);
    document.dispatchEvent(new CustomEvent("cookieconsent", { detail: c }));
  }
  function init() {
    banner.addEventListener("click", function (e) { var b = e.target.closest("[data-cc]"); if (b) save(b.getAttribute("data-cc")); });
    document.addEventListener("click", function (e) { if (e.target.closest("[data-cc-open]")) { e.preventDefault(); open(); } });
    var c = read();
    if (c) activate(c); else banner.hidden = false;
  }
  if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", init); else init();
})();
</script>
How to mark a script
<!-- Before: runs as soon as the page loads -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>

<!-- After: waits until the visitor accepts analytics -->
<script type="text/plain" data-consent="analytics" async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script type="text/plain" data-consent="analytics">
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag("js", new Date());
  gtag("config", "G-XXXXXXXXXX");
</script>

About this tool

How it works and how to read the result.

Need this done for you?

Website design & development

Company websites designed and built to win trust and enquiries.

See the service

What it does

This cookie consent banner generator writes a small banner you paste into any website, with no libraries or external files. It shows your message with Accept all and Reject all buttons of the same size and colour, plus optional checkboxes for analytics and marketing cookies. The visitor's choice is saved in localStorage with a version number, and scripts you have marked as waiting for consent only run after the visitor agrees. A Cookie settings link lets visitors change their mind later. You set the text, colours, position and policy link, and test it all in the live preview.

How to use it

  1. Write the banner text and add the link to your cookie policy.
  2. Choose a bottom bar or a bottom-left box, and pick colours with enough contrast.
  3. Tick the optional categories your site actually uses.
  4. Paste the snippet just before the closing body tag on every page, and move the Cookie settings link into your footer.
  5. Mark each analytics or marketing script so it waits: change its type to text/plain and add a data-consent attribute with the category name.

How to read the result

This is a starting template, not legal advice. Laws differ by country and change; have a lawyer review the text before you publish it.

The snippet has three parts: a style block, the banner markup and a script of about 45 lines. The script looks for tags written as <script type="text/plain" data-consent="analytics">. Browsers ignore scripts with that type, so nothing runs until the visitor accepts the category; the banner then swaps in a real script with the same src and contents. A Google Analytics tag needs both of its script tags marked, as the example under the snippet shows.

The contrast figure checks your text against the background. White text on the default dark navy scores about 17.7:1. Light grey #cccccc on white scores 1.6:1 and would be hard to read. Aim for 4.5:1 or more.

The consent version starts at 1. If you add a marketing pixel later, raise it to 2. Every choice stored under version 1 is then ignored and visitors are asked again, so nobody is opted into a category they never saw.

We build sites that load tracking only after consent as part of website design and development. Pair the banner with a policy from the privacy policy generator.

Questions

Frequently asked.

Does a cookie banner make my site compliant?

Not by itself. This is a starting template, not legal advice. Laws differ by country and change; have a lawyer review the text before you publish it. Compliance also depends on which cookies you set, what your cookie policy says and whether every optional script really waits for consent.

Why are Accept and Reject the same size?

Regulators in the EU and UK have criticised banners that make Accept bright and Reject hard to find, because the design pushes people towards one answer. Giving both buttons the same style keeps the choice fair and avoids one of the most common complaints made about consent banners.

Do I need a banner if I only use necessary cookies?

Usually not. Cookies that are strictly needed for the site to work, such as a session or shopping cart cookie, generally do not need consent under EU and UK rules, though you should still explain them in your cookie policy. Analytics that sets no cookies may also avoid a banner, depending on the law.

Your next move

Like what you see? Tell us about it.

A process that takes too long, a website that undersells you, or an idea ready to take shape. Tell us where you are and we will suggest a useful next step.

India · Working with clients worldwide

We reply within a working day. No spam, ever.