2

I have a DIV that I'm trying to subtract a hole from the top center of, giving it a kinda card holder effect. The hole needs to allow whatever is behind the main DIV to show through.

This image highlights the idea. Any ideas?

enter image description here

Fuego DeBassi
  • 2,967
  • 6
  • 29
  • 37
  • Sadly, I don't think so. That's showing how you can create more a mask where the hole is the only thing that shows, where what I'm trying to do is the opposite - create a mask where the hole is the only thing that *doesn't* show. – Fuego DeBassi May 02 '22 at 16:06

1 Answers1

3

This can be achieved using the CSS clip-path function.

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

Here are the MDN docs on clip-path.

I generally use Clippy or Boxy SVG for designing the clip-path shape.

If you need to add a box-shadow to an element using clip-path, you'll have to use filter: drop-shadow(...); instead.

Update: 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>

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

Anurag Srivastava
  • 14,077
  • 4
  • 33
  • 43
James Hooper
  • 1,475
  • 13
  • 24
  • that's not a circle! and he does not want his element to be circle he wants a hole masked onto his element... – Mahdiar Mransouri May 02 '22 at 17:18
  • It's not a circle, no, but it demonstrates how to use the CSS property to achieve what they want. The design of the actual circle / card shape can be created using an SVG path creator like the ones I linked. – James Hooper May 02 '22 at 17:19
  • it's not what he wants! you are demonstrating something else , he wants exactly the opposite of that – Mahdiar Mransouri May 02 '22 at 17:21
  • I think OP's comment above explains that they want the hole to be the only thing that doesn't show, meaning it's a cutout like in the picture. I've added a working example to the post. – James Hooper May 02 '22 at 17:30
  • that's the misunderstanding, he wants whatever is under the hole to be visible within the hole, if he clips the element using ```clip-path: circle(10% at 50% 0)``` the browser will cut all his element and only keeps a circle from it, he wants opposite of this, he wants to only extract the circle out of element and keep all element still, I've tried many ways including what you mentioned none of them worked... he needs to some how revert that clip-path... – Mahdiar Mransouri May 02 '22 at 17:35
  • 1
    I didn't say to use `clip-path: circle()`, I said to use `clip-path` and linked some tools that OP could use to create the path they needed. Did you check [the example I posted](https://codesandbox.io/s/elated-meitner-74ozm2?file=/src/styles.css)? The circle is extracted from the card and the elements behind it are visible. – James Hooper May 02 '22 at 17:44
  • 1
    my fault, I've checked all your links but that one, I apologize if I behaved rude and take my down vote back, Good luck and thank you – Mahdiar Mransouri May 02 '22 at 17:56
  • No problem at all, and same to you :) – James Hooper May 02 '22 at 17:59