Home > Mobile >  CSS Half Circle border with gradient color
CSS Half Circle border with gradient color

Time:07-16

I have half circle

.progress-semi-circle{
  position: relative;
  display:inline-block;
  padding: 20px;
  text-align: center;
}
.barOverflow{ /* Wraps the rotating .bar */
  position: relative;
  width: 180px; height: 90px; /* Half circle (overflow) */
  margin-bottom: -14px; /* bring the numbers up */
  overflow: hidden;
}
.bar-semi-circle{
  position: absolute;
  top: 0; left: 0;
  width: 180px; height: 180px; /* full circle! */
  border-radius: 50%;
  box-sizing: border-box;
  border: 25px solid #262925;     /* half gray, */
  border-bottom-color: #0bf;  /* half azure */
  border-right-color: #0bf;
  
}
<div >
  <div >
    <div ></div>
  </div>
  <span>64</span>%
</div>

Now I need the border color is using gradient color.

border: 25px solid #262925; 

Is there any possible to set the border using gradient color?

CodePudding user response:

You can achieve this by using two background images. one for padding-box and another for border-box. Provide background colors to padding-box for hiding them, and provide your preferred colors to border-box. For example

- background: 
    linear-gradient(var(--color-bg), var(--color-bg)) padding-box,
    linear-gradient(var(--direction), var(--border-color-1), var(--border-color-2)) border-box;

.progress-semi-circle {
  position: relative;
  display: inline-block;
  padding: 20px;
  text-align: center;
}

.barOverflow {
  /* Wraps the rotating .bar */
  position: relative;
  width: 180px;
  height: 90px;
  /* Half circle (overflow) */
  margin-bottom: -14px;
  /* bring the numbers up */
  overflow: hidden;
}

.bar-semi-circle {
  position: absolute;
  top: 0;
  left: 0;
  width: 180px;
  height: 180px;
  /* full circle! */
  box-sizing: border-box;
  background: linear-gradient(white, white) padding-box, linear-gradient(to right, #0bf, #262925) border-box;
  border-radius: 50%;
  border: 25px solid transparent;
}
<div >
  <div >
    <div ></div>
  </div>
  <span>64</span>%
</div>

Hope this helps you!

CodePudding user response:

.progress-semi-circle {
  display: grid;
  grid-template-columns: 1fr 20px 1fr;
  grid-template-rows: 1fr 20px;
  width: 180px;
  height: 90px;
}

.bar-semi-circle {
  grid-column: 1 / -1;
  grid-row: 1 / -1;
  box-sizing: border-box;
  background: conic-gradient( from 0deg, #0bf, #262925 25%, #0bf);
  clip-path: url(#clip);
}

.label {
  grid-column: 2 / -2;
  grid-row: -2 / -1;
}
<div >
    <div ></div>
  <div >
  <span>64</span>%
  </div>
</div>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 180 90">
  <clipPath id="clip">
    <path d="m0,90a90,90,0,1,1,180,0h-30a60,60,0,1,0,-120,0z"/>
  </clipPath>
</svg>

CodePudding user response:

You can do it like below:

.progress-semi-circle {
  --b: 20px; /* border size */
  --a: 64;   /* percentage*/

  position: relative;
  width: 180px;
  aspect-ratio: 2;
  display: inline-grid;
  grid-auto-flow: column;
  border-radius: 999px 999px 0 0;
  overflow: hidden;
  place-content: end center;
  background: radial-gradient(farthest-side at bottom, #0000 calc(99% - var(--b)), #262925 calc(100% - var(--b)));
}

.progress-semi-circle:before {
  content: "";
  position: absolute;
  inset: 0;
  -webkit-mask:
     conic-gradient(from calc(var(--a)*1.8deg - 90deg) at bottom ,#000 50%, #0000 0),
     radial-gradient(farthest-side at bottom, #0000 calc(99% - var(--b)), #000 calc(100% - var(--b)));
  -webkit-mask-composite: destination-in;
          mask-composite: intersect;
  background: linear-gradient(red,blue); /* your gradient */
}
<div >
  <span>64</span>%
</div>

<div  style="--a:50">
  <span>50</span>%
</div>

<div  style="--a:20">
  <span>20</span>%
</div>

  •  Tags:  
  • css
  • Related