HTML Code structure
When using a CSS perspective property, you need a container, depth the perspective, and then have its children move within the container
.container {
width: 200px;
height: 200px;
perspective: 1000px;
margin: 100px auto 0;
}
Copy the code
Setting the Perspective value
Set the depth value for the perspective, which represents the maximum distance from the screen to the element’s environment
.container {
width: 200px;
height: 200px;
perspective: 1000px;
margin: 100px auto 0;
}
Copy the code
Set 3D properties for the cube
.cube {
transform-style: preserve-3d;
width: 100%;
height: 100%;
position: relative;
}
Copy the code
Set each face of the cube to an absolute position of 100% by 100%, and then set the background to a grid.
.face { position: absolute; width: 100%; height: 100%; background-image: Repeating-linear (0deg, rgba(70,70,70, 0.2) 0px, rgba(70,70,70, 0.2) 0px, transparent 1px, repeating-linear (0deg, rgba(70,70,70, 0.2) 0px, 500px; Transparent 21px),repeating-linear (90deg, rgba(70,70,70, 0.2) 0px, rgba(70,70,70, 0.2) 0px, transparent 1px, repeating-linear (90deg, rgba(70,70,70, 0.2) 0px, 500px; transparent 1px, repeating-linear (90deg, rgba(70,70,70, 0.2) 0px; Transparent 21 px), linear - gradient (90 deg, RGB (255255255), RGB (255255255)); }Copy the code
The six faces of the cube are transformed by the z-axis and rotated to form the six faces of the cube
.top {
transform: rotateX(90deg) translateZ(100px);
}
.bottom {
transform: rotateX(-90deg) translateZ(100px);
}
.right {
transform: rotateY(90deg) translateZ(100px);
}
.left {
transform: rotateY(-90deg) translateZ(100px);
}
.front {
transform: rotateX(0deg) translateZ(100px);
}
.back {
transform: rotateX(-180deg) translateZ(100px);
}
Copy the code
The last step is to rotate the cube and set the animation for the wireless loop
@keyframes spin {
from {
transform: rotateX(0deg) rotateY(0deg);
}
to {
transform: rotateX(360deg) rotateY(360deg);
}
}
.cube {
transform-style: preserve-3d;
width: 100%;
height: 100%;
position: relative;
animation: spin 5s infinite linear;
}
Copy the code
The results are as follows