I am trying to make a 3d cube on top of a surface using CSS and HTML. There is a ball on the top of it. I tried to use transform: translateZ() method on the front , bottom , left and right div.
It did not work. Can someone tell how to fix this and why it didn't work?
This is the code:
:root {
--boxColor: #0ff7
}
body {
align-items: center;
justify-content: center;
display: flex;
background-color: black;
min-height: 100vh;
font-size: 50px;
perspective: 10px;
perspective-origin: 720px 80px
}
.scene {
position: relative;
transform-style: preserve-3d
}
.ball {
background: lightblue;
width: 1em;
height: 1em;
border-radius: 50%;
position: absolute;
top: -3em;
left: -1em;
}
.cube {
background: var(--boxColor);
height: 3em;
width: 3em;
position: absolute;
top: -2em;
left: -2em;
}
.floor {
width: 28em;
height: 0.2em;
background: repeating-linear-gradient(to top right, red, blue);
position: absolute;
top: 1em;
left: -15em;
transform: rotateX(90deg);
}
.left,
.right,
.front,
.bottom {
width: 100%;
height: 100%;
background: var(--boxColor);
position: absolute;
}
.front {
transform: translateZ(9em);
}
.left {
transform: rotateY(90deg) translateZ(3em);
}
This is the html code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title></title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="scene">
<div class="floor"> </div>
//cube section
<div class="cube">
<div class='left'></div>
<div class='right'></div>
<div class="front"></div>
<div class='bottom'></div>
</div>
//cube section
<div class="ball"> </div>
</div>
</div>
</body>
</html>