spinning 3d cube in html
- User
spinning 3d cube in html
- Assistant
Below is a simple example of a spinning 3D cube using only HTML and CSS. You can copy the following code into an HTML file and open it in your browser to see the cube in action.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Spinning 3D Cube</title> <style> body { margin: 0; height: 100vh; display: flex; align-items: center; justify-content: center; background: #111; color: #fff; font-family: sans-serif; } .scene { width: 200px; height: 200px; perspective: 600px; /* Creates a 3D space by setting a perspective distance */ } .cube { position: relative; width: 100%; height: 100%; transform-style: preserve-3d; transform: translateZ(-100px); /* Centering the cube */ animation: spin 5s infinite linear; } .cube__face { position: absolute; width: 200px; height: 200px; border: 2px solid #fff; background: rgba(255, 255, 255, 0.2); box-shadow: inset 0 0 30px rgba(0, 0, 0, 0.2); display: flex; align-items: center; justify-content: center; font-weight: bold; font-size: 1.2em; } /* Position each face in the cube */ .cube__face--front { transform: rotateY( 0deg) translateZ(100px); } .cube__face--back { transform: rotateY(180deg) translateZ(100px); } .cube__face--right { transform: rotateY( 90deg) translateZ(100px); } .cube__face--left { transform: rotateY(-90deg) translateZ(100px); } .cube__face--top { transform: rotateX( 90deg) translateZ(100px); } .cube__face--bottom { transform: rotateX(-90deg) translateZ(100px); } /* Animation for spinning cube */ @keyframes spin { from { transform: translateZ(-100px) rotateX(0deg) rotateY(0deg); } to { transform: translateZ(-100px) rotateX(360deg) rotateY(360deg); } } </style> </head> <body> <div class="scene"> <div class="cube"> <div class="cube__face cube__face--front">Front</div> <div class="cube__face cube__face--back">Back</div> <div class="cube__face cube__face--right">Right</div> <div class="cube__face cube__face--left">Left</div> <div class="cube__face cube__face--top">Top</div> <div class="cube__face cube__face--bottom">Bottom</div> </div> </div> </body> </html>How It Works
-
Scene & Perspective:
The.scenecontainer gets a fixed size and aperspectiveproperty, which defines the distance between the viewer and the object (cube) to create the 3D effect. -
The Cube:
The.cubeelement usestransform-style: preserve-3dso that its children (cube faces) are rendered in 3D space. It is also animated using thespinkeyframes to rotate continuously. -
Cube Faces:
Each face (specified by the.cube__faceclasses) is absolutely positioned and moved into place usingtranslateZalong with rotations so that each face forms one side of the cube. -
Animation:
The@keyframes spinrotates the cube continuously along both the X and Y axes.
Feel free to adjust the size, colors, or animation duration as needed for your projects. Enjoy your spinning cube!
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.