html - 从CSS中的容器div中减去一个洞

标签 html css clip

我有一个 DIV,我试图从顶部中心减去一个洞,给它一种卡片夹效果。该洞需要允许主 DIV 后面的任何内容显示出来。

这张图片突出了这个想法。有什么想法吗?

enter image description here

最佳答案

这可以使用 CSS clip-path 函数来实现。

Here's an example of how to use clip-path (with an approximate shape) .

Here are the MDN docs on clip-path.

我一般用ClippyBoxy SVG用于设计clip-path形状。

如果您需要使用clip-path向元素添加box-shadowyou'll have to use filter: drop-shadow(...);相反。

更新: Here is a working example.

body {
  font-family: sans-serif;
}

#app {
  background: teal;
}

#card {
  position: relative;
  background: red;
  padding: 100px;
  width: 200px;
  height: 400px;
  clip-path: path( "M 93.385 67.118 L 179.825 67.118 C 179.825 67.13 179.825 67.142 179.825 67.154 C 179.825 81.302 191.295 92.772 205.443 92.772 C 219.591 92.772 231.061 81.302 231.061 67.154 C 231.061 67.142 231.061 67.13 231.061 67.118 L 321.217 67.118 C 331.461 67.118 339.766 75.423 339.766 85.667 L 339.766 284.691 C 339.766 294.935 331.461 303.24 321.217 303.24 L 94.385 303.24 C 84.141 303.24 75.836 294.935 75.836 284.691 L 75.836 85.667 C 75.836 75.423 84.141 67.118 94.385 67.118 Z");
}

#bg {
  background: orange;
  padding: 50px;
  position: absolute;
}
<div id="bg">
  <p>This is the background behind the card</p>
</div>
<div id="card">
  <h1>Hello Vanilla!</h1>
  <div>
    We use the same configuration as Parcel to bundle this sandbox, you can find more info about Parcel
    <a href="https://parceljs.org" target="_blank" rel="noopener noreferrer">here</a>.
  </div>
</div>

更新 2: Here is another working example with elements visible behind the card

关于html - 从CSS中的容器div中减去一个洞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72089285/

相关文章:

css - 链接(屏幕)中 Sifr 跨度的奇怪 margin 行为

用上限和下限(钳位、剪裁、阈值)替换列表值的 Pythonic 方法?

android - 非透明像素上的 ImageView 颜色过滤器。夹子

html - <hr> 行标签之间的间距调整

html - AWS API网关: why is the invoke url returning HTML code instead of actual web page?

JavaScript 变量和用户输入

jquery - 不透明度为 1 到 0 的动画剪辑 jq​​uery 效果

html - chalice 技术 - 左容器 float 问题

html - 导航栏不显示内联

javascript - 有条件地强制浏览器缩放