How to create a lollipop shape by stacking divs in a circular manner?

后端 未结 3 1518
-上瘾入骨i
-上瘾入骨i 2021-01-05 18:22

How to stack divs in a circular manner in which last div should come beneath the first div but above the second last div. Is it possible with the css? any helps would be app

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-05 19:25

    I would create this considering two elements (pseudo elements) and multiple radial gradient. You only need to create half the shape twice and rotate one of them.

    .box {
      width:150px;
      height:150px;
      border-radius:100%;
      border:1px solid;
      position:relative;
      overflow:hidden;
    }
    .box::before,
    .box::after{
      content:"";
      position:absolute;
      top:0;
      bottom:0;
      left:0;
      right:50%;
      background:
        /*we rotate by 30deg so will use :
           sin(30deg)*R = 0.5x75px   = 37.5px 
           cos(30deg)*R = 0.866x75px = 64.95px       
           10.05px = 75px - 64.95px;
           112.5px = 75px + 37.5px
           139.95px = 75px + 64.95px
           37.5px = 75px - 37.5px
           */
        radial-gradient(circle 75px at 139.95px 37.5px,red   98%,transparent 100%),
        radial-gradient(circle 75px at 112.5px 10.05px,white 98%,transparent 100%),
        radial-gradient(circle 75px at 75px    0,      red   98%,transparent 100%),
        radial-gradient(circle 75px at 37.5px  10.05px,white 98%,transparent 100%),
        radial-gradient(circle 75px at 10.05px 37.5px ,red   98%,transparent 100%),
        radial-gradient(circle 75px at 0       75px,   white 98%,transparent 100%),
        radial-gradient(circle 75px at 10.05px 112.5px,red   98%,transparent 100%);
    }
    
    .box::after {
     transform:rotate(180deg);
     transform-origin:right;
    }

    To make things more funny we can add CSS variables to easily control the shape:

    .box {
      --R:50px; /*Radius*/
      --c1:red; /*first color*/
      --c2:#fff; /*second color*/
      
      --g1:var(--c1) 98%, transparent 100%;
      --g2:var(--c2) 98%, transparent 100%;
      width:calc(2*var(--R));
      height:calc(2*var(--R));
      border-radius:100%;
      border:1px solid;
      position:relative;
      overflow:hidden;
      display:inline-block;
      vertical-align:middle;
    }
    .box::before,
    .box::after{
      content:"";
      position:absolute;
      top:0;
      bottom:0;
      left:0;
      right:50%;
      background:
         /*we rotate by 30deg so will use :
           sin(30deg)*R = 0.5xR   
           cos(30deg)*R = 0.866xR 
         */
        radial-gradient(circle var(--R) at calc(var(--R) + 0.866*var(--R)) calc(var(--R) - 0.5*var(--R))  ,var(--g1)),
        radial-gradient(circle var(--R) at calc(var(--R) + 0.5*var(--R))   calc(var(--R) - 0.866*var(--R)),var(--g2)),
        radial-gradient(circle var(--R) at var(--R)                        0                              ,var(--g1)),
        radial-gradient(circle var(--R) at calc(var(--R) - 0.5*var(--R))   calc(var(--R) - 0.866*var(--R)),var(--g2)),
        radial-gradient(circle var(--R) at calc(var(--R) - 0.866*var(--R)) calc(var(--R) - 0.5*var(--R))  ,var(--g1)),
        radial-gradient(circle var(--R) at 0                               var(--R)                        ,var(--g2)),
        radial-gradient(circle var(--R) at calc(var(--R) - 0.866*var(--R)) calc(var(--R) + 0.5*var(--R))  ,var(--g1));
    }
    
    /*the same shape rotated*/
    .box::after {
     transform:rotate(180deg);
     transform-origin:right;
    }


    Note that Safari doesn't support the syntax with at (explained here How to make radial gradients work in Safari?) so here is anothe syntax:

    .box {
      --R:50px; /*Radius*/
      --c1:red; /*first color*/
      --c2:#fff; /*second color*/
      
      --g1:var(--c1) 98%, transparent 100%;
      --g2:var(--c2) 98%, transparent 100%;
      width:calc(2*var(--R));
      height:calc(2*var(--R));
      border-radius:100%;
      border:1px solid;
      position:relative;
      overflow:hidden;
      display:inline-block;
      vertical-align:middle;
    }
    .box::before,
    .box::after{
      content:"";
      position:absolute;
      top:0;
      bottom:0;
      left:0;
      right:50%;
      background:
         /*we rotate by 30deg so will use :
           sin(30deg)*R = 0.5xR   
           cos(30deg)*R = 0.866xR 
         */
        radial-gradient(farthest-side,var(--g1)) calc(var(--R) + 0.866*var(--R) - var(--R)) calc(var(--R) - 0.5*var(--R) - var(--R)),
         
        radial-gradient(farthest-side,var(--g1)) calc(var(--R) + 0.866*var(--R) - var(--R)) calc(var(--R) - 0.5*var(--R) - var(--R)),
        radial-gradient(farthest-side,var(--g2)) calc(var(--R) + 0.5*var(--R) - var(--R))   calc(var(--R) - 0.866*var(--R) - var(--R)),
        radial-gradient(farthest-side,var(--g1)) 0 calc(-1*var(--R)),
        radial-gradient(farthest-side,var(--g2)) calc(var(--R) - 0.5*var(--R) - var(--R))   calc(var(--R) - 0.866*var(--R) - var(--R)),
        radial-gradient(farthest-side,var(--g1)) calc(var(--R) - 0.866*var(--R) - var(--R)) calc(var(--R) - 0.5*var(--R) - var(--R)),
        radial-gradient(farthest-side,var(--g2)) calc(-1*var(--R))  0,
        radial-gradient(farthest-side,var(--g1)) calc(var(--R) - 0.866*var(--R) - var(--R)) calc(var(--R) + 0.5*var(--R) - var(--R));
       background-size:calc(2*var(--R)) calc(2*var(--R));
       background-repeat:no-repeat;
    }
    
    /*the same shape rotated*/
    .box::after {
     transform:rotate(180deg);
     transform-origin:right center;
    }

提交回复
热议问题