Hello, in this tutorial, I will show you how to create a 3D rotating cube with radio buttons to control the rotations using only CSS.
Hello, in this tutorial, I will show you how to create a 3D rotating cube with radio buttons to control the rotations using only CSS.
Setting up the HTML Page
Create a CSS file and add it to the page.
<link rel="stylesheet" href="./index.css">
Building the CSS
In the CSS file, create 6 variables for the images you want to put on each side.
Now add the selectors for the cube-container, cube, cube-side- and radio-button classes.
For radio-button set the bottom-margin to 5em. This will place the buttons 5em above the cube.
For cube-container, set the dimensions for the cube to 15em using width and height. Then set text-align to center and the perspective to 3 times the cube size.
Using perspective will allow us to view the elements in a 3D point-of-view.
For cube, set the width and height to match the container, position to relative, transform-style to preserve-3d, and transition-duration to 2s.
Using transform-style: preserve-3d will allow us to preserve the 3d transformations while transitioning.
If we look at the results on the browser, we’ll see a square in the center, but there are really 6 squares on top of each other.
Creating the Cube
To create the cube, create a CSS selector for each side using the nth-child pseudo class.
We are starting from 1 and ending on 6 because there are 6 sides to the cube.
For the first, second, third, and fourth sides, use the transform property and then rotateY() function to rotate them in the Y-axis.
Fifth and sixth sides, use the rotateX() function and rotate the fifth side by -90 degrees and the sixth side by 90 degrees.
If we look at the result on the browser, there should be a box with a line across the center. This tells us everything is transformed correctly.
If we add some transform rotations to the cube, we will be able to see effects easier.
They are all stacked on top of each other in the center because the rotateX() and rotateY() functions will rotate the element relative to the center.
If we look at the result now, we will see the cube.
Adding the Images
To add the images, in the cube-side selector, set the background-position to center and the background-size to cover.
Then for each of the individual sides, set the background-image property with the image variables that was declared.
If we look at the result now, the images are placed on each side.
Rotating the Cube with the Radio Buttons
In the CSS file, remove the transforms from the .cube selector.
.radio-button:checked ~ .cube { }
Set the transition-duration to 3 seconds so we’ll get the animation effect. Then set the transition-timing-function with the cubic-bezier() function.
If we look at the result now and click on the radio buttons, our cube will spin to the correct positions.
That is all for this tutorial, if you find this helpful, please give it a like, share, and subscribe to support the channel. See you in the next video.
Download Source Code