Zig zag border for a circle?

后端 未结 3 1300
滥情空心
滥情空心 2020-12-30 05:02

Based on many tutorials I am able to create a zig zag border for square/rectangle objects using :after and :before. However when it comes to circle

3条回答
  •  情歌与酒
    2020-12-30 05:33

    You can create a (jagged) circle with triangles. The idea is to create an equilateral triangle inscribed inside the circle, create copies of it and rotate them around the center. The following figure explains how the sides of the triangle are calculated:

    .circle {
      width: 100px;
      height: 100px;
      position: relative;
      background-color: #CCCCCC;
    }
    
    /*
     * triangle border left/right = 50px * cos(30°)
     * triangle border bottom = 50px + 50px * sin(30°)
     */
    
    .triangle {
      position: absolute;
      left: 6.7px;
      top: 0;
      width: 0;
      height: 0;
      border-left: 43.3px solid transparent;
      border-right: 43.3px solid transparent;
      border-bottom: 75px solid #BF9020;
      transform-origin: center 50px;
    }
    
    .triangle:nth-child(2) { transform: rotate(10deg); }
    .triangle:nth-child(3) { transform: rotate(20deg); }
    .triangle:nth-child(4) { transform: rotate(30deg); }
    .triangle:nth-child(5) { transform: rotate(40deg); }
    .triangle:nth-child(6) { transform: rotate(50deg); }
    .triangle:nth-child(7) { transform: rotate(60deg); }
    .triangle:nth-child(8) { transform: rotate(70deg); }
    .triangle:nth-child(9) { transform: rotate(80deg); }
    .triangle:nth-child(10) { transform: rotate(90deg); }
    .triangle:nth-child(11) { transform: rotate(100deg); }
    .triangle:nth-child(12) { transform: rotate(110deg); }

    Once you're able to get your head around the above example use the fluid version below:

    .circle {
      width: 80vh;
      height: 80vh;
      position: relative;
      background-color: #CCCCCC;
    }
    
    .triangle {
      position: absolute;
      left: 6.7%;
      top: 0;
      width: 86.6%;
      height: 75%;
      background-color: #BF9020;
      clip-path: polygon(50% 0, 0 100%, 100% 100%);
      transform-origin: center 66.6%;
    }
    
    .triangle:nth-child(2) { transform: rotate(10deg); }
    .triangle:nth-child(3) { transform: rotate(20deg); }
    .triangle:nth-child(4) { transform: rotate(30deg); }
    .triangle:nth-child(5) { transform: rotate(40deg); }
    .triangle:nth-child(6) { transform: rotate(50deg); }
    .triangle:nth-child(7) { transform: rotate(60deg); }
    .triangle:nth-child(8) { transform: rotate(70deg); }
    .triangle:nth-child(9) { transform: rotate(80deg); }
    .triangle:nth-child(10) { transform: rotate(90deg); }
    .triangle:nth-child(11) { transform: rotate(100deg); }
    .triangle:nth-child(12) { transform: rotate(110deg); }

提交回复
热议问题