Open In App

HTML | Flip a card

Last Updated : 22 Aug, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

Flip cards are the cards in your website that will flip when you hover your mouse over them. There will be information, links or images in the back face of the card which will be visible when you hover over the cards.

In this article, you’re going to learn how to make Flip cards on your website using only HTML and CSS.

HTML file for card:




<div class="card">
    <h2>HTML | Flip a Card</h2>
      
    <div class="card-inner">
        <div class="card-front">
              
            <!-- adding picture in the card -->
            <img src="" style="width:250px;height:250px;">
        </div>
          
        <!-- Decorate card back side -->
        <div class="card-back">
            <h1>GeeksforGeeks</h1>
            <p>A Computer Science Portal for Geeks</p>
              
            <a href="">
               Try our ide
            </a>
        </div>
    </div>
</div>


Decorating the front and back of the card using CSS: Built the structure of both faces of the card in the HTML part. Now we need to hide the back face initially. So we do that by backface-visibility: hidden property of CSS and this to the card-front and card-back classes so that when the front face is visible the back face is hidden and vice-versa. The key is to use the transform property of CSS and rotate the element by 180 degree i.e. transform: rotateY(180deg).




<style> .card {
    width: 250px;
    height: 250px;
    perspective: 1000px;
}
.card-inner {
    position: relative;
    text-align: center;
    transition: transform 0.6s;
    transform-style: preserve-3d;
}
.card:hover .card-inner {
    transform: rotateY(180deg);
}
.card-front, .card-back {
    position: absolute;
    backface-visibility: hidden;
}
.card-back {
    transform: rotateY(180deg);
}
</style>


Note: You can rotate your cards as much as possible depending on the transform: rotateY(180deg). If you decrease the deg and increase the deg rotation is depending on that also you can flip the card vertically.

Example: This example uses transform: rotateY(180deg) property to flip/rotate the image. If you change the axis Y to X then cards will change the flip direction.




<!DOCTYPE html>
<html>
  
<head>
    <meta name="viewport" 
            content="width=device-width, initial-scale=1">
  
    <!-- CSS code -->
    <style>
        h1 {
            color: green;
        }
        a {
            color: purple;
            text-decoration: none;
        }
        .card {
            background-color: transparent;
            width: 250px;
            height: 250px;
            perspective: 1000px;
        }
        .card-inner {
            position: relative;
            width: 100%;
            height: 100%;
            text-align: center;
            transition: transform 0.6s;
            transform-style: preserve-3d;
            box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
        }
        .card:hover .card-inner {
            transform: rotateY(180deg);
        }
        .card-front, .card-back {
            position: absolute;
            width: 100%;
            height: 100%;
            backface-visibility: hidden;
        }
        .card-front {
            background-color: #bbb;
            color: black;
        }
        .card-back {
            background-color: white;
            transform: rotateY(180deg);
        }
    </style>
</head>
  
<body>
    <center>
        <div class="card">
            <h2>HTML | Flip Card</h2>
            <div class="card-inner">
                <div class="card-front">
                    <img src=
                            style="width:250px;height:250px;">
                </div>
                  
                <div class="card-back">
                    <h1>GeeksforGeeks</h1>
                    <p>A Computer Science Portal for Geeks</p>
                      
                    <a href="https://ide.geeksforgeeks.org/tryit.php">
                        Try our ide
                    </a>
                </div>
            </div>
        </div>
    </center>
</body>
  
</html>


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads