Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue creating white space inside conic-gradient progress bar

Tags:

html

css

I need to create a progress bar using CSS/HTML. I already have a conic-gradient version working fine with two colors but I want to add a small part of white after the --percentage part and another white part after the --secondary color part.

Here's my code :

[role="progressbar"] {
  --percentage: var(--value);
  --primary: #00B4F4;
  --secondary: #00D0FF;
  --size: 200px;
  width: var(--size);
  aspect-ratio: 1;
  border-radius: 50%;
  position: relative;
  overflow: hidden;
  display: grid;
  place-items: center;
}

[role="progressbar"]::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: conic-gradient(var(--primary) calc(var(--percentage) * 1%), var(--secondary));
  mask-mode: alpha;
  -webkit-mask: radial-gradient(#0000 55%, #000 0);
  -webkit-mask-mode: alpha;
}

[role="progressbar"]::after {
  counter-reset: percentage var(--value);
  content: counter(percentage) '%';
  font-family: Helvetica, Arial, sans-serif;
  font-size: calc(var(--size) / 5);
  color: var(--secondary);
  font-weight: bold;
}
<div role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="--value: 60"></div>

I tried setting the white parts like that :

background: conic-gradient(
    var(--primary) 0 calc(var(--percentage) * 1%),
    white calc(var(--percentage) * 1%) calc(calc(var(--percentage) + 5) * 1%),
    var(--secondary) calc(calc(var(--percentage) + 5) * 1%) calc(calc(100% - var(--percentage) - 5% - 5%) * 1%),
    white calc(calc(100% - var(--percentage) - 5% - 5%) * 1%) 100%
  );

But then nothing is displayed.

like image 930
Nicolas Halberstadt Avatar asked Feb 12 '26 17:02

Nicolas Halberstadt


1 Answers

For a gap of 5%, half of that needs to be taken off each side of the %s.

This snippet defines the gap in a CSS variable.

<style>
  [role="progressbar"] {
    --percentage: var(--value);
    --primary: #00B4F4;
    --secondary: #00D0FF;
    --size: 200px;
    width: var(--size);
    aspect-ratio: 1;
    border-radius: 50%;
    position: relative;
    overflow: hidden;
    display: grid;
    place-items: center;
  }
  
  [role="progressbar"]::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    mask-mode: alpha;
    -webkit-mask: radial-gradient(#0000 55%, #000 0);
    -webkit-mask-mode: alpha;
    --pc: calc(var(--percentage) * 1%);
    --gap: 5%;
    --halfgap: calc(var(--gap) / 2);
    background: conic-gradient(white 0 var(--halfgap), var(--primary) var(--halfgap) calc(var(--pc) - var(--halfgap)), white calc(var(--pc) - var(--halfgap)) calc(var(--pc) + var(--halfgap)), var(--secondary) calc(var(--pc) + var(--halfgap)) calc(100% - var(--halfgap)), white calc(100% - var(--halfgap)) 100%);
  }
  
  [role="progressbar"]::after {
    counter-reset: percentage var(--value);
    content: counter(percentage) '%';
    font-family: Helvetica, Arial, sans-serif;
    font-size: calc(var(--size) / 5);
    color: var(--secondary);
    font-weight: bold;
  }
</style>

<div role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="--value: 60"></div>
<style>

</style>
like image 84
A Haworth Avatar answered Feb 15 '26 10:02

A Haworth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!