The Feast

Home cooking recipes for everyday dinners and easy entertaining

Cookie Pop Up Banner: Build and Style a Consent Notice

· 5 min read · Baking and Desserts

A cookie pop up banner suits any website that sets non-essential cookies or uses analytics and ad scripts, and it works best when it appears on first visit, before those scripts load. It is served as a fixed overlay at the bottom or top of the page, with Accept, Reject and a link to a settings or policy page. The hard part is not the styling; it is wiring the banner so tracking scripts stay blocked until the visitor makes a choice.

Decide What the Banner Must Do Before Writing Code

Two kinds of banner exist. A notice-only banner just informs; a consent banner gates scripts behind a choice. If your site loads analytics, advertising pixels, embedded videos or chat widgets that set cookies, you need the gating kind, because consent rules in the EU, UK and similar jurisdictions require an active opt-in before non-essential cookies run. List every third-party script on your site and mark each as necessary or optional; that list becomes your banner's logic.

Build the Banner HTML and CSS

Keep the markup minimal: a container, a short message, two buttons and a link. A typical banner is about 60-90 pixels tall on desktop and full-width on screens under 768 pixels, with 16-20 pixels of padding and body text of at least 14 pixels so it stays readable.

<div id="cookie-banner" role="dialog" aria-live="polite" hidden>
  <p>We use cookies for analytics and to improve the site.
     <a href="/cookie-policy">Cookie policy</a></p>
  <button id="accept">Accept</button>
  <button id="reject">Reject</button>
</div>
#cookie-banner {
  position: fixed; bottom: 0; left: 0; right: 0;
  display: flex; gap: 12px; align-items: center;
  padding: 16px 20px; background: #1f2937; color: #fff;
  z-index: 9999;
}

Two accessibility details matter: role="dialog" with aria-live="polite" announces the banner to screen readers, and both buttons must be reachable by keyboard. Never auto-dismiss the banner after a timer, and never make Reject harder to reach than Accept; equal prominence for both choices is a common legal expectation.

Save the decision in localStorage so returning visitors are not asked again. On page load, read the stored value before any optional script runs. The standard pattern is to keep tracking script tags out of the initial HTML entirely and inject them only after consent.

const KEY = 'cookie-consent';
function loadOptional() {
  // inject analytics/ad scripts here, e.g. by
  // creating script elements and appending to head
}
if (localStorage.getItem(KEY)) {
  document.getElementById('cookie-banner').hidden = true;
  if (localStorage.getItem(KEY) === 'accepted') loadOptional();
} else {
  document.getElementById('cookie-banner').hidden = false;
}
document.getElementById('accept').onclick = () => {
  localStorage.setItem(KEY, 'accepted');
  document.getElementById('cookie-banner').hidden = true;
  loadOptional();
};
document.getElementById('reject').onclick = () => {
  localStorage.setItem(KEY, 'rejected');
  document.getElementById('cookie-banner').hidden = true;
};

Give the stored value a version, for example cookie-consent-v1, so that when you add a new tracking script later you can bump the version and re-show the banner for a fresh consent. If you need consent that works across subdomains, use a scoped first-party cookie instead of localStorage, since localStorage is per-origin.

Gate Each Script by Category, Not All at Once

Once you have more than one optional script, split consent into categories such as analytics and marketing. Store a JSON object rather than a single string, and give each script a data-consent attribute so the loader can match scripts to granted categories. This also lets you build a settings view later where visitors change individual categories. A practical rule: necessary scripts (session, cart, security) load immediately; everything else waits.

Let Visitors Change Their Mind After the Banner Closes

Consent must be withdrawable, so add a persistent "Cookie settings" link in your site footer that reopens the banner or settings panel. When a visitor withdraws consent, delete the optional cookies you can identify by name and stop injecting the scripts on future page loads. Note the limit: you cannot reliably delete third-party cookies set in the browser's third-party storage, which is why the primary action is to stop the scripts from loading again.

When the hand-built version stops being enough

A hand-built banner is fine for a small site with one or two analytics scripts. Move to a consent management platform when you need automatic script blocking for many tags, per-region behaviour (banner in the EU, none where consent is not required), consent records for proof, or integration with an advertising tag manager. Most platforms work by giving you a wrapper around your script tags and a small loader snippet in the page head.

What changes with a platform

You typically replace each tracking script's direct tag with the platform's placeholder, and the platform injects the real script only for granted categories. Your own CSS can still restyle the standard banner in many platforms, so the visual design you built above is not wasted.

Check the Banner Against the Rules That Apply to You

Requirements differ by jurisdiction and change over time, so verify current rules for your audience rather than relying on any static list. The checks that come up most often: no pre-ticked checkboxes, Reject as easy as Accept, a link to a plain-language cookie policy that names the cookies and their purpose, and consent captured before any optional cookie is set. Test with your browser's dev tools open on the Network tab and the Application tab: load the page, confirm no optional cookies or tracking requests fire before you click, and confirm they fire only after Accept. Also test keyboard-only navigation and a hard refresh after each choice. Menu names and storage APIs described here are long-standing and stable, but exact labels can differ by browser version.

In this article
  1. Decide What the Banner Must Do Before Writing Code
  2. Build the Banner HTML and CSS
  3. Store the Choice and Reload Scripts on Consent
  4. Gate Each Script by Category, Not All at Once
  5. Let Visitors Change Their Mind After the Banner Closes
  6. Swap the DIY Banner for a Consent Platform When
  7. Check the Banner Against the Rules That Apply to You