如何围绕现有的圆圈边界创建多个边界

How to create multiple borders around existing border of circle

我正在使用 border-radius: 50%;border: 400px solid rgba(255, 255, 255, .5); 在 css 中创建一个半透明圆圈。

围绕这个圆圈,我希望有另一个完全透明的边框(假设 10 像素),只有另一个边框(10 像素)又是半透明的。

这就是我创建圈子的方式:

div.circle {
  background: rgba(255, 255, 255, .5);
  height: 400px;
  width: 400px;
  border-radius: 50%;
  margin: auto;
  margin-top: 10px;
}
<div class="circle"></div>

我需要做什么才能围绕现有边框创建另一个边框,然后再创建另一个边框?

您可以使用简单的边框和 clip the backgroundcontent-box 在填充区域创建透明部分:

div.circle {
  background: rgba(255, 255, 255, .5) content-box;
  padding: 10px;
  height: 180px;
  width: 180px;
  box-sizing: border-box;
  border-radius: 50%;
  margin:10px auto;
  border: 10px solid rgba(255, 255, 255, .5);
}

body {
  background: pink;
}
<div class="circle"></div>

你也可以考虑radial-gradient

div.circle {
  background: 
    radial-gradient(farthest-side, 
      rgba(255, 255, 255, .5) calc(100% - 20px),transparent calc(100% - 20px),
      transparent calc(100% - 10px),rgba(255, 255, 255, .5) calc(100% - 10px));
  height: 180px;
  width: 180px;
  box-sizing: border-box;
  border-radius: 50%;
  margin:10px auto;
}

body {
  background: pink;
}
<div class="circle"></div>

您可以轻松缩放到任意数量的边框:

div.circle {
  background: 
    radial-gradient(farthest-side,
      #fff        calc(100% - 61px),transparent calc(100% - 60px), 
      transparent calc(100% - 51px),#fff        calc(100% - 50px),
      #fff        calc(100% - 41px),transparent calc(100% - 40px),
      transparent calc(100% - 31px),#fff        calc(100% - 30px),
      #fff        calc(100% - 21px),transparent calc(100% - 20px),
      transparent calc(100% - 11px),#fff        calc(100% - 10px));
  height: 180px;
  width: 180px;
  box-sizing: border-box;
  border-radius: 50%;
  margin:10px auto;
}

body {
  background: pink;
}
<div class="circle"></div>

您可以使用 ::before:after 伪元素,如我的代码片段所示。我都添加了,但对于您的要求,一个可能就足够了:

(编辑:我改变了位置参数,使伪元素居中。这样你只需要改变 heightwidth 如果你想要不同的尺寸)

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: #7a4;
}

div.circle {
  background: rgba(255, 255, 255, 0.5);
  height: 400px;
  width: 400px;
  border-radius: 50%;
  margin: auto;
  margin-top: 100px;
  position: relative;
}

.circle::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  height: 440px;
  width: 440px;
  border-radius: 50%;
  border: 20px solid;
  border-color: rgba(255, 255, 255, 0.5);
}

.circle::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  height: 520px;
  width: 520px;
  border-radius: 50%;
  border: 20px solid;
  border-color: rgba(255, 255, 255, 0.5);
}
<div class="circle"></div>