Search code examples
htmlcssmaskclip

Circular hole in a <div>


I am looking for a way to create a square div of relative size, say, 70% x 70% with a background color and a circular hole in it (so that the background can be seen behind it) of the same size so that the sides of the div are tangent.


Solution

  • You could use radial-gradient to achieve this.

    html,
    body {
      height: 100%;
      margin: 0;
      background: url(http://www.lorempixel.com/600/400) 100% 100%;
    }
    div {
      position: relative;
      width: 70%;
      height: 70%;
      background: -webkit-radial-gradient(50% 50%, circle, transparent 10%, #000 10%);
      background: -moz-radial-gradient(50% 50%, circle, transparent 10%, #000 10%);
      background: radial-gradient(50% 50%, circle, transparent 10%, #000 10%);
      margin: 0 auto;
    }
    <div></div>


    Here's an svg approach.

    body {
      background: url(http://www.lorempixel.com/600/400/);
      background-size: 100% 100%;
      margin: 0;
    }
    <svg viewBox="0 0 400 200" width="400" height="200">
      <path d="M0,0 L400,0 L400,200 L0,200z M200,100 m-50,0 a50,50 0 1,0 100,0 a50,50 0 1,0 -100,0z" fill="black" />
    </svg>