Understanding Cookie Consent in Modern Web Development
As internet usage expands across Pakistan, businesses operating local e-commerce stores, corporate portals, and service websites must align with global digital standards. When visitors land on your site, storing tracking cookies or user data requires explicit, transparent consent. Implementing a clean cookie consent banner ensures user privacy while maintaining compliance with international data standards.
At netbit tech, our developers frequently build user interfaces that respect user privacy without slowing down page performance. In this tutorial, we will walk through building a lightweight, accessible cookie consent banner using vanilla HTML, CSS, and modern JavaScript.
Step 1: Structuring the HTML Markup
Start by creating a simple markup structure that sits at the bottom of your webpage. Using semantic HTML ensures screen readers can easily interpret the consent notice.
Add the following snippet just before the closing tag:
We use cookies to improve your browsing experience. By continuing to use our site, you accept our Privacy Policy.
Accept
Decline
Step 2: Styling the Banner with CSS
A good UI element should remain non-intrusive yet clearly visible. Style your banner as a fixed element anchored to the bottom of the viewport so it does not block primary content.
.cookie-banner {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
background-color: #1a1a1a;
color: #ffffff;
padding: 16px 24px;
box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.15);
z-index: 9999;
display: none;
font-family: inherit;
}
.cookie-content {
max-width: 1200px;
margin: 0 auto;
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
gap: 16px;
}
.cookie-content p {
margin: 0;
font-size: 14px;
line-height: 1.5;
}
.cookie-content a {
color: #4da3ff;
text-decoration: underline;
}
.cookie-actions {
display: flex;
gap: 12px;
}
.btn-accept, .btn-reject {
padding: 8px 16px;
font-size: 14px;
font-weight: 600;
border: none;
border-radius: 4px;
cursor: pointer;
}
.btn-accept {
background-color: #007bff;
color: #ffffff;
}
.btn-reject {
background-color: transparent;
color: #cccccc;
border: 1px solid #666666;
}
Step 3: Handling User Preferences with JavaScript
To ensure the banner does not reappear on every page load, use the browser's localStorage API to remember the user's choice. If no preference exists, display the banner.
document.addEventListener("DOMContentLoaded", function() {
const banner = document.getElementById("cookie-banner");
const acceptBtn = document.getElementById("accept-cookies");
const rejectBtn = document.getElementById("reject-cookies");
const consentStatus = localStorage.getItem("netbit_cookie_consent");
if (!consentStatus) {
banner.style.display = "block";
}
acceptBtn.addEventListener("click", function() {
localStorage.setItem("netbit_cookie_consent", "accepted");
banner.style.display = "none";
// Initialize analytics or tracking scripts here
});
rejectBtn.addEventListener("click", function() {
localStorage.setItem("netbit_cookie_consent", "rejected");
banner.style.display = "none";
// Disable non-essential tracking scripts here
});
});
Best Practices for Pakistani Web Platforms
- Keep it Lightweight: Avoid heavy third-party plugins that increase page load time, especially for mobile users on cellular networks across Pakistan.
- Ensure Accessibility: Allow keyboard navigation using the
Tabkey so users can easily select buttons without a mouse. - Clear Documentation: Link your banner directly to an updated privacy policy page detailing what data your web application collects.
Professional Web Development Services by NetBit Software House
Building secure, compliant, and lightning-fast web applications requires specialized technical knowledge. Whether you run a startup in Khuzdar or an established enterprise in major Pakistani cities, netbit khuzdar delivers robust digital solutions tailored to your business needs.
From custom software development, responsive web applications, and mobile apps to reliable IT infrastructure, networking (Cat6 cabling, fiber patch cords, LiteBeam, PowerBeam), and CCTV security systems, netbit tech is your trusted partner for complete technological growth. Contact our expert team at netbit software house today to discuss your next project and elevate your online presence.
Reader comments.
Share your question, suggestion or experience related to this article.
No comments yet
There are no approved comments on this article yet. You can be the first person to leave one.