css 边框宽度可变的圆形遮罩

css circle mask with variable border width

我想知道是否有办法实现像使用图像遮罩显示的图像那样的效果?

不需要左上角的蓝色效果。我想得到圆形遮罩。

任何帮助将不胜感激。

使用两个具有相同背景的元素并调整 background-size/background-position 以创建这种效果的错觉:

.box {
  width:200px;
  height:200px;
  border-radius:50%;
  background-image:url(https://picsum.photos/id/1057/800/800);
  background-size:auto 220px;
  background-position:top left;
  position:relative;
}
.box:before {
  content:"";
  position:absolute;
  bottom:-20px;
  left:0;
  width:100px;
  height:100px;
  background-image:inherit;
  background-size:inherit;
  background-position:bottom left;
  border-radius:inherit;
}
<div class="box">

</div>

这里有一个更通用的方法,使用 CSS 变量来轻松控制它:

.box {
  --b:-20px; /* Bottom of the pseudo element*/
  --l:10px;  /* Left of the pseudo element*/
  --s:2; /*size of the pseudo element (as a scale factor)*/
  width:200px;
  height:200px;
  border-radius:50%;
  background-image:url(https://picsum.photos/id/1057/800/800);
  background-size:auto calc(100% - var(--b));
  background-position:top left;
  position:relative;
  display:inline-block;
}
.box:before {
  content:"";
  position:absolute;
  bottom:var(--b);
  left:var(--l);
  width:calc(100%/var(--s));
  height:calc(100%/var(--s));
  background-image:inherit;
  background-size:auto calc(100%*var(--s) - var(--b));
  background-position:bottom 0 left calc(-1 * var(--l));
  border-radius:inherit;
}
<div class="box"></div>

<div class="box" style="--s:3;l:50px;"></div>

<div class="box" style="--s:1.5;l:50px;--b:-10px"></div>

并且您可以轻松添加边框:

.box {
  --b:-20px; /* Bottom of the pseudo element*/
  --l:10px;  /* Left of the pseudo element*/
  --s:2; /*size of the pseudo element (as a scale factor)*/
  width:200px;
  height:200px;
  border-radius:50%;
  background-image:url(https://picsum.photos/id/1057/800/800);
  background-size:auto calc(100% - var(--b));
  background-position:top left;
  position:relative;
  display:inline-block;
  border:2px solid blue;
  box-sizing:border-box;
}
.box:before {
  content:"";
  position:absolute;
  bottom:var(--b);
  left:var(--l);
  width:calc(100%/var(--s));
  height:calc(100%/var(--s));
  background-image:inherit;
  background-size:auto calc(100%*var(--s) - var(--b));
  background-position:bottom 0 left calc(-1 * var(--l));
  border-radius:inherit;
  border:inherit;
  box-sizing:inherit;
}
<div class="box">

</div>

<div class="box" style="--s:3;l:50px;">

</div>

<div class="box" style="--s:1.5;l:50px;--b:-10px">

</div>