簡體   English   中英

如何使用CSS分割不同顏色的圓圈

[英]How to segment a circle with different colors using CSS

我希望能夠繪制一個帶有一段其他顏色的圓圈,我希望覆蓋的一段數量能夠以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%我都能做到。

你可以使用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% ); } 
 <div class="circle"></div> 

否則你可以在overflow:hidden放入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> 

跨瀏覽器解決方案:

的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> 

BOX SHADOW

另一種方法可能是使用一個元素和盒子陰影。

  • 主要元素是圓( 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; } 
 <div></div> 

另一種方法是使用SVG。 這些段由<rect />元素組成,並使用<clipPath/>元素將它們剪切為圓形:

 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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM