How do I create a cube with only HTML and CSS?

enter a description of the image here

I have this code, and I want to make a cube with HTML and CSS, just like in the picture above

.mainDiv{
  position: relative;
  width: 206px;
  height: 190px;
  margin: 0px auto;
  margin-top:100px;
}
.square{
  width:100px;
  height:100px;
  background:#c52329;
  border:solid 2px #FFF;
  float:left;
  transform: skew(180deg,210deg);
  position: absolute;
  top: 43px;
}
.square2{
  width:100px;
  height:100px;
  background:#c52329;
  border:solid 2px #FFF;
  float:left;
  transform: skew(180deg,150deg);
  position: absolute;
  left:102px;
  top: 43px;
}
.square3{
  width:100px;
  height:100px;
  background:#c52329;
  border:solid 2px #FFF;
  float:left;
  transform: skew(180deg,180deg);
  position: absolute;
  left: 51px;
  top: -61px;
}
<div class="mainDiv">
  <div class="square"></div>
  <div class="square2"></div>
  <div class="square3"></div>
</div>

What changes and additions should be made to the code?

Free translation of the question How to create a cube with only HTML and CSS? from member @Sunil Gehlot.

Author: Alexandr_TT, 2020-04-14

3 answers

And if you need to number the faces of the cube, for example, then the pseudo-elements :before and :after will not help here :)

.container {
  perspective: 500px;
  position: relative;
  width: 150px;
  height: 150px;
  margin: 25px auto;
}

.cube {
  transform-style: preserve-3d;
  position: absolute;
  width: 100%;
  height: 100%;
  animation: animate 7s linear infinite;
}

.cube span {
  backface-visibility: hidden;
  display: block;
  position: absolute;
  width: 100%;
  height: 100%;
  border: 3px solid #ffffff;
  background-color: #c62329;
  color: #ffffff;
  line-height: 150px;
  text-align: center;
  font-size: 80px;
  font-weight: bold;
}

.cube .front {transform: rotateY(0deg) translateZ(75px);}
.cube .back {transform: rotateX(180deg) translateZ(75px);}
.cube .top {transform: rotateX(90deg) translateZ(75px);}
.cube .bottom {transform: rotateX(-90deg) translateZ(75px);}
.cube .left {transform: rotateY(-90deg) translateZ(75px);}
.cube .right {transform: rotateY(90deg) translateZ(75px);}

@keyframes animate {
0% {transform: translateZ(-150px) rotateY(360deg) rotateZ(0deg) rotateX(45deg)}
50% {transform: translateZ(-150px) rotateY(180deg) rotateZ(180deg) rotateX(45deg)}
100% {transform: translateZ(-150px) rotateY(0deg) rotateZ(360deg) rotateX(45deg)}
}
<div class="container">
  <div class="cube"><span class="front">1</span><span class="back">2</span><span class="top">3</span><span class="bottom">4</span><span class="left">5</span><span class="right">6</span></div>
</div>

Will anyone be able to additionally give an answer to the rotation of the cube it depended on the cursor movements. Let's say we move the cursor up and down the cube rotates around the Y-axis in different directions. When you move the cursor left-right, the cube rotates around the X-axis

Yes, please. But here only HTML and CSS are used, I think we can't do it. You need to add a little JavaScript.

var cube = document.getElementById("cube");
document.onmousemove = function(){
var x = event.clientX,
y = event.clientY;
cube.style.transform = "rotateY(" + x + "deg)" + "rotateX(" + y + "deg)";
}
.container {
  perspective: 500px;
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -75px;
  margin-left: -75px;
  width: 150px;
  height: 150px;
}

.cube {
  transform-style: preserve-3d;
  position: absolute;
  width: 100%;
  height: 100%;
}

.cube span {
  backface-visibility: hidden;
  display: block;
  position: absolute;
  width: 100%;
  height: 100%;
  border: 3px solid #ffffff;
  background-color: #c62329;
  color: #ffffff;
  line-height: 150px;
  text-align: center;
  font-size: 80px;
  font-weight: bold;
}

.cube .front {transform: rotateY(0deg) translateZ(75px);}
.cube .back {transform: rotateX(180deg) translateZ(75px);}
.cube .top {transform: rotateX(90deg) translateZ(75px);}
.cube .bottom {transform: rotateX(-90deg) translateZ(75px);}
.cube .left {transform: rotateY(-90deg) translateZ(75px);}
.cube .right {transform: rotateY(90deg) translateZ(75px);}
<div class="container">
  <div id="cube" class="cube"><span class="front">1</span><span class="back">2</span><span class="top">3</span><span class="bottom">4</span><span class="left">5</span><span class="right">6</span></div>
</div>
 3
Author: Sevastopol', 2020-04-14 19:54:27

You can also get a cube with 3D-transforms.
This will give your cube a more realistic perspective.
As if the cube was a real three-dimensional shape like this:

enter a description of the image here

Next, I used a single div with 2 pseudo elements:

body {
  perspective: 900px;
  padding-bottom:50%;
}
div {
  position: relative;
  width: 20%;
  padding-bottom: 20%;
  margin: 0 auto;
  transform-style: preserve-3d;
  background: #C52329;
  transform: rotateX(60deg) rotatez(45deg);
}
div:before, div:after {
  content: '';
  position: absolute;
  width: 100%;
  height: 100%;
  transform-origin: -2% -2%;
  background: inherit;
}
div:before {
  top: 104%; left: 0;
  transform: rotateX(-90deg);
}
div:after {
  top: 0; left: 104%;
  transform: rotateY(90deg);
}
<div></div>

CSS 3d cube with 6 faces

This technique allows you to create a "real cube" with 6 faces that can be rotated:

body{
  perspective-origin:50% -100%;
  perspective: 900px;
  overflow:hidden;
}
h1{position:absolute;font-family:sans-serif;}
.cube {
  position:relative;
  padding-bottom:20%;
  transform-style: preserve-3d;
  transform-origin: 50% 100%;
  transform:rotateY(45deg) rotateX(0);
  transition:transform 3s;
}
.cubeFace {
  position: absolute;
  left:40%;top:0;
  width: 20%;height:100%;
  margin: 0 auto;
  transform-style: inherit;
  background: #C52329;
  box-shadow:inset 0 0 0 5px #fff; 
  transform-origin:50% 50%;
  transform: rotateX(90deg);
  backface-visibility:hidden;
}
.face2{
  transform-origin:50% 50%;
  transform: rotatez(90deg) translateX(100%) rotateY(90deg);
}
.cubeFace:before, .cubeFace:after {
  content: '';
  position: absolute;
  width: 100%;
  height: 100%;
  transform-origin:0 0;
  background: inherit;
  box-shadow:inherit;
  backface-visibility:inherit;
}
.cubeFace:before {
  top: 100%; left: 0;
  transform: rotateX(-90deg);
}
.cubeFace:after {
  top: 0; left: 100%;
  transform: rotateY(90deg);
}

body:hover .cube{
  transform:rotateY(405deg) rotateX(360deg);
}
<h1>Hover me:</h1>
<div class="cube">
  <div class="cubeFace"></div>
  <div class="cubeFace face2"></div>
</div>

Note that I didn't add vendor prefixes in the examples. For more information about browser support and what vendor prefixes are needed for your target audience, see canIuse for 3d transforms.

Free translation of the answer How to create a cube with only HTML and CSS? from a member of @web-tiki.

 3
Author: Alexandr_TT, 2020-04-14 16:11:15

According to your HTML, I get this JSFiddle I just played with transform.

.mainDiv{
  position: relative;
  width: 206px;
  height: 190px;
  margin: 0px auto;
  margin-top:100px;
}
.square{
  width:100px;
  height:100px;
  background:#c52329;
  border:solid 2px #FFF;
  transform: skew(180deg,210deg);
  position: absolute;
  top: 43px;
}
.square2{
  width:100px;
  height:100px;
  background:#c52329;
  border:solid 2px #FFF;
  transform: skew(180deg,150deg);
  position: absolute;
  left:102px;
  top: 43px;
}
.square3{
  width:114px;
  height:100px;
  background:#c52329;
  border:solid 2px #FFF;
 

transform: rotate(150deg) translate(-40px, -16px) skew(30deg, 0deg);
  position: absolute;
  left: 0px;
  top: -32px;
}
<div class="mainDiv">
  <div class="square"></div>
  <div class="square2"></div>
  <div class="square3"></div>
</div>

Updated CSS

.square3{
  width:114px;
  height:100px;
  background:#c52329;
  border:solid 2px #FFF;
  transform: rotate(150deg) translate(-40px, -16px) skew(30deg, 0deg);
  position: absolute;
  left: 0px;
  top: -32px;
}

I changed the CSS conversion with this:

Additionally: David Walsh has a cool animated version in cuba. Besides the fact that it looks pretty cool, you can learn a lot about it by playing around with the settings.

Free translation of the answer How to create a cube with only HTML and CSS? from member @Leo the lion.

 3
Author: Alexandr_TT, 2020-04-14 17:36:07