Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create cube with only HTML and CSS?

CSS cube

I have this and I want to make a cube with HTML & CSS only like in the above image. My best try:

.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>
like image 501
Sunil Gehlot Avatar asked Mar 31 '16 10:03

Sunil Gehlot


People also ask

How do you make a cube using HTML and CSS?

3D cube effect can be achieved easily with CSS 3D Transform by creating div for each side of the cube. Then use rotateX, rotateY and rotateZ to put them into their places. Transform-origin is also needed to move the rotation fulcrum.

How do you make a 3D element in CSS?

Note the use of the transform-style property: when this is set to preserve-3d, the cube's sides will carry out transforms in their own 3D space. We also specify the point around which transform is done. The transform-origin property sets a point in the X, Y and Z axes that acts as a centre of rotation.

What is Cube CSS?

What is CUBE CSS? CUBE CSS was created by Andy Bell, and it is a CSS methodology geared towards simplicity, pragmatism, and consistency. CUBE stands for “Composition Utility Block Exception” and is a tool-agnostic methodology.


2 Answers

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 transform CSS with this.


Extra: David Walsh has a cool animated version on an cube. Apart from the fact that it looks kinda cool, by fiddling with the settings you can learn quite a lot about it.

like image 174
Leo the lion Avatar answered Oct 01 '22 06:10

Leo the lion


You can also achieve a cube with 3d transforms. This will give your cube a more realistic perspective. As if the cube was a real 3d shape like this:

3d cube with 3d transforms

In the following I used one 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 make a "real cube" with 6 faces:

rotating 3d cube with 6 faces

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 the vendor prefixes in the examples. For more info about browser support and what vendor prefixes are needed according to your target audience, see canIuse for 3d transforms.

like image 44
web-tiki Avatar answered Oct 01 '22 06:10

web-tiki