How to create cube with only HTML and CSS?

前端 未结 9 1965
星月不相逢
星月不相逢 2021-01-30 17:03

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

9条回答
  •  孤独总比滥情好
    2021-01-30 18:05

    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;
    }

    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.

提交回复
热议问题