Segments in a circle using CSS3

后端 未结 4 1965
失恋的感觉
失恋的感觉 2020-11-27 04:06

I know you can make a circle in CSS3 using the border radius hack. But is there any way to make them have segments like this picture? Is there a way of doing this through HT

4条回答
  •  醉酒成梦
    2020-11-27 04:21

    Yes, you can get such slices of custom angles using either one of the following two methods:

    1. If you don't need the slices to be elements themselves, the you can simply do it with one element and linear gradients - see this rainbow wheel I did last month.
    2. If you need the slices to be elements themselves, then you can do it by chaining rotate and skew transforms - see this circular menu I did a while ago.

    For #2, see also this very much simplified example I did right now.

    .pie {
      overflow:hidden;
      position: relative;
      margin: 1em auto;
      border: dashed 1px;
      padding: 0;
      width: 32em; height: 32em;
      border-radius: 50%;
      list-style: none;
    }
    .slice {
      overflow: hidden;
      position: absolute;
      top: 0; right: 0;
      width: 50%; height: 50%;
      transform-origin: 0% 100%; 
    }
    .slice:first-child {
      transform: rotate(15deg) skewY(-22.5deg);
    }
    .slice-contents {
      position: absolute;
      left: -100%;
      width: 200%; height: 200%;
      border-radius: 50%;
      background: lightblue;
    }
    .slice:first-child .slice-contents {
      transform: skewY(22.5deg); /* unskew slice contents */
    }
    .slice:hover .slice-contents { background: violet; } /* highlight on hover */

提交回复
热议问题