首页 > 解决方案 > 具有可变边框宽度的css圆形掩码

问题描述

我想知道是否有办法实现类似使用图像遮罩显示的图像? 示例图像

左上角的蓝色效果不是必需的。我想得到圆圈遮罩。

任何帮助将不胜感激。

标签: cssmask

解决方案


使用具有相同背景的两个元素并调整背景大小/背景位置以创建这种效果的错觉:

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


推荐阅读