How to Make 3D Perspective Boxes using CSS3

Introduction

 
In this article, I will demonstrate how to make 3D perspective boxes using CSS3. CSS stands for Cascading Style Sheet,and helps design and style a form in a web application.  CSS3 is always used to create an animated form and application with the help of HTML 5 tools. It also provides advanced concepts used in web applications, such as 3D transformation, animation and user interface.
 
Step 1
 
First Open a Sublime Text editor, 
  • Open start -> Sublime Text Editor.
  • Go to file -> File -> New File
  • Name of File -> 3DBoxes.html.
Step 2 - CSS Style code
 
Note that in the below sample, I have inserted CSS3 styles inside the <style> tag. You will create CSS3 styles for the 3D cube boxes animation in the below code:
  1. <style>  
  2.       
  3. body {  
  4.     font-size100%;  
  5. }  
  6. .perspective {  
  7.     background-color: hsla(0,0%,0%,.1);  
  8.     background-image: linear-gradient(hsla(0,0%,0%,.12.5%transparent 2.5%transparent 97.5%, hsla(0,0%,0%,.197.5%),  
  9.                       linear-gradient(90deg, hsla(0,0%,0%,.12.5%transparent 2.5%transparent 97.5%, hsla(0,0%,0%,.197.5%);  
  10.     background-size3em 3em;  
  11.     box-shadow: 0 0 0 .25em hsla(0,0%,0%,.2);  
  12.     height9em;  
  13.     left: 50%;  
  14.     margin-7.5em;  
  15.     padding3em;  
  16.     positionabsolute;  
  17.     top: 50%;  
  18.     transform: perspective(500px) rotateX(45deg) rotateZ(45deg);  
  19.     transform-style: preserve-3d;  
  20.     transition: 1s;  
  21.     width9em;  
  22. }  
  23. .cube,  
  24. .cube:after,  
  25. .cube:before {  
  26.     box-shadow: inset 0 0 0 .25em hsla(0,0%,0%,.1);  
  27.     content'';  
  28.     floatleft;  
  29.     height3em;  
  30.     positionabsolute;  
  31.     width3em;  
  32. }  
  33. /* Top */  
  34. .cube {  
  35.     background-colorblue;  
  36.     positionrelative;  
  37.     transform: rotateZ(0deg) translateZ(3em);  
  38.     transform-style: preserve-3d;  
  39.     transition: .25s;  
  40. }  
  41. /* Left */  
  42. .cube:after {  
  43.     background-color#e55;  
  44.     transform: rotateX(-90deg) translateY(3em);  
  45.     transform-origin: 100% 100%;  
  46. }  
  47. /* Right */  
  48. .cube:before {  
  49.     background-color#d44;  
  50.     transform: rotateY(90deg) translateX(3em);  
  51.     transform-origin: 100% 0;  
  52. }  
  53. /* Alternate Colour */  
  54. .cube:nth-child(even) {  
  55.     background-color#fc6;  
  56. }  
  57. .cube:nth-child(even):after {  
  58.     background-color#eb5;  
  59. }  
  60. .cube:nth-child(even):before {  
  61.     background-color#da4;  
  62. }  
  63. /* Animation */  
  64. @keyframes wave {  
  65.     50% { transform: translateZ(4.5em); }  
  66. }  
  67. .cube:nth-child(1) {  
  68.     animation: wave 2s .1s ease-in-out infinite;  
  69. }  
  70. .cube:nth-child(2) {  
  71.     animation: wave 2s .2s ease-in-out infinite;  
  72. }  
  73. .cube:nth-child(3) {  
  74.     animation: wave 2s .4s ease-in-out infinite;  
  75. }  
  76. .cube:nth-child(4) {  
  77.     animation: wave 2s .3s ease-in-out infinite;  
  78. }  
  79. .cube:nth-child(5) {  
  80.     animation: wave 2s .5s ease-in-out infinite;  
  81. }  
  82. .cube:nth-child(6) {  
  83.     animation: wave 2s .7s ease-in-out infinite;  
  84. }  
  85. .cube:nth-child(7) {  
  86.     animation: wave 2s .6s ease-in-out infinite;  
  87. }  
  88. .cube:nth-child(8) {  
  89.     animation: wave 2s .8s ease-in-out infinite;  
  90. }  
  91. .cube:nth-child(9) {  
  92.     animation: wave 2s .9s ease-in-out infinite;  
  93. }  
  94. /* Labels */  
  95. input {  
  96.     displaynone;  
  97. }  
  98. label {  
  99.     background#ddd;  
  100.     cursorpointer;  
  101.     displayblock;  
  102.     font-familysans-serif;  
  103.     line-height3em;  
  104.     positionabsolute;  
  105.     right: .5em;  
  106.     text-aligncenter;  
  107.     top: 4em;  
  108.     transition: .25s;  
  109.     width4.5em;  
  110. }  
  111. label[for="left"] {  
  112.     right: 10.5em;  
  113. }  
  114. label[for="reset"] {  
  115.     right: 5.5em;  
  116. }  
  117. label[for="up"] {  
  118.     right: 5.5em;  
  119.     top: .5em;  
  120. }  
  121. label[for="down"] {  
  122.     right: 5.5em;  
  123.     top: 7.5em;  
  124. }  
  125. label:hover {  
  126.     background-color#bbb;  
  127. }  
  128. input:checked + label {  
  129.     background-color#666;  
  130.     color#fff;  
  131. }  
  132. #left:checked ~ .perspective {  
  133.     transform: perspective(500px) rotateX(45deg) rotateZ(75deg);  
  134. }  
  135. #right:checked ~ .perspective {  
  136.     transform: perspective(500px) rotateX(45deg) rotateZ(15deg);  
  137. }  
  138. #up:checked ~ .perspective {  
  139.     transform: perspective(500px) rotateX(75deg) rotateZ(45deg);  
  140. }  
  141. #down:checked ~ .perspective {  
  142.     transform: perspective(500px) rotateX(15deg) rotateZ(45deg);  
  143. }​  
  144.   
  145. </style> 
Step 3
 
Below you can see all the code for HTML5 for boxes bouncing in a 3D space. By clicking on any of the move buttons in the top-right corner, you can view this 3D model from various views. This is represented in the below HTML5 code.
 
  1. <body>  
  2.     <input type="radio" id="left" name="rotate">  
  3.         <label for="left">Left</label>  
  4.         <input type="radio" id="reset" name="rotate" checked>  
  5.             <label for="reset">Reset</label>  
  6.             <input type="radio" id="right" name="rotate">  
  7.                 <label for="right">Right</label>  
  8.                 <input type="radio" id="up" name="rotate">  
  9.                     <label for="up">Up</label>  
  10.                     <input type="radio" id="down" name="rotate">  
  11.                         <label for="down">Down</label>  
  12.                         <div class="perspective">  
  13.                             <div class="cube"></div>  
  14.                             <div class="cube"></div>  
  15.                             <div class="cube"></div>  
  16.                             <div class="cube"></div>  
  17.                             <div class="cube"></div>  
  18.                             <div class="cube"></div>  
  19.                             <div class="cube"></div>  
  20.                             <div class="cube"></div>  
  21.                             <div class="cube"></div>  
  22.                         </div>  
  23.                     </body>   
Output
 
Right-click on the sublime text window stage, a dialog box appears, select->open a new browser,
 
You must have the Firebox browser on your PC. You will see the 3D perspective boxes, as we run the application in the browser.
 
How To Make 3D Perspective Boxes Using CSS3
 
How To Make 3D Perspective Boxes Using CSS3
 
How To Make 3D Perspective Boxes Using CSS3
 
How To Make 3D Perspective Boxes Using CSS3
 
How To Make 3D Perspective Boxes Using CSS3
 

Summary

 
I hope you understood this article, in which we created 3D perspective boxes. Thanks for reading, and have a nice day  :)