I'm trying to use a radial gradient to create a border within circle elements that are radio buttons. The basic CSS is shown below. I cannot figure out why the red gradient does not circle the entire circumference of the circles.
As the white color-stop approaches 100%, gaps appear in the red at the top, right, left, and bottom.
Why does this happen, and how would I fix it while still using a radial gradient?
.container {
background: #ddd;
width: 400px;
height: 200px;
padding: 20px;
}
.radio {
display: inline-block;
background: white;
border-radius: 50%;
border: 2px solid transparent;
width: 20px;
height: 20px;
margin-right: 20px;
}
.radio1 { background: radial-gradient(circle closest-side, white 75%, red 100%); }
.radio2 { background: radial-gradient(circle closest-side, white 90%, red 100%); }
.radio3 { background: radial-gradient(circle closest-side, white 95%, red 100%); }
.radio4 { background: radial-gradient(circle closest-side, white 99%, red 100%); }
<div class="container">
<div class="radio"></div>
<div class="radio radio1"></div>
<div class="radio radio2"></div>
<div class="radio radio3"></div>
<div class="radio radio4"></div>
</div>
Also on JSFiddle: https://jsfiddle.net/zvgcs80f/
Your percentage will be converted to pixel relatively to the width/height of your element. In your case, your element is small thus
99%
and100%
will most likely be converted to the same value or very close values and you will have subpixel rendring issue.Instead you can rely on
calc()
where you can easily define the thickness as a pixel value independently of the element size.You need to also adjust
background-origin
and make itborder-box
so that you draw the gradient considering the border area and you will have a perfect circle.Here is an example with big values of border to better illustrate the issue related to
background-origin
The background is drawn on the padding box then it's getting repeated over all the area (border box).
If you disable the repeat you will have this:
Here is if we repeat only on the X axis
And here is what is happening when using 100% for both colors which is similar to your situation and you will better understand why you have coloration only on the corners.
And if we change the origin it's fine: