spinning 3d cube in html
- User
spinning 3d cube in html
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
spinning 3d cube in html
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Of course! Creating a 3D spinning cube is a fantastic way to learn about CSS 3D transforms. It involves some HTML for the structure and CSS for the positioning, 3D perspective, and animation.
Here is the complete code and a detailed breakdown of how it works.
You can copy and paste this directly into an HTML file (.html) and open it in your browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Spinning Cube</title>
<style>
/* Basic styling for the page */
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #2c3e50;
margin: 0;
overflow: hidden;
}
/* 1. The Scene: A container to create the 3D perspective */
.scene {
width: 200px;
height: 200px;
perspective: 600px; /* This creates the 3D effect. The smaller the value, the more extreme the perspective. */
}
/* 2. The Cube: The main container for the faces, which will be animated */
.cube {
width: 100%;
height: 100%;
position: relative;
transform-style: preserve-3d; /* This is crucial! It tells the browser to render children in 3D space. */
animation: spin 10s infinite linear; /* Apply the animation */
}
/* 3. The Faces: Common styling for all 6 faces of the cube */
.face {
position: absolute; /* Position all faces at the same starting point */
width: 200px;
height: 200px;
background-color: rgba(26, 188, 156, 0.7);
border: 2px solid #ecf0f1;
color: white;
font-size: 2rem;
font-family: Arial, sans-serif;
font-weight: bold;
display: flex;
justify-content: center;
align-items: center;
/* Helps with performance by not rendering the back of a face when it's not visible */
backface-visibility: hidden;
}
/* 4. Positioning Each Face in 3D Space */
/* The 'translateZ' value is half of the width/height (200px / 2 = 100px) */
.front {
transform: rotateY(0deg) translateZ(100px);
}
.back {
/* Rotate 180 degrees to face the back, then push it out */
transform: rotateY(180deg) translateZ(100px);
background-color: rgba(231, 76, 60, 0.7);
}
.right {
transform: rotateY(90deg) translateZ(100px);
background-color: rgba(52, 152, 219, 0.7);
}
.left {
transform: rotateY(-90deg) translateZ(100px);
background-color: rgba(241, 196, 15, 0.7);
}
.top {
transform: rotateX(90deg) translateZ(100px);
background-color: rgba(155, 89, 182, 0.7);
}
.bottom {
transform: rotateX(-90deg) translateZ(100px);
background-color: rgba(46, 204, 113, 0.7);
}
/* 5. The Animation: Defining the spin */
@keyframes spin {
from {
transform: rotateX(0deg) rotateY(0deg);
}
to {
transform: rotateX(360deg) rotateY(360deg);
}
}
</style>
</head>
<body>
<div class="scene">
<div class="cube">
<div class="face front">Front</div>
<div class="face back">Back</div>
<div class="face right">Right</div>
<div class="face left">Left</div>
<div class="face top">Top</div>
<div class="face bottom">Bottom</div>
</div>
</div>
</body>
</html>
The HTML is simple. We have a container for the entire 3D world (<div class="scene">), a container for the cube itself (<div class="cube">), and then six divs for the cube's faces.
<div class="scene">
<div class="cube">
<div class="face front">Front</div>
<div class="face back">Back</div>
<div class="face right">Right</div>
<!-- etc. -->
</div>
</div>
This is where all the action happens.
Step 1: The Scene
.scene {
width: 200px;
height: 200px;
perspective: 600px;
}
perspective: This is the most important property for creating a 3D environment. It defines how "deep" the scene is. A smaller value (e.g., 300px) creates a more dramatic, "in-your-face" perspective, while a larger value makes it look flatter.Step 2: The Cube Wrapper
.cube {
width: 100%;
height: 100%;
position: relative;
transform-style: preserve-3d;
animation: spin 10s infinite linear;
}
position: relative: This is necessary so that the absolutely positioned faces inside are positioned relative to this cube container.transform-style: preserve-3d: This is the magic key. It tells the browser that the children of this element (.face divs) should be positioned in 3D space, not flattened onto their parent's plane.animation: This applies our spinning animation, which we'll define later.Step 3: Styling the Faces
.face {
position: absolute;
width: 200px;
height: 200px;
/* ... other visual styles */
}
position: absolute: This makes all six faces stack on top of each other in the top-left corner of the .cube container before we move them into place with transform.Step 4: Positioning Each Face in 3D
This is the core logic. We use the transform property to move and rotate each face into its correct position.
Imagine the center of the cube is at coordinates (0, 0, 0).
rotateY(90deg): Rotates the element 90 degrees around the vertical Y-axis.rotateX(90deg): Rotates the element 90 degrees around the horizontal X-axis.translateZ(100px): Pushes the element 100px "out of the screen" towards you along the Z-axis.Since our cube is 200px wide, we need to push each face out from the center by half that amount, which is 100px.
transform: translateZ(100px); (Just push it forward)transform: rotateY(90deg) translateZ(100px); (Rotate it to the right, then push it forward)transform: rotateY(180deg) translateZ(100px); (Rotate it to face backward, then push it forward)Step 5: The Animation
@keyframes spin {
from {
transform: rotateX(0deg) rotateY(0deg);
}
to {
transform: rotateX(360deg) rotateY(360deg);
}
}
spin.from), the cube has no rotation.to), it has completed a full 360-degree rotation around both the X and Y axes.animation property on .cube then applies this continuously (infinite) over 10 seconds with a steady pace (linear).background-color to background-image: url('path/to/your/image.jpg'); and set background-size: cover;.animation property. For example, animation: spin 20s infinite linear; will make it slower. You can also change the to transform in @keyframes to rotateY(360deg) to only spin on one axis./* In .cube class, remove the animation property and add this: */
transition: transform 1s;
/* Then add a hover state */
.scene:hover .cube {
transform: rotateX(360deg) rotateY(360deg);
}