CSS 圆 - 可以在边框和背景颜色之间添加透明间隙吗?

标签 css

我正在用 CSS 创建一个圆圈,我希望圆圈的边框和背景颜色之间有一个间隙,以便您可以看到背景。这可能吗?

.container {
  background:url('https://picsum.photos/536/354');
}
.circle {
  width:50px;
  height:50px;
  border-radius:50px;
  border:1px solid red;
  background:red;
}
<div class="container">
<div class="circle"></div>
</div>

期望的结果:

enter image description here

最佳答案

您可以使用伪元素 (.circle::after) 和以下(或类似)设置来实现此目的:

.container {
  height: 100px;
  background: url('https://picsum.photos/536/354');
}

.circle {
  top: 20px;
  left: 20px;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: red;
  position: relative;
  box-sizing: border-box;
}

.circle::after {
  content: '';
  position: absolute;
  box-sizing: border-box;
  border: 3px solid red;
  border-radius: 50%;
  width: calc(100% + 16px);
  height: calc(100% + 16px);
  left: -8px;
  top: -8px;
}
<div class="container">
  <div class="circle"></div>
</div>

关于CSS 圆 - 可以在边框和背景颜色之间添加透明间隙吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59990542/

相关文章:

javascript - Knockout foreach 中的意外空格

Css 特异性——样式表中链接样式的问题

css - 由于边框半径为 : 50%; 的包装元素上的背景颜色,显示锯齿状 "border"

css - 页脚不完全占据页面的 100% ( CSS )

html - 我想在一个 div 上放置多个 div

html - 带有叠加文本的响应图像保持在同一位置

html - MDC-Web对话框宽度

html - 继承父容器的resthight

javascript - 如何使用 jQuery 创建半圆形进度条

html - 设置正确的行高以避免截断文本