HTML | Flip a card

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.