body { /* Set the size of the sides of the cube here */ --S: 150px; /* Set perspective */ perspective: 800px; } /* Common properties for all faces: position absolute and size */ .face { position: absolute; width: var(--S); height: var(--S); } .cube { transform-style: preserve-3d; /* transform-origin: center center; */ width: var(--S); height: var(--S); } .face.front { transform: translateZ(calc(var(--S) / 2)); } .face.back { transform: rotateY(180deg) translateZ(calc(var(--S) / 2)); } .face.left { transform: rotateY(-90deg) translateZ(calc(var(--S) / 2)); } .face.right { transform: rotateY(90deg) translateZ(calc(var(--S) / 2)); } .face.top { transform: rotateX(90deg) translateZ(calc(var(--S) / 2)); } .face.bottom { transform: rotateX(-90deg) translateZ(calc(var(--S) / 2)); } /* Colors and text position */ .face { line-height: var(--S); text-align: center; color: white; font-size: calc(var(--S) / 4); } .front { background-color: rgba(255, 0, 0, 0.5); } .back { background-color: rgba(0, 255, 0, 0.5); } .left { background-color: rgba(0, 0, 255, 0.5); } .right { background-color: rgba(0, 255, 255, 0.5); } .top { background-color: rgba(255, 0, 255, 0.5); } .bottom { background-color: rgba(255, 255, 0, 0.5); } /* Rotate a bit to show something 3D */ .cube { transform: rotateX(var(--rotX)) rotateY(var(--rotY)) rotateZ(var(--rotZ)); } .cube { border: solid 1px black; } .cube { margin: 50px auto 0 auto; } /* Define an animation on the cube */ .cube { animation: 7s ease infinite alternate spin; } @keyframes spin { from { transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg); } to { transform: rotateX(180deg) rotateY(180deg) rotateZ(0deg); } }