FlipcardTutorials

How to create a flip card animation

How to create a flip card animation

How to create a flip card animation

Before We Start

Every developer needs clean and modern elements to develop their websites, It contains pictures and a user interface element within a website. The most important one and the first section that the user sees when the user enters a website. How to create a flip card animation

So if you are a person who wishes to develop a website, you have to know how to create that properly.

How to create a flip card animation

We are here to solve your problem. In this article we discuss how to create these elements. Before we start please read the below articles which easier you to understand the process, if you are new to development.

Step 1 – Add HTML

It’s too easy and simple. Just copy and paste below code in your HTML editor between <body> </body> tag.

<div class='container'></div>

Step 2 – Add CSS

Copy and paste below code in your HTML editor between <style></style> tag.

<style>
.container{
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
  height: 100vh;
  font-family: Courier new, monospace;
  perspective: 1000px;
}

.title{
  font-size: 3.5rem;
}

.flip-card-container{
  height: 20rem;
  width: 12rem;
  cursor: pointer;
}

.flip-card{
  width: 100%;
  height: 100%;
  transition: all 0.5s ease-in-out;
  transform-style: preserve-3d; // This is very useful property of CSS if we want to use 3D type of animations.
}

.flip-card div{
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  margin: 0;
  width: 100%;
  height: 100%;
  border-radius: 0.7rem;
  box-shadow: 2rem 3rem 4rem;
  backface-visibility: hidden;
  font-weight: bold;
  font-size: 1.4rem;
}

.card-front{
  background: linear-gradient(45deg, #2193b0, #6dd5ed);
}

.card-back{
  background: linear-gradient(45deg, #56ab2f, #a8e063);
  transform: rotateY(180deg);    
  // As we are using preserve-3d as transform style on the parent conatainer we flip our backside card using rotateY. It is important to have preserve-3d on parent container for this to work.
}

.message{
  font-size: 1.5rem;
  font-weight: 400;
}
</style>

Step 3 – Add JavaScript

And also copy and paste below JavaScript code between <script></script> tag.

<script>
const container = document.querySelector('.container');
const title = document.createElement('h1');
const titleTextContent = document.createTextNode('Flip Card');
title.className = 'title';
title.appendChild(titleTextContent);
container.appendChild(title);

// Create a container for cards.
const flipCardContainer = document.createElement('div');
flipCardContainer.className = 'flip-card-container';
container.appendChild(flipCardContainer);

// We are creating nested div so we can use preserve-3d.
const flipCard = document.createElement('div');
flipCard.className = 'flip-card';
flipCardContainer.appendChild(flipCard);

const cardFront = document.createElement('div');
const cardBack = document.createElement('div');
cardFront.className = 'card-front';
cardBack.className = 'card-back';
flipCard.appendChild(cardFront);
flipCard.appendChild(cardBack);

const frontTextContent = document.createTextNode('I am Front!');
const backTextContent = document.createTextNode('I am Back!');

cardFront.appendChild(frontTextContent);
cardBack.appendChild(backTextContent);

const message = document.createElement('div');
const messageTextContent= document.createTextNode('Hover over the card !');
message.className = 'message';
message.appendChild(messageTextContent);
container.appendChild(message);

// This also can be achieved by using a hover effect on card container.
flipCardContainer.addEventListener('mouseenter', flipIn);
flipCardContainer.addEventListener('mouseleave', flipOut);

// If you want only flip remove the translateZ property.
function flipIn(){
  flipCard.style.transform = 'rotateY(180deg) translateZ(100px)';
}

function flipOut(){
  flipCard.style.transform = 'rotateY(0deg) translateZ(0)';
}


</script>

At the end we will have something like this. You can change fonts, colors, backgrounds and all things that you want. Enjoy it.

Your Output

How to create a flip card animation

Your Output

How to create a flip card animation

Video Tutorial

Please watch this video to better understand this tutorial and don’t forget to subscribe to our channel.


Help others to find out about this article on Social Media sites. If you have any doubt or any problem, don’t hesitate to contact us. Thereafter we will be able to help you and also make sure you bookmark our site on your browser.

Related Articles

Leave a Reply

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

Back to top button