【问题标题】:How to segment a circle with different colors using CSS如何使用 CSS 分割具有不同颜色的圆
【发布时间】:2015-08-05 10:33:50
【问题描述】:

我希望能够用另一种颜色的一部分绘制一个圆圈,我希望所覆盖的部分的数量能够以10% 的增量从0% 增加到100%

Google 上的所有示例都是扇区而非细分。

到目前为止,这是我能想到的最好的:

div.outerClass {
    position: absolute;
    left: 10px;
    top: 10px;
    height: 2.5px;
    overflow: hidden;
    -ms-transform: rotate(270deg); /* IE 9 */
    -webkit-transform: rotate(270deg); /* Chrome, Safari, Opera */
    transform: rotate(270deg);
}

div.innerClass {
    width: 10px;
    height: 10px;
    border: 5px solid green;
    border-radius: 36px;
}
<div class="outerClass">
    <div class="innerClass"></div>
</div>

0%50%100% 我都可以。

【问题讨论】:

    标签: html css svg css-shapes


    【解决方案1】:

    您可以使用linear-gradient 完成此操作

    .circle{
      position:absolute;
      width:80px;
      height:80px;
      border-radius:50%;
      background: linear-gradient(
        to right,
        yellow  0%, yellow 10%,
        orange 10%, orange 20%,
        yellow 20%, yellow 30%,
        orange 30%, orange 40%,
        yellow 40%, yellow 50%,
        orange 50%, orange 60%,
        yellow 60%, yellow 70%,
        orange 70%, orange 80%,
        yellow 80%, yellow 90%,
        orange 90%, orange 100%
      );
    }
    &lt;div class="circle"&gt;&lt;/div&gt;

    否则,您可以将 10 子元素放入您的 overflow:hidden 圈父:

    .circle{
      position:absolute;
      width:80px;
      height:80px;
      border-radius:50%;
      overflow:hidden;
    }
    .circle > span{
      width:10%;
      height:100%;
      float:left;
    }
    
    .circle > span:nth-child(1){ background: yellow;}
    .circle > span:nth-child(2){ background: orange;}
    .circle > span:nth-child(3){ background: blue;}
    .circle > span:nth-child(4){ background: green;}
    .circle > span:nth-child(5){ background: fuchsia;}
    .circle > span:nth-child(6){ background: orange;}
    .circle > span:nth-child(7){ background: gold;}
    .circle > span:nth-child(8){ background: tan;}
    .circle > span:nth-child(9){ background: navy;}
    .circle > span:nth-child(10){background: brown;}
    <div class="circle">
      <span></span><span></span><span></span><span></span><span></span>
      <span></span><span></span><span></span><span></span><span></span>
    </div>

    【讨论】:

    • 感谢您提供的答案,我考虑过使用渐变,但我不知道线性渐变,不幸的是我选择了 Sergey 的答案,因为它更简单,尽管两者都正确回答了我的问题。
    • @user2871826 坦率地说,我看不出 sergey 的回答有多简单。随你便。 :) 不客气
    • 它的长度更短,系统将更容易实现我使用它的目的。但是是的,在简单性方面没有太大区别。
    • 这个渐变使用一个元素而不是最多 11 个...对我来说似乎更简单...而且它也可以正确保存内容。
    • 我真的很喜欢第一种方法,但它有unfortunate side effect of jaggy pixels on the edges on my screen(我在配备 Retina 的 15" MacBook Pro 上使用 Chrome)。
    【解决方案2】:

    跨浏览器解决方案:

    JSFiddle

    .circle {
        border-radius: 50%;
        background: gray;
        width: 300px;
        height: 300px;
        overflow: hidden;
    }
    .segment {
        float: left;
        width: 10%;
        height: 100%;
    }
        .segment_1 {
            background: red;
        }
        .segment_2 {
            background: green;
        }
        .segment_3 {
            background: yellow;
        }
        .segment_4 {
            background: blue;
        }
    <div class="circle">
        <div class="segment segment_1"></div>
        <div class="segment segment_2"></div>
        <div class="segment segment_3"></div>
        <div class="segment segment_4"></div>
    </div>

    【讨论】:

    • @grgarside 因为它只改变了格式样式,我不喜欢它。如果 SO 有自己的帖子/代码风格指南,请给我链接。
    • @grgarside 抱歉,我没有注意到堆栈 sn-p。回滚并修复了格式。
    • @grgarside:公平地说,他也不必更改格式样式以使其在堆栈片段中工作。即使他使用 Stack Snippet 编辑器生成 sn-p,他也可以按原样复制代码并且它会工作。根本没有理由重新格式化它。
    • 我将代码转换为 stacksn-p,并在此过程中单击整理按钮。顺便说一下,压痕对我来说很奇怪。不明白为什么要缩进 .segment_... 类而不是 .segment 本身。
    • @SalmanA 我喜欢这样格式化。 getbem.com/introduction.segment 是块,.segment_... 是修饰符。
    【解决方案3】:

    盒子阴影

    另一种方法是使用一个元素和盒子阴影。

    • 主要元素是圆形 (border-radius: 50%;),纵横比为 1:1。

    • 伪元素位于left: -100%;,或者就在主元素的左侧。

    • 对伪元素应用10个盒子阴影,颜色不同,横坐标不同。我把横坐标设置为 30px,因为 30px 是 300px 的 10% ...

    • 选择了 10% 的宽度,因为需要 10 个条纹。

    div {
      height: 300px;
      width: 300px;
      border: 1px solid black;
      position: relative;
      border-radius: 50%;
      overflow: hidden;
    }
    div:before {
      position: absolute;
      content: '';
      height: inherit;
      width: inherit;
      left: -100%;
      background: red;
      box-shadow: 
        30px 0 0 chocolate,
        60px 0 0 hotpink,
        90px 0 0 indigo,
        120px 0 0 orangered,
        150px 0 0 gold,
        180px 0 0 deepskyblue,
        210px 0 0 springgreen,
        240px 0 0 darkslategray,
        270px 0 0 gold,
        300px 0 0 navy;
    }
    &lt;div&gt;&lt;/div&gt;

    【讨论】:

    • 我真的很惊讶使用盒子阴影没有答案!我以为有人已经发布了它。我想你刚才看到了这个问题?我也开始制作小提琴:jsfiddle.net/nqys0fyo :)
    【解决方案4】:

    另一种方法是使用 SVG。这些段由&lt;rect /&gt; 元素组成,并使用&lt;clipPath/&gt; 元素剪辑成一个圆圈:

    svg{width:40%;display:block;margin:0 auto;}
    use:hover{fill:#000;}
    <svg viewBox="0 0 10 10">
      <defs>
        <clipPath id="circle">
          <circle cx="5" cy="5" r="5" />
        </clipPath>
        <rect id="seg" y="0" width="1" height="10" />
      </defs>  
      <g clip-path="url(#circle)">
        <use xlink:href="#seg" x="0" fill="pink"/>
        <use xlink:href="#seg" x="1" fill="green" />
        <use xlink:href="#seg" x="2" fill="orange" />
        <use xlink:href="#seg" x="3" fill="teal" />
        <use xlink:href="#seg" x="4" fill="tomato"/>
        <use xlink:href="#seg" x="5" fill="gold"/>
        <use xlink:href="#seg" x="6" fill="darkorange" />
        <use xlink:href="#seg" x="7" fill="pink" />
        <use xlink:href="#seg" x="8" fill="red" />
        <use xlink:href="#seg" x="9" fill="yellow" />
      </g>
    </svg>

    【讨论】:

    • SVG 规则! (如果你不担心 IE8)很好的例子。我们应该真正开始/继续关心 SVG 并展示更多 ♥
    • Raphael 与 VML 后备将适用于 IE 6.0+
    • @RokoC.Buljan 是的,虽然这种情况可以很容易地用 CSS 处理我相信给 SVG 更多♥对每个人来说都是一个真正的好处:)
    • 在这种情况下,svg 与接受的答案相比没有优势。它有相同的编号。元素
    • @TimKrul 这取决于您需要对形状做什么。如果 HTML 元素的数量是选择标准,那么您的答案是最好的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 2017-02-28
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    • 1970-01-01
    相关资源
    最近更新 更多