Add to cart button | add to cart html css javascript - codewithrandom

Add to cart button | add to cart html css javascript

Add to cart button | add to cart html css javascript


Welcome🎉 to Code With Random blog. In this blog, we learn that how we create an Add cart. We use HTML & CSS and javascript for this Add to cart. Hope you enjoy our blog so let's start with a basic HTML structure for an Add to cart button.

HTML code 

 <div class="container">  
   <button class="add-to-cart-button">  
     <svg class="add-to-cart-box box-1" width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><rect width="24" height="24" rx="2" fill="#ffffff"/></svg>  
     <svg class="add-to-cart-box box-2" width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><rect width="24" height="24" rx="2" fill="#ffffff"/></svg>  
     <svg class="cart-icon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="#ffffff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="9" cy="21" r="1"></circle><circle cx="20" cy="21" r="1"></circle><path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"></path></svg>  
     <svg class="tick" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0V0z"/><path fill="#ffffff" d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zM9.29 16.29L5.7 12.7c-.39-.39-.39-1.02 0-1.41.39-.39 1.02-.39 1.41 0L10 14.17l6.88-6.88c.39-.39 1.02-.39 1.41 0 .39.39.39 1.02 0 1.41l-7.59 7.59c-.38.39-1.02.39-1.41 0z"/></svg>  
     <span class="add-to-cart">Add to cart</span>  
     <span class="added-to-cart">Added to cart</span>  
   </button>  
 </div>  
There is all HTML code for the Add to cart. Now, you can see output without CSS, then we write CSS for Add to cart.
output
Add to cart button | add to cart html css javascript

CSS/scss Code

 .add-to-cart-button {  
   background: #e6a247;  
   border: none;  
   border-radius: 4px;  
   -webkit-box-shadow: 0 3px 13px -2px rgba(0,0,0,.15);  
   box-shadow: 0 3px 13px -2px rgba(0,0,0,.15);  
   color: #ffffff;  
   display: flex;  
   font-family: 'Ubuntu', sans-serif;  
   justify-content: space-around;  
   min-width: 195px;  
   overflow: hidden;  
   outline: none;  
   padding: 0.7rem;  
   position: relative;  
   text-transform: uppercase;  
   transition: 0.4s ease;  
   width: auto;  
   &:active {  
     -webkit-box-shadow: 0 0 0 0.2rem rgba(252, 186, 3,.45);  
     box-shadow: 0 0 0 0.2rem rgba(252, 186, 3,.45);  
     -webkit-transform: translateY(4px);  
     transform: translateY(4px);  
   }  
   &:hover {  
     cursor: pointer;  
   }  
   &:hover,  
   &:focus {  
     -webkit-box-shadow: 0 0 0 0.2rem rgba(252, 186, 3,.45);  
     box-shadow: 0 0 0 0.2rem rgba(252, 186, 3,.45);  
     -webkit-transform: translateY(-1px);  
     transform: translateY(-1px);  
   }  
   &.added {  
     background: #2fbf30;  
     -webkit-box-shadow: 0 0 0 0.2rem rgba(11, 252, 3, 0.45);  
     box-shadow: 0 0 0 0.2rem rgba(11, 252, 3, 0.45);  
     .add-to-cart {  
       display: none;  
     }  
     .added-to-cart {  
       display: block;  
     }  
     .cart-icon {  
       animation: drop 0.3s forwards;  
       -webkit-animation: drop 0.3s forwards;  
       animation-delay: 0.18s;  
     }  
     .box-1,  
     .box-2 {  
       top: 18px;  
     }  
     .tick {  
       animation: grow 0.6s forwards;  
       -webkit-animation: grow 0.6s forwards;  
       animation-delay: 0.7s;  
     }  
   }  
 }  
 .add-to-cart,  
 .added-to-cart {  
   margin-left: 36px;  
 }  
 .added-to-cart {  
   display: none;  
   position: relative;  
 }  
 .add-to-cart-box {  
   height: 5px;  
   position: absolute;  
   top: 0;  
   width: 5px;  
 }  
 .box-1,  
 .box-2 {  
   transition: 0.4s ease;  
   top: -8px;  
 }  
 .box-1 {  
   left: 23px;  
   transform: rotate(45deg);  
 }  
 .box-2 {  
   left: 32px;  
   transform: rotate(63deg);  
 }  
 .cart-icon {  
   left: 15px;  
   position: absolute;  
   top: 8px;  
 }  
 .tick {  
   background: #146230;  
   border-radius: 50%;  
   position: absolute;  
   left: 28px;  
   transform: scale(0);  
   top: 5px;  
   z-index: 2;  
 }  
 @-webkit-keyframes grow {  
   0% {-webkit-transform: scale(0);}  
   50% {-webkit-transform: scale(1.2);}  
   100% {-webkit-transform: scale(1);}  
 }  
 @keyframes grow {  
   0% {transform: scale(0);}  
   50% {transform: scale(1.2);}  
   100% {transform: scale(1);}  
 }  
 @-webkit-keyframes drop {  
   0% {-webkit-transform: translateY(0px);}  
   100% {-webkit-transform: translateY(1px);}  
 }  
 @keyframes drop {  
   0% {transform: translateY(0px);}  
   100% {transform: translateY(1px);}  
 }  
 /* Page style */  
 .container {  
   align-items: center;  
   display: flex;  
   height: 100vh;  
   justify-content: center;  
   width: 100%;  
 }  

Now we have completed our CSS section,  Here is our updated output CSS.

output
Add to cart button | add to cart html css javascript

Now add javascript for when we click on add to cart, so its  pop-up added to cart!

Javascript code 

 addToCartButton = document.querySelectorAll(".add-to-cart-button");  
 document.querySelectorAll('.add-to-cart-button').forEach(function(addToCartButton) {  
   addToCartButton.addEventListener('click', function() {  
     addToCartButton.classList.add('added');  
     setTimeout(function(){  
       addToCartButton.classList.remove('added');  
     }, 2000);  
   });  
 });  
Final output

Add to cart button | add to cart html css javascript

Now we have completed our javascript section,  Here is our updated output with javascriptHope you like the Add to cart. you can see output video and project screenshots. See our other blogs and gain knowledge in front-end development. Thank you 🙏💕

In this post, we learn how to create an Add to cart using simple HTML & CSS and javascript. If we made a mistake or any confusion, please drop a comment to reply or help you in easy learning.

Written by - Code With Random/Anki 

1 Comments

Post a Comment

Previous Post Next Post