Creating a 3D Card Flip Animation using HTML and CSS Tutorial

In this tutorial, you can learn to create a simple 3D Card Flip Animation using HTML and CSS. The tutorial aims to provide students and beginners with a reference for learning some CSS tricks to develop a creative user interface and functionality of a website or web application. Here, I will be providing simple web page scripts that demonstrate the creation of a card container or elements with a 3D flip animation.

What is Flip Animation?

Flip Animation is an animation that contains an element with a front and back face. This animation flip or rotates the element from front to back face or vice-versa. It works like turning the piece of paper or plane sheet to view the other side or the backside. This animation is useful to a website for designing card elements with contents in both the front and back faces.

How to create a 3D Flip Animation using CSS?

The 3D Flip Animation can be easily achieved using CSS or Cascading Stylesheet. CSS comes with multiple useful properties for creating HTML Elements more creative and interactive. Using the CSS transform, transform-style, and perspective properties, we can create a 3D Flip Animation. The transform property will be used for flipping or rotating the HTML element using either rotateX() or rotateY() values. For the transform-style, we can set the element and its container to preserve-3d which indicates that child elements should be positioned in the 3D space. The perspective property will be used to determine the distance between the element plane and the user. Check out the sample web page scripts that I provided below to have a better idea of how to create a 3D Flip Animation.

Sample Web Application

Here are the scripts of a simple web application page that demonstrate the creation of a simple 3D Flip Animation. The web page contains a single card with 2 faces (front and back) and has different content. The card will flip when the user will hover the card to display the design and content of the back face.

HTML

Here is the HTML file script of the web page which contains the HTML elements of the card. It also contains the elements used for the web page layout. Save the file as index.html.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <meta charset="UTF-8">
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>CSS - Card Flip Animation</title>
  7. <link rel="preconnect" href="https://fonts.googleapis.com">
  8. <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  9. <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200" />
  10. <link rel="stylesheet" href="style.css">
  11. </head>
  12. <div id="wrapper">
  13. <div class="page-title">CSS - Card Flip Animation</div>
  14. <hr style="margin:auto; width:25px">
  15. <div id="card-wrapper">
  16. <div class="card">
  17. <div class="card-front">
  18. <div class="card-title">Hover me to read more.</div>
  19. </div>
  20. <div class="card-back">
  21. <div class="card-title">Sample Content Title</div>
  22. <hr width="15px">
  23. <div class="card-description">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam vestibulum, velit ac porttitor cursus, sapien enim ultrices ante, aliquam interdum risus dui vel mauris.</div>
  24. </div>
  25. </div>
  26. </div>
  27. </div>
  28. </body>
  29. </html>

Stylesheet

Here's the CSS file script that contains the page elements' styles. The script is used for designing the layout of the web page and contents. It also contains the script for adding a 3D Flip Animation to the card element when the user hovers over the element. Save this file as style.css and make sure to load the file in the index file.

  1. @import url('https://fonts.googleapis.com/css2?family=Rubik&display=swap');
  2. @import url('https://fonts.googleapis.com/css2?family=Courgette&family=Secular+One&display=swap" rel="stylesheet');
  3. @import url('https://fonts.googleapis.com/css2?family=Satisfy&display=swap" rel="stylesheet');
  4. :root{
  5. --secular-font: 'Secular One', sans-serif;
  6. --satisfy-font: 'Satisfy', cursive;
  7. }
  8. *{
  9. box-sizing: border-box;
  10. }
  11. body *{
  12. font-family: 'Rubik', sans-serif;
  13. }
  14. /**
  15. Page Design
  16. */
  17. body,
  18. html{
  19. height: 100%;
  20. width: 100%;
  21. margin: 0;
  22. padding: 0;
  23. }
  24. body{
  25. background-color: #363636;
  26. }
  27. /* Page Wrapper */
  28. #wrapper{
  29. width: 100%;
  30. padding: 3em 5em;
  31. }
  32. .page-title{
  33. font-size: 2.5rem;
  34. font-weight: 500;
  35. color: #fff;
  36. letter-spacing: 3px;
  37. font-family: var(--secular-font);
  38. text-align: center;
  39. }
  40. /* Card Wrapper and containers */
  41. #card-wrapper{
  42. width: 100%;
  43. padding: 2em 0;
  44. }
  45. .card{
  46. position: relative;
  47. width: 250px;
  48. height: 300px;
  49. margin:auto;
  50. display: flex;
  51. transform-style: preserve-3d;
  52. perspective: 500px;
  53. }
  54. .card .card-front,
  55. .card .card-back{
  56. position: absolute;
  57. width: 100%;
  58. height: 100%;
  59. background:#fff;
  60. display:flex;
  61. flex-direction: column;
  62. align-items: center;
  63. justify-content: center;
  64. backface-visibility: hidden;
  65. transform-style: preserve-3d;
  66. transition: all 1s ease;
  67. border: 1px solid #a5a5a5;
  68. box-shadow: 2px 2px 6px #f1f1f140;
  69. }
  70. .card .card-front:before,
  71. .card .card-back:before{
  72. content: "";
  73. position:absolute;
  74. height: 2px;
  75. bottom: -27px;
  76. background: linear-gradient(95deg, transparent, #222121, transparent);
  77. box-shadow: 1px 1px 5px #1e1e1e;
  78. transition: all 1s ease-in-out;
  79. }
  80. .card .card-front:before{
  81. width: 100%;
  82. }
  83. .card .card-back:before{
  84. width: 0%;
  85. }
  86. .card .card-front{
  87. /* background-image: linear-gradient(to top, #5ee7df 0%, #b490ca 100%); */
  88. background-image: url(./bg.jpg);
  89. background-repeat: no-repeat;
  90. background-size: cover;
  91. transform: rotateY(0deg);
  92. }
  93. .card .card-front .card-title{
  94. font-size: 1rem;
  95. letter-spacing: 4px;
  96. font-weight: 600;
  97. font-family: var(--secular-font);
  98. color: #fff;
  99. text-shadow: 1px 1px 3px #000;
  100. text-align: center;
  101. }
  102. .card .card-back{
  103. transform: rotateY(180deg);
  104. padding: 1em;
  105. }
  106. .card .card-back .card-title{
  107. font-size: 1.2rem;
  108. font-family: var(--satisfy-font);
  109. text-align: center;
  110. }
  111. .card .card-back .card-description {
  112. font-size: .9rem;
  113. font-family: var(--satisfy-font);
  114. text-align: center;
  115. color: #3c3c3c;
  116. letter-spacing: 1.5px;
  117. }
  118. /*
  119. Flip Animation when Card is Hover
  120. */
  121. .card:hover .card-front{
  122. transform: rotateY(-180deg)
  123. }
  124. .card:hover .card-front:before{
  125. width: 0%;
  126. }
  127.  
  128. .card:hover .card-back{
  129. transform: rotateY(0deg)
  130. }
  131. .card:hover .card-back:before{
  132. width: 100%;
  133. }

Kindly replace the image path used as the background of the front face of the card.

Snapshots

Here are the snapshots of the sample web page using the given HTML and CSS scripts above.

Card Front Face

3D Card Flip Animation using CSS

Card Back Face

3D Card Flip Animation using CSS

The Card when Flipping

3D Card Flip Animation using CSS

DEMO

The following is the overall result of the scripts I provided:

3D Card Flip Animation using CSS

There you go! I have also provided the complete source code zip file that I created for the demonstration of this tutorial on this site. The zip file is free to download. Feel free to download and do some experiments to enhance your skills or design capabilities. The download button is located below this tutorial's content.

That's the end of this tutorial! I hope this Creating a 3D Card Flip Animation using HTML and CSS Tutorial will help you with what you are looking for and will be useful for your current and future web application projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)

Add new comment