gpt4 book ai didi

html - 使用背景颜色的多色圆形 div?

转载 作者:技术小花猫 更新时间:2023-10-29 11:40:43 25 4
gpt4 key购买 nike

我正在尝试在 CSS 中创建一个多色圆圈来模拟命运之轮,我尝试过使用线性渐变,但它只是应用了垂直穿过圆形 div 的颜色条,而不是像彩色区域一样如果这有意义的话,你正在切披萨?

这是我试过的代码:

background: -moz-linear-gradient(left, red, red 20%, blue 20%, blue);

结果是:

Wheel-attempt

但我希望它看起来更像这样?

Coloured wheel

这在 CSS 中是否可行,或者我是否必须使用背景图片(我宁愿避免这种情况,因为它不像页面大小调整那样容易缩放等等)?

最佳答案

考虑到旋转线性渐变,一种解决方案是使用多个背景层。我们还可以依赖伪元素和一些透明颜色。

然后只需调整伪元素的度数、颜色、颜色的不透明度和位置即可得到任何你想要的图表:

.circle {
margin: 20px;
width: 200px;
height: 200px;
border-radius: 50%;
background:
linear-gradient(to right, rgba(255,0,0,0.5) 50%, yellow 0%),
linear-gradient(-110deg, black 50%, pink 0%);
position: relative;
overflow: hidden;
}

.circle:after,
.circle:before{
content: "";
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
.circle:after {
background: linear-gradient(-45deg, rgba(255, 180, 180, 0.5) 50%, transparent 0%);
bottom: 50%;
left: 50%;
}

.circle:before {
background:
linear-gradient(0deg, rgba(128, 169, 170, 0.5) 50%, transparent 0%),
linear-gradient(50deg, rgba(0, 169, 170, 1) 50%, transparent 0%);
}
<div class="circle"></div>

这里有更多考虑不同配置的例子

  1. 仅使用一个元素和多个渐变:

.circle {
margin: 20px;
width: 200px;
height: 200px;
border-radius: 50%;
background: linear-gradient(0deg, rgba(0, 255, 217, 0.4) 50%, transparent 0%),
linear-gradient(45deg, rgba(0, 128, 0, 0.4) 50%, transparent 0%),
linear-gradient(90deg, rgba(11, 255, 0, 0.4) 50%, transparent 0%),
linear-gradient(135deg, pink 50%, transparent 0%),
linear-gradient(180deg, brown 50%, transparent 0%),
linear-gradient(225deg, yellow 50%, transparent 0%),
linear-gradient(270deg, red 50%, transparent 0%);
position: relative;
overflow: hidden;
}
<div class="circle"></div>

  1. 使用多个元素和每个元素一个渐变:

.circle {
margin: 20px;
width: 200px;
height: 200px;
border-radius: 50%;
background: linear-gradient(to right, red 50%, yellow 0%);
position: relative;
overflow: hidden;
}

.circle:after {
content: "";
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
background: linear-gradient(45deg, rgba(255, 180, 180, 0.5) 50%, transparent 0%);
}

.circle:before {
content: "";
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
background: linear-gradient(125deg, rgba(128, 169, 170, 0.5) 50%, transparent 0%);
}

.circle-alt {
width: 200px;
height: 200px;
border-radius: 50%;
background: linear-gradient(to bottom, rgba(0, 250, 0, 0.5) 50%, rgba(0, 250, 255, 0.5) 0%);
position: absolute;
overflow: hidden;
}
<div class="circle">
<div class="circle-alt"></div>
</div>

  1. 使用多个元素和每个元素的多个渐变和仅纯色颜色(不改变background-position像@vals 的回答):

.circle {
margin: 20px;
width: 200px;
height: 200px;
border-radius: 50%;
background: linear-gradient(to right, red 50%, transparent 0%),
linear-gradient(225deg, transparent 50%, blue 0%),
linear-gradient(0deg, green 50%, transparent 0%),
linear-gradient(-45deg, black 50%, transparent 0%),
linear-gradient(-90deg, yellow 50%, transparent 0%);
position: relative;
overflow: hidden;
}

.circle:before {
content: "";
position: absolute;
top: 0;
left: 0;
bottom: 50%;
right: 50%;
background:linear-gradient(45deg,lightblue 50%, transparent 0%)
}
.circle:after {
content: "";
position: absolute;
top: 50%;
left: 0;
bottom: 0;
right: 50%;
background:linear-gradient(135deg, brown 50%, pink 0%);
}
<div class="circle"></div>

  1. 命运之轮(旋转!):

.circle {
margin: 20px;
width: 200px;
height: 200px;
border-radius: 50%;
background: linear-gradient(to right, #06b51d 50%, transparent 0%),
linear-gradient(60deg, #7e00ff 50%, transparent 0%),
linear-gradient(30deg, #ff00bd 50%, transparent 0%),
linear-gradient(0deg, #ff0000 50%, transparent 0%),
linear-gradient(-30deg, #ff4700 50%, transparent 0%),
linear-gradient(-60deg, #ffa500 50%, transparent 0%),
linear-gradient(-90deg, #ffff00 50%, transparent 0%);
position: relative;
overflow: hidden;
animation: rotate 6s linear infinite;
}
.circle:before {
content: "";
position: absolute;
top: 0;
left: 0;
bottom: 50%;
right: 50%;
background: linear-gradient(210deg, transparent 64%, #09ffa5 0%),
linear-gradient(240deg, transparent 37%, #34ff00 0%);
}

.circle:after {
content: "";
position: absolute;
top: 50%;
left: 0;
bottom: 0;
right: 50%;
background:linear-gradient(150deg, #00acff 37%, transparent 0%),
linear-gradient(120deg, #0075ff 63%, #1200ff 0%);
}
@keyframes rotate {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
<div class="circle"></div>


以不同方式实现相同结果的相关问题:Sass/CSS color wheel

关于html - 使用背景颜色的多色圆形 div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47789300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com