Open In App

Create a Animated Product Card using HTML CSS & JavaScript.

In this article, we dive into creating a simple animated product card using HTML, CSS, and JavaScript. Here we’re going to create a product card with the product name and price. On hover, it flips and shows detailed information along with a buy button. These cards not only make the website visually appealing but also give more information regarding products within the allocated space on the web page.

Preview:

On hover

Prerequisites:

Approach:

Project Structure:

Project structure

Example: Below example helps you create an animated product card using HTML, CSS and JavaScript.




// script.js
const card = document.querySelector('.card');
 
card.addEventListener('mouseover', function () {
  card.style.transform = 'scale(1.2)';
});
 
card.addEventListener('mouseout', function () {
  card.style.transform = 'scale(1)';
});




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,
                                   initial-scale=1.0">
    <link rel="stylesheet" href="styles.css">
    <script src="script.js" defer></script>
    <title>Animated Product Card</title>
</head>
 
<body>
    <div class="card">
        <div class="card-inner">
            <div class="card-front">
                <h2>GeeksForGeeks</h2>
                <p>$99.99</p>
            </div>
            <div class="card-back">
                <h2>GeeksForGeeks</h2>
                <p>$99.99</p>
                <p>Product Details:</p>
                <p>You can show extra info here...</p>
                <button class="buy-button">Buy Now</button>
            </div>
        </div>
    </div>
</body>
 
</html>




/* styles.css */
body {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  margin: 0;
}
 
.card {
  width: 300px;
  height: 400px;
  perspective: 1000px;
}
 
.card-inner {
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.5s;
}
 
.card:hover .card-inner {
  transform: rotateY(180deg);
}
 
.card-front,
.card-back {
  width: 100%;
  height: 100%;
  position: absolute;
  backface-visibility: hidden;
}
 
.card-front {
  background: #f2f2f2;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  transition: transform 0.5s;
}
 
.card-back {
  background: #1abc9c;
  transform: rotateY(180deg);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  transition: transform 0.5s;
  opacity: 0;
  pointer-events: none;
}
 
.card:hover .card-back {
  opacity: 1;
  pointer-events: auto;
}
 
.buy-button {
  animation: pulse 1s infinite;
}
 
@keyframes pulse {
  0% {
    transform: scale(1);
  }
 
  50% {
    transform: scale(1.2);
  }
 
  100% {
    transform: scale(1);
  }
}

Output:

Output


Article Tags :