Animated Add To Cart Button With HTML, CSS & JavaScript

Add To Cart Button With HTML, CSS, JS

Hey Programmers! In this post, we will make an Animated Add To Cart Button with HTML, CSS & JavaScript. In today’s digital marketplace enhancing user experience can help you to stand out from the other digital sites.

Add to add-to-cart button is an important component of any e-commerce site. This tutorial focuses on making an add-to-cart button that animates smoothly with pure HTML, CSS & JavaScript.

Also Read: How To Make Social Share Button With HTML & CSS

We will understand the code step by step and & see the important concepts like SVG Icon & Animation, and JavaScirpt Event Handling so that you can make such projects on your own.

At the end of this post, I will provide the entire source code so that you can run this project on your local device & make some changes.

Concepts Used To Make Add To Cart Button

  1. CSS Transitions
  2. SVG Icons & Animation With CSS
  3. JavaScript Event Handling that responds to clicks & triggers animations defined in CSS.

Understanding HTML To Make Add To Cart Button

<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>

Also Read: Make Back To Top Button With HTML, CSS & JavaScript

Understanding CSS To Make Add To Cart Button

body {
    margin: 0;
}
.container {
    align-items: center;
    display: flex;
    justify-content: center;
    height: 100vh;
}
.add-to-cart-button {
    background: #254ee4;
    border: none;
    border-radius: 4px;
    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.8rem;
    position: relative;
    text-transform: uppercase;
    transition: 0.4s ease;
    width: auto;
}
.add-to-cart-button:active {
    box-shadow: 0 0 0 0.2rem rgba(37, 78, 228, 0.45);
    transform: translateY(4px);
}
.add-to-cart-button:hover {
    cursor: pointer;
}

.add-to-cart-button:hover,
.add-to-cart-button:focus {
    box-shadow: 0 0 0 0.2rem rgba(37, 78, 228, 0.45);
    transform: translateY(-1px);
}
.add-to-cart-button.added {
    background: #254ee4;
    box-shadow: 0 0 0 0.2rem rgba(37, 78, 228, 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;
    -webkit-animation-delay: 0.18s;
    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;
    -webkit-animation-delay: 0.7s;
    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: #254ee4;
    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);
    }
}

Source Code Of Add To Cart Button

HTML:

<link href="https://fonts.googleapis.com/css?family=PT+Sans|Ubuntu:500&display=swap" rel="stylesheet">
  
<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>

Also Read: Trash Button With HTML, CSS & JavaScript

CSS:

<style>
    body {
        margin: 0;
    }

    .container {
        align-items: center;
        display: flex;
        justify-content: center;
        height: 100vh;
    }

    .add-to-cart-button {
        background: #254ee4;
        border: none;
        border-radius: 4px;
        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.8rem;
        position: relative;
        text-transform: uppercase;
        transition: 0.4s ease;
        width: auto;
    }

    .add-to-cart-button:active {
        box-shadow: 0 0 0 0.2rem rgba(37, 78, 228, 0.45);
        transform: translateY(4px);
    }

    .add-to-cart-button:hover {
        cursor: pointer;
    }

    .add-to-cart-button:hover, .add-to-cart-button:focus {
        box-shadow: 0 0 0 0.2rem rgba(37, 78, 228, 0.45);
        transform: translateY(-1px);
    }

    .add-to-cart-button.added {
        background: #254ee4;
        box-shadow: 0 0 0 0.2rem rgba(37, 78, 228, 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;
        -webkit-animation-delay: 0.18s;
        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;
        -webkit-animation-delay: 0.7s;
        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: #254ee4;
        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);
        }
    }
</style>

JavaScript:

<script>
    document.querySelectorAll('.add-to-cart-button').forEach(function(addToCartButton) {
        addToCartButton.addEventListener('click', function() {
            addToCartButton.classList.add('added');
            setTimeout(function(){
                addToCartButton.classList.remove('added');
            }, 2000);
        });
    });
</script>

Last Updated: June 18, 2024

By JSM Hemant

About Author

Hello, Myself Hemant. I Am Web Developer & Likes To Create Awesome Projects By Using MERN, Java, Python, C++. In This Website You Will Get Cool Projects Made With Above Technologies With Full Source Code. You Can Also Follow This Website On Author Social Media:

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories

Recent Posts