DEV Community

Onyeneke
Onyeneke

Posted on • Edited on

Creating a 3D Card Flip Animation Gallery with CSS 3D Transforms and JavaScript

Boosting user engagement on your website is crucial, and one effective way to achieve this is by integrating 3D card flip animations. In this tutorial, we'll explore why adding this feature is worthwhile and provide a straightforward guide on how to implement it using CSS 3D transforms and JavaScript.

Prerequisites

Before diving into the tutorial, ensure you have a basic understanding of HTML, CSS, and JavaScript. Familiarity with CSS transitions and transformations will be beneficial.

Why Choose 3D Card Flip Animation?

Adding a 3D flip animation to your cards can bring several advantages to your website:

  • Appealing Visuals: The 3D card flip adds a touch of elegance, capturing users' attention and leaving a lasting impression.
  • Interactivity Boost: Enhance user interaction with this dynamic feature, making your content more engaging and impressive.
  • Product and Portfolio Showcase: Perfect for displaying products, services, or portfolios, the 3D flip animation offers a clear view of both sides, presenting information without clutter.
  • Storytelling and Information Display: Utilize the flip animation to share additional information or tell a story, creating an engaging experience for users.
  • Gamification Touch: For interactive or educational websites, the 3D card flip adds a fun element. Users can enjoy flipping cards to find hidden content, making the experience enjoyable.
  • Modernizing UI Design: Stay current with design trends by incorporating 3D animations. It not only enhances user experience but also contributes to a contemporary website design.

Setting Up the HTML Structure

Begin by creating the HTML structure for your gallery. Each card will be represented by a div element, and the entire gallery will be wrapped in a container.


<body>
    <div class="scene scene--card">
      <div class="card">
        <div class="card__face card__face--front">front</div>
        <div class="card__face card__face--back">back</div>
      </div>
    </div>
    <p>Click card to flip.</p>
  </body>

Enter fullscreen mode Exit fullscreen mode

The HTML structure consists of a

element containing a with the class "scene scene--card." Inside this div, there is another div with the class "card." Within the card div, there are two child divs with classes "card__face card__face--front" and "card__face card__face--back," representing the front and back faces of the card, respectively. Additionally, there's a paragraph indicating that the card can be clicked to flip.

Adding the CSS Styles

body { font-family: sans-serif; }

.scene {
  width: 200px;
  height: 260px;
  border: 1px solid #CCC;
  margin: 40px 0;
  perspective: 600px;
}

.card {
  position: relative;
  width: 100%;
  height: 100%;
  cursor: pointer;
  transform-style: preserve-3d;
  transform-origin: center right;
  transition: transform 1s;
}

.card.is-flipped {
  transform: translateX(-100%) rotateY(-180deg);
}

.card__face {
  position: absolute;
  width: 100%;
  height: 100%;
  line-height: 260px;
  color: white;
  text-align: center;
  font-weight: bold;
  font-size: 40px;
  backface-visibility: hidden;
}

.card__face--front {
  background: #00C5FF;
}

.card__face--back {
  background:#773400;
  transform: rotateY(180deg);
}

transform: translateX(-100%) rotateY(-180deg); moves the card to the left (-100% along the X-axis) and rotates it 180 degrees around the Y-axis, simulating a flip.

Now, lets add some interactive JavaScript code:


var card = document.querySelector(".card");
card.addEventListener("click", function () {
  card.classList.toggle("is-flipped");
});

  • The JavaScript code selects the card element with the class "card" using document.querySelector(".card").
  • I added an event listener to the card, listening for the 'click' event.
  • When the card is clicked, the function toggles the "is-flipped" class on and off using classList.toggle("is-flipped"). The class triggers the rotation animation defined in the CSS, causing the card to flip.

Result

Here is the final result:

Conclusion

By combining the power of CSS 3D transforms and JavaScript, you can create an engaging 3D card flip animation gallery that adds a layer of interactivity to your web projects. Experiment with different styling and customization to achieve the desired visual impact. This tutorial serves as a starting point for enhancing user experience through creative and immersive web animations.

Top comments (0)