How to create cube with only HTML and CSS?

前端 未结 9 1967
星月不相逢
星月不相逢 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:00

    Changing the CSS for .square3 should do it:

    height: 58px;
    left: 50px;
    position: absolute;
    top: -18px;
    transform: skew(240deg, 150deg);
    width: 100px;
    

    https://jsfiddle.net/8vuj7peb/26/

    0 讨论(0)
  • 2021-01-30 18:01

    Use the following css for .square3:

    .square3{
      width:110px;
      height:110px;
      background:#c52329;
      float:left;
      transform: rotate(45deg) skew(-15deg, -15deg);
      position: absolute;
      left: 46px;
      top: -42px;
    }
    
    0 讨论(0)
  • 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;
    }
    <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.

    0 讨论(0)
提交回复
热议问题