Blog URL: https://www.codingnepalweb.com/cookie-consent-box-using-html-css-javascript/

YouTube: https://youtu.be/TWxRELEDAdI



HTML


 <div class="wrapper">

    <img src="#" alt="">

    <div class="content">

      <header>Cookies Consent</header>

      <p>This website use cookies to ensure you get the best experience on our website.</p>

      <div class="buttons">

        <button class="item">I understand</button>

        <a href="#" class="item">Learn more</a>

      </div>

    </div>

  </div>


SCRIPT


<script>

    const cookieBox = document.querySelector(".wrapper"),

    acceptBtn = cookieBox.querySelector("button");

    acceptBtn.onclick = ()=>{

      //setting cookie for 1 month, after one month it'll be expired automatically

      document.cookie = "CookieBy=CodingNepal; max-age="+60*60*24*30;

      if(document.cookie){ //if cookie is set

        cookieBox.classList.add("hide"); //hide cookie box

      }else{ //if cookie not set then alert an error

        alert("Cookie can't be set! Please unblock this site from the cookie setting of your browser.");

      }

    }

    let checkCookie = document.cookie.indexOf("CookieBy=CodingNepal"); //checking our cookie

    //if cookie is set then hide the cookie box else show it

    checkCookie != -1 ? cookieBox.classList.add("hide") : cookieBox.classList.remove("hide");

  </script>


CSS


@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500;600;700&display=swap');

*{

  margin: 0;

  padding: 0;

  box-sizing: border-box;

  font-family: 'Poppins', sans-serif;

}

body{

  background: #FCBA7F;

}

.wrapper{

  position: absolute;

  bottom: 30px;

  left: 30px;

  max-width: 365px;

  background: #fff;

  padding: 25px 25px 30px 25px;

  border-radius: 15px;

  box-shadow: 1px 7px 14px -5px rgba(0,0,0,0.15);

  text-align: center;

}

.wrapper.hide{

  opacity: 0;

  pointer-events: none;

  transform: scale(0.8);

  transition: all 0.3s ease;

}

::selection{

  color: #fff;

  background: #FCBA7F;

}

.wrapper img{

  max-width: 90px;

}

.content header{

  font-size: 25px;

  font-weight: 600;

}

.content{

  margin-top: 10px;

}

.content p{

  color: #858585;

  margin: 5px 0 20px 0;

}

.content .buttons{

  display: flex;

  align-items: center;

  justify-content: center;

}

.buttons button{

  padding: 10px 20px;

  border: none;

  outline: none;

  color: #fff;

  font-size: 16px;

  font-weight: 500;

  border-radius: 5px;

  background: #FCBA7F;

  cursor: pointer;

  transition: all 0.3s ease;

}

.buttons button:hover{

  transform: scale(0.97);

}

.buttons .item{

  margin: 0 10px;

}

.buttons a{

  color: #FCBA7F;

}


Comments

Popular posts from this blog

Code Mirror

Language Model

Keyword Ideas