我想让一个元素看起来像一个牌匾,通过在它的角落附近钻洞,这样背景图像就可以在后面看到.

a rounded square with holes in the corners

该元素是敏感的,所以它可以是肖像,正方形或风景,但随着其形状的调整,其角落的孔需要保持圆形.孔的大小应该是响应,虽然-最好是成比例的vmin.

这个片段是我的起点.

body {
  margin: 0;
  background-image: url(http://picsum.photos/id/167/1000);
  background-size: cover;
  height: 100svh;
  display: flex;
  justify-content: center;
  align-items: center;
  font-family: cursive;
  font-size: 7vmin;
}

.d1 {
  background-color: goldenrod;
  color: white;
  width: 60vw;
  height: 60vh;
  border-radius: 4vmin;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
}
<div class="d1">Roses are red,<br>violets are blue.</div>

我知道我可以使用SVG背景图像或SVG蒙版接近,但这两种解决方案都不允许孔洞保持圆形.

body {
  margin: 0;
  background-image: url(http://picsum.photos/id/167/1000);
  background-size: cover;
  height: 100svh;
  display: flex;
  justify-content: center;
  align-items: center;
  font-family: cursive;
  font-size: 7vmin;
}

.d1 {
  background-color: goldenrod;
  color: white;
  width: 60vw;
  height: 60vh;
  border-radius: 4vmin;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;

  -webkit-mask: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" preserveAspectRatio="none"><circle cx="15" cy="15" r="5"/><circle cx="185" cy="15" r="5"/><circle cx="15" cy="185" r="5"/><circle cx="185" cy="185" r="5"/></svg>') 0/100% 100%, linear-gradient(#fff,#fff);
  -webkit-mask-composite: destination-out;

  mask: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" preserveAspectRatio="none"><circle cx="15" cy="15" r="5"/><circle cx="185" cy="15" r="5"/><circle cx="15" cy="185" r="5"/><circle cx="185" cy="185" r="5"/></svg>') 0/100% 100%, linear-gradient(#fff,#fff);
  mask-composite: exclude;
}
<div class="d1">Roses are red,<br>violets are blue.</div>

推荐答案

使用径向渐变着色.不需要戴面具

body {
  margin: 0;
  background-image: url(http://picsum.photos/id/167/1000);
  background-size: cover;
  height: 100svh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.d1 {
  --r: 2vmin; /* radius */
  color: white;
  width: 60vw;
  height: 60vh;
  border-radius: calc(2*var(--r));
  border: var(--r) solid goldenrod; /* the outer space*/
  background: 
   radial-gradient(var(--r) at var(--r) var(--r),#0000 98%,goldenrod)
   0 0/calc(100% - 2*var(--r)) calc(100% - 2*var(--r));
  
  box-sizing: border-box;
}
<div class="d1"></div>

Css相关问答推荐

CSS Grid,意想不到的差距

如何在移动屏幕尺寸上包装元素?

Css:有没有更快捷的方式来编写多媒体查询?

Vue3日期 Select 器:禁用向左和向右箭头

如何在CSS中平滑线性平移动画

如何将CSS动画从第一列移动到第二列?

如何在真实文本旁边制作文本阴影,使其看起来像 css 中的重复文本?

如何添加一个类以在开始时触发转换

CSS:如何在嵌套计数器中有多个计数器

CSS背景位置从顶部的给定像素开始?

预期失败时使用toHaveStyle进行测试

包装器内两个跨度之一上的文本溢出省略号

弹性盒项目之间的间距

Bootstrap:在列之间添加边距/填充空间

透明的空心或切出的圆圈

CSS:在图像周围创建白光

通过javascript删除或禁用浏览器的焦点边框

名称-值对的语义和 struct

删除行内块元素中大文本上方和下方的空白

对象拟合不影响图像