如何在尊重弹性物品数量的同时,在此flex容器中向多个物品背靠背添加渐变类?目前,只能通过将一个类添加到一组特定的“单位”来工作,并且会产生单独的渐变。
我希望渐变可以覆盖两个或多个项目,同时使所有项目保持灵活和相等。
.chart {
display: flex;
flex-direction: row;
justify-content: space-between;
padding: 0 15px;
margin-bottom: 50px;
}
.dot-black {
width: 8px;
height: 8px;
border-radius: 4px;
background: black;
}
.dot-grey {
width: 22px;
height: 22px;
border-radius: 11px;
background: grey;
}
.dot-green {
width: 22px;
height: 22px;
border-radius: 11px;
background: green;
}
.unit {
flex: 1;
display: flex;
align-items: center;
}
.line-black {
height: 2px;
background: black;
}
.line-grey {
height: 2px;
background: grey;
flex-grow: 1;
}
.gradient {
background: rgb(2,0,36);
background: linear-gradient(90deg, rgba(2,0,36,1) 0%, rgba(9,9,121,1) 35%, rgba(0,212,255,1) 100%);
}
<div class="chart">
<div class="unit gradient">
<div class="dot-black"></div>
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit gradient">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
</div>
答案 0 :(得分:2)
您可以控制父元素的渐变,您可以在其中指定想要的背景大小以仅覆盖所需元素:
.chart {
display: flex;
flex-direction: row;
justify-content: space-between;
padding: 0 15px;
margin-bottom: 50px;
background:
linear-gradient(90deg, rgba(2, 0, 36, 1), rgba(9, 9, 121, 1) 35%, rgba(0, 212, 255, 1))
left /* Start from the left */
/
calc(var(--n,0)* 100%/8) 100% /* we have 8 circles so Nx(width)/8 */
content-box /* Cover only the content, don't account for padding */
no-repeat; /* Don' repeat */
}
.dot-black {
width: 8px;
height: 8px;
border-radius: 4px;
background: black;
}
.dot-grey {
width: 22px;
height: 22px;
border-radius: 11px;
background: grey;
}
.dot-green {
width: 22px;
height: 22px;
border-radius: 11px;
background: green;
}
.unit {
flex: 1;
display: flex;
align-items: center;
}
.line-black {
height: 2px;
background: black;
}
.line-grey {
height: 2px;
background: grey;
flex-grow: 1;
}
<div class="chart" style="--n:2">
<div class="unit">
<div class="dot-black"></div>
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
</div>
<div class="chart" style="--n:5">
<div class="unit">
<div class="dot-black"></div>
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
</div>
<div class="chart" style="--n:6">
<div class="unit">
<div class="dot-black"></div>
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
</div>
如果您很感兴趣,则可以仅使用背景而不使用html元素来构建整个布局:
.chart {
margin: 0 15px 5px;
height:22px;
background:
/* Small dot */
radial-gradient(circle 4px,black 95%,transparent 100%) left center/8px 8px no-repeat,
/* horizontal line */
linear-gradient(grey,grey) center/100% 2px no-repeat,
/* big dots */
radial-gradient(circle 11px at calc(100% - 11px) 50% ,grey 95%,transparent 100%) left/calc(100%/8) 100%,
/* Our Gradient */
linear-gradient(90deg, rgba(2, 0, 36, 1), rgba(9, 9, 121, 1) 35%, rgba(0, 212, 255, 1)) left/calc(var(--n, 0)* 100%/8) 100% no-repeat;
}
<div class="chart" ></div>
<div class="chart" style="--n:2"></div>
<div class="chart" style="--n:5"></div>
<div class="chart" style="--n:8"></div>
答案 1 :(得分:1)
如果该行上的点数已知并且永远不会改变,则您可以自己计算颜色的颜色并加以实现,以使该线形成一个完整的渐变。
但是,如果您希望将某种东西应用于此类对象,我的建议是使用Javascript。
此功能将为您计算梯度。这很困难,但是有可能在应用程序中实现它,以便它实际上更改CSS。我已经手动完成了这两个单位的计算:
//background: linear-gradient(90deg, rgba(2,0,36,1) 0%, rgba(9,9,121,1) 35%, rgba(0,212,255,1) 100%);
function gradientProgress(gStart,gEnd,at){
//gStart and gEnd are [r,g,b,a,%]
var prog = (at - gStart[4]) / (gEnd[4] - gStart[4]);
var res = [0,0,0,0,100]
for(var a = 0;a<4;a++){
res[a] = Math.round(gStart[a] + (prog * (gEnd[4] - gStart[4])));
}
return res;
}
console.log("First one: " + "[2, 0, 36, 1, 0], [9, 9, 121, 1, 70], [" + (gradientProgress([9,9,121,1,35],[0,212,255,1,100],50)).toString() + "]")
.chart {
display: flex;
flex-direction: row;
justify-content: space-between;
padding: 0 15px;
margin-bottom: 50px;
}
.dot-black {
width: 8px;
height: 8px;
border-radius: 4px;
background: black;
}
.dot-grey {
width: 22px;
height: 22px;
border-radius: 11px;
background: grey;
}
.dot-green {
width: 22px;
height: 22px;
border-radius: 11px;
background: green;
}
.unit {
flex: 1;
display: flex;
align-items: center;
}
.line-black {
height: 2px;
background: black;
}
.line-grey {
height: 2px;
background: grey;
flex-grow: 1;
}
<div class="chart">
<div class="unit gradient" style="background: linear-gradient(90deg, rgba(2,0,36,1) 0%, rgba(9,9,121,1) 70%, rgba(24,24,136,16) 100%);">
<div class="dot-black"></div>
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit gradient" style="background: linear-gradient(90deg, rgba(24,24,136,16) 0%, rgba(0,212,255,1) 100%);">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
<div class="unit">
<div class="line-grey"></div>
<div class="dot-grey"></div>
</div>
</div>