局部背景透明

104 阅读1分钟

常规解决办法是在背景上添加一个元素,使该元素的背景色与底色保持一致,从而达到视觉上的局部透明效果。

.wrapper {
  width: 200px;
  height: 200px;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #ccc;
}

.content {
  position: relative;
  width: 100px;
  height: 100px;
  background: #fff;
}

.content::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #ccc;
}

如果底色是渐变色,又该如何解决呢?使用CSS径向渐变!

.wrapper {
  width: 200px;
  height: 200px;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #ccc;
}

.content {
  width: 100px;
  height: 100px;
  background-image: radial-gradient(
    circle at 50% 50%,
    transparent 10px,
    #fff 11px
  ); /* 创建圆形透明区域 */
}