Design your custom cookie alert pop-up

I'm encountering an issue with implementing a cookie banner on my Bootstrap-built website. When I insert the code into my actual site, the banner appears at the top and loses its styling. How can I integrate this code correctly to ensure it functions as intended?

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Cookie Consent Banner</title>
    <!-- Google Font -->
    <link
      href="https://fonts.googleapis.com/css2?family=Poppins&display=swap"
      rel="stylesheet"
    />
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div id="cookiePopup" class="hide">
      <img src="cookie.png" />
      <p>
        Our website uses cookies to provide your browsing experience and
        relevant information. Before continuing to use our website, you agree &
        accept of our <a href="#">Cookie Policy & Privacy.</a>
      </p>
      <button id="acceptCookie">Accept</button>
    </div>
    <!-- Script -->
    <script src="script.js"></script>
  </body>
</html>

style.css

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
body {
  background-color: #f5f8ff;
}
#cookiePopup {
  background-color: #ffffff;
  position: absolute;
  font-size: 14px;
  width: 70vw;
  max-width: 42.85em;
  box-shadow: 0 0 2em rgba(5, 0, 31, 0.15);
  font-family: "Poppins", sans-serif;
  text-align: justify;
  line-height: 1.8em;
  padding: 2em 1.4em;
  border-radius: 6px;
  transition: all 0.5s ease-in;
}
#cookiePopup img {
  display: block;
  width: 3.75em;
  transform: translateZ(0);
...

script.js

let popUp = document.getElementById("cookiePopup");
//When user clicks the accept button
document.getElementById("acceptCookie").addEventListener("click", () => {
  //Create date object
  let d = new Date();
  //Increment the current time by 1 minute (cookie will expire after 1 minute)
  d.setMinutes(2 + d.getMinutes());
  //Create Cookie withname = myCookieName, value = thisIsMyCookie and expiry time=1 minute
  document.cookie = "myCookieName=thisIsMyCookie; expires = " + d + ";";
  //Hide the popup
  popUp.classList.add("hide");
  popUp.classList.remove("show");
});
//Check if cookie is already present
const checkCookie = () => {
  ...
};
//Check if cookie exists when page loads
window.onload = () => {
  setTimeout(() => {
    checkCookie();
  }, 2000);
};

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <title>Home - Brand</title>
    <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
    ...
</head>


<body id="page-top" data-bs-spy="scroll" data-bs-target="#mainNav" data-bs-offset="77" style="background: var(--bs-body-bg);font-family: Cabin, sans-serif;--bs-primary: #42DCA3;--bs-primary-rgb: 66,220,163;">
    <div id="cookiePopup" class="hide">
        <img src="assets/img/cookie.png" />
        <p>
         Our website uses cookies to provide your browsing experience and
        relevant information. Before continuing to use our website, you agree &
        accept of our <a href="#">Cookie Policy & Privacy.</a>
        </p>
        <button id="acceptCookie">Accept</button>
      </div>
    <nav class="navbar navbar-light navbar-expand-md fixed-top" id="mainNav">
        ...

</body>

</html>

Answer №1

If your banner is appearing at the top but not in the expected style, there might be an issue with the CSS;

To troubleshoot, try commenting out each of these CSS links one by one and then reloading the page to see if any of them are causing the problem:

<link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="assets/fonts/fontawesome-all.min.css">
<link rel="stylesheet" href="assets/fonts/font-awesome.min.css">
<link rel="stylesheet" href="assets/fonts/fontawesome5-overrides.min.css">
<link rel="stylesheet" href="assets/css/Footer-Dark.css">
<link rel="stylesheet" href="assets/css/Hero-Clean-Reverse.css">
<link rel="stylesheet" href="assets/css/untitled.css">

Alternatively, you can include the CSS file "style.css" within the HTML code and add the !important declaration.

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <title>Home - Brand</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="24464b4b50575056455464100a140a14">[email protected]</a>/dist/css/bootstrap.min.css"><!--EDIT for coding-->
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic&amp;display=swap">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Cabin:700&amp;display=swap">
    <link rel="stylesheet" href="assets/fonts/fontawesome-all.min.css">
    <link rel="stylesheet" href="assets/fonts/font-awesome.min.css">
    <link rel="stylesheet" href="assets/fonts/fontawesome5-overrides.min.css">
    <link rel="stylesheet" href="assets/css/Footer-Dark.css">
    <link rel="stylesheet" href="assets/css/Hero-Clean-Reverse.css">
    <link rel="stylesheet" href="assets/css/untitled.css">
    <link
      href="https://fonts.googleapis.com/css2?family=Poppins&display=swap"
      rel="stylesheet"/>
      <!--<link rel="stylesheet" href="assets/css/style.css" />-->
</head>
  
    <!-- Stylesheet on the html -->
    <style>
        * {
          padding: 0;
          margin: 0;
          box-sizing: border-box;
        }
        body {
          background-color: #f5f8ff;
        }
        #cookiePopup {
          background-color: #ffffff;
          position: absolute;
          font-size: 14px;
          width: 70vw;
          max-width: 42.85em;
          box-shadow: 0 0 2em rgba(5, 0, 31, 0.15);
          font-family: "Poppins", sans-serif;
          text-align: justify;
          line-height: 1.8em;
          padding: 2em 1.4em;
          border-radius: 6px;
          transition: all 0.5s ease-in;
        }
        #cookiePopup img {
          display: block;
          width: 3.75em;
          transform: translateZ(0);
          position: relative;
          margin: auto;
        }
        #cookiePopup p {
          text-align: center;
          margin: 1.4em 0;
        }
        #cookiePopup button {
          background-color: #6859fe;
          border: none;
          color: #ffffff;
          font-size: 1.2em;
          padding: 1em 1.4em;
          display: block;
          position: relative;
          margin: auto;
          border-radius: 5px;
        }
        #cookiePopup a {
          color: #6859fe;
        }
        .hide {
          visibility: hidden;
          bottom: 0;
          right: 2em;
        }
        .show {
          visibility: visible;
          bottom: 2em;
          right: 2em;
        }
        @media only screen and (max-width: 37.5em) {
          #cookiePopup {
            width: 100%;
          }
          .hide {
            bottom: 2em;
            right: 0;
          }
          .show {
            right: 0;
            bottom: 0;
          }
        }

    </style>
  <body id="page-top" data-bs-spy="scroll" data-bs-target="#mainNav" data-bs-offset="77" style="background: var(--bs-body-bg);font-family: Cabin, sans-serif;--bs-primary: #42DCA3;--bs-primary-rgb: 66,220,163;">
    <!--COOKIES-->
    <div id="cookiePopup" class="hide">
      <img src="cookie.png" />
      <p>
        Our website uses cookies to improve your browsing experience and provide relevant information. By continuing to use our website, you agree to our &
        accept our <a href="#">Cookie Policy & Privacy.</a>
      </p>
      <button id="acceptCookie">Accept</button>
    </div>
    
    <nav class="navbar navbar-light navbar-expand-md fixed-top" id="mainNav">
        <div class="container"><a class="navbar-brand" href="index.html">Name</a><button data-bs-toggle="collapse" class="navbar-toggler navbar-toggler-right" data-bs-target="#navbarResponsive" type="button" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation" value="Menu"><i class="fa fa-bars"></i></button>
            <div class="collapse navbar-collapse" id="navbarResponsive">
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item nav-link"><a class="nav-link active" href="kontakt.html" style="background: rgba(255,255,255,0);">Contact</a></li>
                    <li class="nav-item nav-link"></li>
                    <li class="nav-item nav-link"></li>
                </ul>
            </div>
        </div>
    </nav>
    <!--Section-->
    <section class="mb-5"></section>
    
    <!--Footer-->
    <footer class="text-center bg-dark" style="background: rgb(0,0,0);">
        ...
    </footer>
    
    <!-- Script -->
    <script>
        let popUp = document.getElementById("cookiePopup");
        //When user clicks the accept button
        document.getElementById("acceptCookie").addEventListener("click", () => {
          //Create date object
          let d = new Date();
          //Increment the current time by 1 minute (cookie will expire after 1 minute)
          d.setMinutes(2 + d.getMinutes());
          //Create Cookie withname = myCookieName, value = thisIsMyCookie and expiry time=1 minute
          document.cookie = "myCookieName=thisIsMyCookie; expires = " + d + ";";
          //Hide the popup
          popUp.classList.add("hide");
          popUp.classList.remove("show");
        });
        //Check if cookie is already present
        const checkCookie = () => {
          //Read the cookie and split on "="
          let input = document.cookie.split("=");
          //Check for our cookie
          if (input[0] == "myCookieName") {
            //Hide the popup
            popUp.classList.add("hide");
            popUp.classList.remove("show");
          } else {
            //Show the popup
            popUp.classList.add("show");
            popUp.classList.remove("hide");
          }
        };
        //Check if cookie exists when page loads
        window.onload = () => {
          setTimeout(() => {
            checkCookie();
          }, 2000);
        };
    </script>
    
        <footer class="text-center bg-dark" style="background: rgb(0,0,0);">
         ...
        </footer>
        <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1e7c71716a6d6a6c7f6e5e2a302e302e">[email protected]</a>/dist/js/bootstrap.min.js"></script><!--EDIT for coding-->
        <script src="assets/js/smart-forms.min.js"></script>
        <script src="assets/js/grayscale.js"></script>
        <script src="assets/js/script.js"></script>
    </body>
</html>

In my browser, it's working correctly

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to make a component gradually appear and disappear in an Angular application

I have developed a task management application using Angular and I wanted to implement a fading effect for each task when it is created and before it is deleted. Despite successfully applying the fade in effect at the todo-item component level, I encounter ...

Creating a reusable function in AngularJS using the factory method

Currently, I am using the AngularJS factory method to retrieve data and passing that value to controllers. In order to avoid creating separate functions, I would like to create a common function that can be utilized in all controllers. How can I effi ...

Locate a JQuery element within another JQuery element

Apologies for my poor grasp of English. I am working with HTML and JavaScript. <noindex> <h1>This is h1 in noindex</h1> <div> <h1>This is h1 in noindex and in div</h1> <div> <h1>This is h1 in noindex a ...

Running the JavaScript function '.hover' using Selenium WebDriver

My goal is to utilize Selenium WebDriver to run some javascript code from a webpage. I have come across several helpful posts on executing javascript, but I often struggle when trying to call javascript from page objects (I am still learning the terminolog ...

What sets apart `this.user.id` from `this.user = {id: ....}`?

I am puzzled by the error thrown in the code below: import { Component, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; @Component({ selector: 'app-user', templateUrl: './user.compone ...

Switching an array with a single string in MongoDB

I'm facing an issue with grouping data in mongoDB and wondering if there's a way to transform an array into a string. The objects included in $lookup are currently displayed in an array format, but I need them in a single string. Here is my code ...

Manipulate jQuery to set the table row containing empty table data cells to be lower than the row with

I am using HTML and jQuery to sort my table, including non-standard sorting with multi-tbody. The issue I am facing is that the prices (Цена) in the td are sorted ascending but not correctly. Why is this happening? Additionally, I need to move the tr&a ...

Send email without refreshing the page - no need for JQuery

I am attempting to create a form that can be submitted without refreshing the page. I am unable to utilize jQuery for this task. Despite my efforts, I haven't been able to find any solutions that do not involve jQuery. Therefore, I am seeking assistan ...

Display the network in a reaching layout, and then halt the force simulation

I'm having trouble getting my D3 network to freeze once it reaches a desired layout (when the alpha value reaches 0). I want the force to stop completely, even when a node is dragged by the user. The nodes should be rearrangeable manually but the forc ...

Changing HTML tags with Javascript: A step-by-step guide

I'm in need of assistance with updating HTML code similar to the following: <textarea name="message"></textarea> Specifically, I want to make a modification so that it appears as follows: <textarea maxlength="300" name="message">& ...

The CSS descendant selector does not seem to be functioning properly with the focus state on iOS

In order to display content using the :focus state when the parent element has focus, I have used the following CSS code: .child { display: none; } .parent:focus .child { display: block; } It's important to note that the parent element has tabindex= ...

resolved after a new promise returned nothing (console.log will output undefined)

Here is my Promise Function that iterates through each blob in Azure BlobStorage and reads each blob. The console.log(download) displays the values as JSON. However, when trying to close the new Promise function, I want the resolve function to return the ...

Encountering a Firebase error: createUser failed due to missing "password" key in the first argument in AngularJS

Recently, I started learning Angular and decided to follow an online tutorial on creating a chat application. However, I encountered an issue when trying to register with an email and password - the error message "Firebase.createUser failed: First argument ...

Learn how to effectively transfer data from $.getjson to PHP, specifically through the use of Laravel's @foreach loop

$.getJSON( 'http://localhost/media_books/index.php/new_books.json?provider_id=1&limit=99&offset=1') .done(function( json ) { $(tar).closest('.accordion').children('div').text(json); }); I have successfully obtaine ...

Is there a way to show a 'Refresh' icon in HTML without the need to download an image through HTTP?

In my HTML/JavaScript app project, I am looking to incorporate a 'refresh' symbol without having to load an image through HTTP requests. Are there reliable methods that can achieve this across all major browsers? I came across the Unicode value: ...

Issues with retrieving information between AJAX and PHP (and vice versa)

I have a script that sends the value of a text input from an HTML form to emailform.php. This PHP file then adds the data to a .txt file. The issue I'm facing is setting the value of $email in the PHP file to match that of the HTML input field. Curren ...

Break free from the confines of a single quote in jQuery within

I'm currently facing a challenge with my ajax function that calls a PHP class to retrieve code stored in the variable $var. The issue arises from having single quotes within the $var string, causing JavaScript to throw an error. Is there a more effic ...

In JavaFX, when adjusting the transparency of a popup window, only the brightness changes while the solid color remains unchanged, effectively concealing

Is it possible to have a transparent popup window (errorDialog.fxml) on top of the main window (mainWindow.fxml) with 50% opacity while still showing the content of the main window underneath? My attempts at setting the background color of the overlay insi ...

Steps to correct alignment in a numbered list when the dot is removed using CSS

Currently, I'm working on developing a rule page for a game. The specific layout requirements dictate that the rules be presented in an ordered list without periods following each number: 1 Rule one 2 Rule two 3 Rule three To achieve this format ...

The reduce function is displaying an undefined result

Check out this code snippet: const filterByType = (target , ...element) => { return element.reduce((start, next) =>{ if(typeof next === target){ start.push(next) } } , []) } I'm trying to achieve a specific g ...