Telegram Group Join Now
Create Add To Cart Button Using HTML, CSS, & JavaScript
Welcome to Code With Random blog. In this blog, we will explore how to create an Add to cart button. This is a very common component of every e-commerce website.
To get started with this project all you need is a basic understanding of HTML, CSS(SCSS), and Javascript. Hope you enjoy our blog so let’s start with a basic HTML structure for a create Add to cart button Using HTML, CSS, & JavaScript.
We’ll show you how to Create Add To Cart Button Using HTML, CSS, & JavaScript with complete Source code available for you so you just copy and paste it into your project.
ADVERTISEMENT
100+ JavaScript Projects With Source Code ( Beginners to Advanced)
HTML Code For Add To Cart Button
<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>
Portfolio Website using HTML and CSS (Source Code)
There is all the HTML code for the Add to cart. To create the required figures we are using <svg> HTML tag. To know more about SVG (Scalable Vector Graphics) read our blog for an easy introduction.
Given below is the output for our HTML file without styling it with CSS.
output
Restaurant Website Using HTML and CSS
CSS Code For Add To Cart Button
To style our project we are using SCSS, which is a more advanced variant of CSS. SCSS provides a lot more features than regular CSS, it uses a lot of similar syntax to CSS, thus making it easier to learn.
.add-to-cart-button { background: #e6a247; border: none; border-radius: 4px; -webkit-box-shadow: 0 3px 13px -2px rgba(0, 0, 0, 0.15); box-shadow: 0 3px 13px -2px rgba(0, 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; } .add-to-cart-button:active { -webkit-box-shadow: 0 0 0 0.2rem rgba(252, 186, 3, 0.45); box-shadow: 0 0 0 0.2rem rgba(252, 186, 3, 0.45); -webkit-transform: translateY(4px); transform: translateY(4px); } .add-to-cart-button:hover { cursor: pointer; } .add-to-cart-button:hover, .add-to-cart-button:focus { -webkit-box-shadow: 0 0 0 0.2rem rgba(252, 186, 3, 0.45); box-shadow: 0 0 0 0.2rem rgba(252, 186, 3, 0.45); -webkit-transform: translateY(-1px); transform: translateY(-1px); } .add-to-cart-button.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-button.added .add-to-cart { display: none; } .add-to-cart-button.added .added-to-cart { display: block; } .add-to-cart-button.added .cart-icon { animation: drop 0.3s forwards; -webkit-animation: drop 0.3s forwards; animation-delay: 0.18s; } .add-to-cart-button.added .box-1, .add-to-cart-button.added .box-2 { top: 18px; } .add-to-cart-button.added .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.
Gym Website Using HTML and CSS With Source Code
output
JavaScript Code For Add To Cart Button
Now add javascript for when we click on add to cart, to create a pop-up display “added to cart”!
Here, we are using the setTimeout() function that will execute the callback function inside it 2 seconds after you click Add to Cart – button.
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); }); });
Create A Travel/Tourism Website Using HTML and CSS
Final Output Add To Cart Button
ADVERTISEMENT
50+ HTML, CSS & JavaScript Projects With Source Code
ADVERTISEMENT
Conclusion
Hope you like the Add to cart button HTML. you can see the output video and project screenshots. See our other blogs and gain knowledge in front-end development.
ADVERTISEMENT
In this post, we learn how to Create Add To Cart Button Using HTML, CSS, & JavaScript. If we made a mistake or any confusion, please drop a comment to reply or help you in easy learning.
ADVERTISEMENT
Thank You And Keep Learning!!!
ADVERTISEMENT
Written by – Code With Random/Anki
sir javascript part is not working