css - 为什么这个径向渐变不完成圆圈?

标签 css radial-gradients

我正在尝试使用径向渐变在作为单选按钮的圆形元素内创建边框。基本的 CSS 如下所示。我不明白为什么红色渐变没有环绕整个圆周。

当白色色标接近 100% 时,顶部、右侧、左侧和底部的红色出现间隙。

为什么会发生这种情况,如何在仍然使用径向渐变的情况下修复它?

.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>

同样在 JSFiddle 上:https://jsfiddle.net/zvgcs80f/

最佳答案

您的百分比将转换为相对于您元素的宽度/高度的像素。在您的情况下,您的元素很小,因此 99%100% 很可能会转换为相同的值或非常接近的值,并且您将遇到子像素渲染问题。

相反,您可以依赖 calc(),在这里您可以轻松地将厚度定义为独立于元素大小的像素值。

您还需要调整 background-origin 并使其成为 border-box 以便绘制考虑边框区域的渐变,您将得到一个完美的圆。

.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;
  background-origin:border-box;
}

.radio1 { background-image: radial-gradient(circle closest-side, white calc(100% - 1px), red 100%); }
.radio2 { background-image: radial-gradient(circle closest-side, white calc(100% - 2px), red 100%); }
.radio3 { background-image: radial-gradient(circle closest-side, white calc(100% - 3px), red 100%); }
.radio4 { background-image: radial-gradient(circle closest-side, white calc(100% - 4px), 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>


这里有一个 border 值很大的例子,可以更好地说明与 background-origin

相关的问题

.container {
  background: #ddd;
  width: 400px;
  height: 200px;
  padding: 20px;
}

.radio {
  display: inline-block;
  background: white;
  border-radius: 50%;
  border: 20px solid rgba(0,0,0,0.2);
  width: 50px;
  height: 50px;
  margin-right: 20px;
 background-image: radial-gradient(circle closest-side, white calc(100% - 4px), red 100%); }
<div class="container">
  <div class="radio radio4"></div>
</div>

背景绘制在填充框上,然后在所有区域(边框框)上重复。

如果你禁用重复你会得到这个:

.container {
  background: #ddd;
  width: 400px;
  height: 200px;
  padding: 20px;
}

.radio {
  display: inline-block;
  background: white;
  border-radius: 50%;
  border: 20px solid rgba(0,0,0,0.2);
  width: 50px;
  height: 50px;
  margin-right: 20px;
 background-image: radial-gradient(circle closest-side, white calc(100% - 4px), red 100%); 
  background-repeat:no-repeat;
 }
<div class="container">
  <div class="radio"></div>
</div>

如果我们只在 X 轴上重复

.container {
  background: #ddd;
  width: 400px;
  height: 200px;
  padding: 20px;
}

.radio {
  display: inline-block;
  background: white;
  border-radius: 50%;
  border: 20px solid rgba(0,0,0,0.2);
  width: 50px;
  height: 50px;
  margin-right: 20px;
 background-image: radial-gradient(circle closest-side, white calc(100% - 4px), red 100%); 
  background-repeat:repeat-x;
 }
<div class="container">
  <div class="radio"></div>
</div>

下面是两种颜色都使用 100% 时发生的情况,这与您的情况类似,您会更好地理解为什么只在 Angular 上着色。

.container {
  background: #ddd;
  padding: 20px;
}

.radio {
  display: inline-block;
  background: white;
  border-radius: 50%;
  border: 20px solid rgba(0, 0, 0, 0.2);
  width: 50px;
  height: 50px;
  margin-right: 20px;
  background-image: radial-gradient(circle closest-side, white 100%, red 100%);
}

.one {
  background-repeat: no-repeat;
}

.two {
  background-repeat: repeat;
}

.three {
  border-width: 5px;
}
.four {
  width:20px;
  height:20px;
  border-width: 2px;
}
<div class="container">
  <div class="radio one"></div>
  <div class="radio two"></div>
  <div class="radio three"></div>
  <div class="radio four"></div>
</div>


如果我们改变原点就没问题:

.container {
  background: #ddd;
  width: 400px;
  height: 200px;
  padding: 20px;
}

.radio {
  display: inline-block;
  background: white;
  border-radius: 50%;
  border: 20px solid rgba(0,0,0,0.2);
  width: 50px;
  height: 50px;
  margin-right: 20px;
 background-image: radial-gradient(circle closest-side, white calc(100% - 4px), red 100%); 
  background-origin:border-box;
 }
<div class="container">
  <div class="radio"></div>
</div>

关于css - 为什么这个径向渐变不完成圆圈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56399252/

相关文章:

html - CSS - 旋转径向渐变

javascript - jQuery 可在不同的无序列表之间排序

javascript - 用js聚焦和取消聚焦div的元素

css - 为什么 Bootstrap 显示带空格的列

html - MVC : Not render css class for certain view?

html - CSS:不透明度导致轻微的背景颜色变化

html - CSS:带有渐变的背景图像 "overlay"

css - 无法在 Safari 中相对于底部定位 CSS 渐变

html - CSS 动画在径向渐变背景下不平滑