How to Add a Stylish Cookie Consent Banner (GDPR Compliant)

 With privacy laws becoming stricter around the world, adding a cookie consent banner to your website is no longer optional—it is essential. Regulations such as the General Data Protection Regulation (GDPR) require websites to inform users about data collection practices and obtain their consent before storing or accessing certain types of cookies. However, compliance doesn’t mean sacrificing user experience. A well-designed, lightweight, and non-intrusive cookie banner can enhance trust while keeping your site visually appealing.

This guide will walk you through how to create a stylish and GDPR-compliant cookie consent banner that balances functionality, aesthetics, and performance.



Understanding GDPR Requirements

Before diving into design and implementation, it’s important to understand what GDPR expects from your cookie consent mechanism. In simple terms, your banner must:

  • Clearly inform users about the use of cookies
  • Allow users to accept or reject non-essential cookies
  • Avoid pre-checked consent boxes
  • Provide access to a detailed cookie policy
  • Store user preferences securely

Cookies are typically categorized into essential and non-essential. Essential cookies (like those required for login or security) do not require consent. However, analytics, marketing, and tracking cookies do.

Designing a Non-Intrusive Banner

A cookie banner should not disrupt the user experience. Instead, it should feel like a natural part of your website.

Placement matters. The most common positions are:

  • Bottom bar (least intrusive)
  • Top bar (more visible but still subtle)
  • Corner popup (modern and compact)

Keep it minimal. Avoid large overlays that block content. A slim banner with concise text works best.

Use clear language. Instead of legal jargon, opt for user-friendly messaging such as:

“We use cookies to improve your experience. You can accept or manage your preferences.”

Make buttons distinct. Include:

  • “Accept All”
  • “Reject All”
  • “Customize”

Ensure that the “Reject” option is just as visible as the “Accept” option to remain compliant.

Styling for a Modern Look

A stylish banner aligns with your website’s design rather than clashing with it.

  • Match your color palette: Use your brand colors while maintaining enough contrast for readability
  • Use subtle shadows and rounded corners: This gives a modern, polished feel
  • Choose clean typography: Ensure the text is legible across devices
  • Add smooth animations: A gentle fade-in effect can make the banner feel less abrupt

Example CSS styling:

.cookie-banner {

  position: fixed;

  bottom: 20px;

  left: 20px;

  right: 20px;

  background: #1e1e1e;

  color: #fff;

  padding: 15px 20px;

  border-radius: 10px;

  display: flex;

  justify-content: space-between;

  align-items: center;

  box-shadow: 0 4px 12px rgba(0,0,0,0.2);

  font-family: Arial, sans-serif;

}


.cookie-banner button {

  margin-left: 10px;

  padding: 8px 14px;

  border: none;

  border-radius: 6px;

  cursor: pointer;

}


.accept-btn {

  background-color: #4caf50;

  color: white;

}


.reject-btn {

  background-color: #f44336;

  color: white;

}


Implementing the Banner with JavaScript

The functionality of your cookie banner depends on tracking user choices and acting accordingly.

Here’s a simple implementation:

<div class="cookie-banner" id="cookieBanner">

  <span>We use cookies to improve your experience.</span>

  <div>

    <button class="accept-btn" onclick="acceptCookies()">Accept</button>

    <button class="reject-btn" onclick="rejectCookies()">Reject</button>

  </div>

</div>

function setCookie(name, value, days) {

  const expires = new Date(Date.now() + days * 864e5).toUTCString();

  document.cookie = name + '=' + value + '; expires=' + expires + '; path=/';

}


function getCookie(name) {

  return document.cookie.split('; ').find(row => row.startsWith(name + '='))?.split('=')[1];

}


function acceptCookies() {

  setCookie('cookieConsent', 'accepted', 365);

  document.getElementById('cookieBanner').style.display = 'none';

}


function rejectCookies() {

  setCookie('cookieConsent', 'rejected', 365);

  document.getElementById('cookieBanner').style.display = 'none';

}


window.onload = function () {

  if (!getCookie('cookieConsent')) {

    document.getElementById('cookieBanner').style.display = 'flex';

  }

};

This script stores user preferences and hides the banner after a choice is made.


Blocking Scripts Before Consent

A key GDPR requirement is that non-essential cookies should not load until the user gives explicit consent.

For example, if you use analytics scripts, delay them:

if (getCookie('cookieConsent') === 'accepted') {

  loadAnalytics();

}

function loadAnalytics() {

  const script = document.createElement('script');

  script.src = "analytics.js";

  document.head.appendChild(script);

}

This ensures tracking scripts only run after user approval.

Adding a Preferences Panel

For full compliance and better user control, consider adding a “Customize” option. This allows users to choose specific categories of cookies, such as:

  • Analytics
  • Marketing
  • Functional

A modal window can be used to display these options. Each category should include a short explanation and a toggle switch.

Optimizing Performance

A cookie banner should be lightweight and fast. Avoid heavy libraries or frameworks unless necessary.

Tips for performance:

  • Use vanilla JavaScript instead of large dependencies
  • Minify CSS and JS files
  • Load scripts asynchronously
  • Avoid excessive animations

A fast-loading banner improves user experience and prevents layout shifts.

Accessibility Considerations

Your cookie banner should be usable by everyone, including people with disabilities.

  • Ensure keyboard navigation works properly
  • Use proper contrast ratios for text and buttons
  • Add ARIA labels for screen readers
  • Avoid auto-dismiss features that remove user control

Accessibility is not just good practice—it is increasingly becoming a legal requirement.

Linking to a Cookie Policy

Your banner should include a link to a detailed cookie policy page. This page must explain:

  • What cookies you use
  • Why you use them
  • How users can manage or delete cookies

Make sure this link is easily accessible from the banner.

Testing and Compliance Checks

Once your banner is implemented, test it thoroughly:

  • Does it appear only when needed?
  • Are cookies blocked before consent?
  • Can users easily change their preferences?
  • Does it work across browsers and devices?

You may also use compliance tools or audits to verify that your implementation meets legal standards.

A cookie consent banner is more than just a legal requirement—it’s an opportunity to build trust with your users. By keeping it simple, stylish, and transparent, you can create a solution that respects privacy without compromising design or performance.

Focus on clarity, user control, and subtle design. When done right, your cookie banner will feel like a natural extension of your website rather than an annoying interruption.

Comments