I’m trying to create a circular progress bar where the variables can be controlled with an inline style. The progress bar will have two different bars that should be added together but are still different colors.
These variables work on their own:
.card svg circle {
width: 100%;
height: 100%;
fill: none;
stroke: #f0f0f0;
stroke-width: 10;
stroke-linecap: round;
--laresult: calc(625px - (625px * var(--lapercent)) / 34);
--saresult: calc(625px - (625px * var(--sapercent)) / 34);
}
.card svg circle.la {
stroke-dasharray: 625px;
stroke-dashoffset: var(--laresult);
stroke: blue;
}
.card svg circle.sa {
stroke-dasharray: 625px;
stroke-dashoffset: var(--saresult);
stroke: pink;
}
But I don’t want the lines to overlap, I want them to appear like they’re stacked. So, For the second progress bar, I’d like to add the two together. However, it breaks when I try to add them:
.card svg circle {
width: 100%;
height: 100%;
fill: none;
stroke: #f0f0f0;
stroke-width: 10;
stroke-linecap: round;
--laresult: calc(625px - (625px * var(--lapercent)) / 34);
--saresult: calc(625px - (625px * var(--sapercent)) / 34);
}
.card svg circle.la {
stroke-dasharray: 625px;
stroke-dashoffset: var(--laresult);
stroke: blue;
}
.card svg circle.sa {
stroke-dasharray: 625px;
stroke-dashoffset: calc(var(--saresult) + var(--laresult));
stroke: pink;
}
2
Answers
I figured this out. There were two issues:
Like Temani Afif said, adding the two variables was incorrect anyways. I needed a new formula.
The second issue was that progress bar didn't have access to the first variable since I only declared it inline in the first progress bar. Declaring both in both progress bars worked:
Seeing the HTML in the codepen I see the variables are defined in each element:
The problem I think is that the element that needs both variables has no way to "see" the value of the other variable because it is outside the scope (
--laresult
can’t be calculated because--lapercent
is not defined). In the inspector, it is visible that it is not defined:If you define the variables in a parent element this way…
… the variable appears as defined: