How to Create a Rotating 3D Cube Using HTML and CSS- DesignUKnow

How to Create a Rotating 3D Cube Using HTML and CSS

Creating 3D animations using just HTML and CSS is both fun and powerful. In this blog post, we'll walk through how to build a rotating 3D cube using only basic web technologies — no JavaScript required!

We’ll break down the code step-by-step and explain the key concepts like perspective, transform-style, and @keyframes animation.






📌 Final Result Preview

You'll end up with a smooth, continuously rotating 3D cube that looks something like this in your browser:

+--------+
/ /|
+--------+ |
| | +
| |/
+--------+

It will rotate on both X and Y axes using CSS animation, giving it a realistic 3D effect.


🧱 Step 1: Basic HTML Structure

The first part of our project is the HTML structure . We use six <div> elements to represent each face of the cube.

html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Rotating 3D Cube</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="cube-container">
<div class="cube">
<!-- Faces -->
<div class="face front"></div>
<div class="face back"></div>
<div class="face left"></div>
<div class="face right"></div>
<div class="face top"></div>
<div class="face bottom"></div>
</div>
</div>
</body>
</html>

🎨 Step 2: Styling with CSS

Now let’s dive into the CSS that brings the cube to life.

🔁 Reset & Page Setup

This ensures consistent styling across browsers and centers the cube on the screen.

css
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
}

👁️ 3D Perspective

To enable 3D effects, we set the perspective property on the container. This simulates depth for all child elements.

css
.cube-container {
perspective: 1000px;
}

🧱 The Cube Itself

Here's where we define the size, transform style, and start the animation.

css
.cube {
position: relative;
width: 200px;
height: 200px;
transform-style: preserve-3d; /* Keeps children in 3D space */
animation: rotateCube 5s infinite linear; /* Rotate forever */
}
  • transform-style: preserve-3d: Ensures child elements (faces) are rendered in 3D.
  • animation: Applies a continuous rotation using keyframe animation.

🟦 Each Face of the Cube

Each face is absolutely positioned and moved outward in 3D space using translateZ.

css
.face {
position: absolute;
width: 200px;
height: 200px;
background-color: red;
border: 2px solid whitesmoke;
}

Positioning Each Face in 3D Space

We use rotateX, rotateY, and translateZ to place each face correctly.

css
.front { transform: translateZ(100px); }
.back { transform: rotateY(180deg) translateZ(100px); }
.left { transform: rotateY(-90deg) translateZ(100px); }
.right { transform: rotateY(90deg) translateZ(100px); }
.top { transform: rotateX(-90deg) translateZ(100px); }
.bottom { transform: rotateX(90deg) translateZ(100px); }

These transformations create the illusion of depth and form a complete cube.


🌀 Animation: Rotating the Cube

Finally, we define an animation using @keyframes to rotate the cube smoothly around both the X and Y axes.

css
@keyframes rotateCube {
0% {
transform: rotateX(0deg) rotateY(0deg);
}
100% {
transform: rotateX(360deg) rotateY(360deg);
}
}

This creates a full 3D rotation loop.


<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Rotating 3D Cube</title> <link rel="stylesheet" href="styles.css"> </head> <body> <div class="cube-container"> <div class="cube"> <!-- Front face --> <div class="face front"></div> <!-- Back face --> <div class="face back"></div> <!-- Left face --> <div class="face left"></div> <!-- Right face --> <div class="face right"></div> <!-- Top face --> <div class="face top"></div> <!-- Bottom face --> <div class="face bottom"></div> </div> </div> </body> <style type="text/css">/* Reset default margins and paddings */ * { margin: 0; padding: 0; box-sizing: border-box; } body { display: flex; justify-content: center; align-items: center; height: 100vh; background-color: #f0f0f0; } /* Container for the cube */ .cube-container { perspective: 1000px; /* Creates a 3D perspective */ } /* The cube itself */ .cube { position: relative; width: 200px; height: 200px; transform-style: preserve-3d; /* Ensures 3D child elements are rendered correctly */ animation: rotateCube 5s infinite linear; /* Applies the rotation animation */ } /* Each face of the cube */ .face { position: absolute; width: 200px; height: 200px; background-color: red; border: 2px solid; whitesmoke; } /* Positioning each face */ .front { transform: translateZ(100px); } .back { transform: rotateY(180deg) translateZ(100px); } .left { transform: rotateY(-90deg) translateZ(100px); } .right { transform: rotateY(90deg) translateZ(100px); } .top { transform: rotateX(-90deg) translateZ(100px); } .bottom { transform: rotateX(90deg) translateZ(100px); } /* Animation to rotate the cube */ @keyframes rotateCube { 0% { transform: rotateX(0deg) rotateY(0deg); } 100% { transform: rotateX(360deg) rotateY(360deg); } }</style> </html>



✅ Summary

Concept
Purpose
perspective
Enables 3D depth on the parent container
transform-style: preserve-3d
Keeps child elements in 3D space
translateZ()
Pushes faces outward to form a cube
rotateX(),rotateY()
Rotates faces to correct orientation
@keyframes
Animates continuous rotation

💡 Tips & Enhancements

  • 🎨 Add color : Give each face a different color to better visualize the cube.
  • 🕹️ Interactivity : Add hover effects or pause on click.
  • 🖥️ Responsive design : Use vw units so the cube scales nicely on mobile.
  • 🧮 More complex shapes : Try creating pyramids, cylinders, or even text using similar techniques.

🚀 Try It Yourself!

Just copy the full HTML and CSS into an .html file and open it in your browser. You should see a beautiful, rotating 3D cube made entirely with HTML and CSS.

Let me know if you'd like to:

  • Add textures or images to each face
  • Control rotation with mouse movement
  • Export as a downloadable template

Happy coding! 🧑‍💻




Post a Comment

Previous Post Next Post