How to create cube with only HTML and CSS?

前端 未结 9 1974
星月不相逢
星月不相逢 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 17:46

    Basically, you want to do 2 transformations:

    1. rotate the rectangle
    2. squeeze it (skew it)

    so basically, you need to do a transform: rotate(x) skew(y, y) and play a bit with size and placing.

    here's a little demo I created, based on your own demo:

    (I did remove the borders since they felt unneeded to me)

    .mainDiv{
      position: relative;
      width: 206px;
      height: 190px;
      margin: 0px auto;
      margin-top:100px;
    }
    .square{
      width:100px;
      height:100px;
      background:#c52329;
      float:left;
      transform: skew(180deg,210deg);
      position: absolute;
      top: 43px;
    }
    .square2{
      width:100px;
      height:100px;
      background:#c52329;
      float:left;
      transform: skew(180deg,150deg);
      position: absolute;
      left:102px;
      top: 43px;
    }
    .square3{
      width:110px;
      height:110px;
      background:#c52329;
      float:left;
      transform: rotate(45deg) skew(-15deg, -15deg);
      position: absolute;
      left: 46px;
      top: -42px;
    }

提交回复
热议问题