Search code examples
svgcss-positionfluid-layoutmasking

Issue perfectly masking a div using an SVG frame


I'm trying to mask a div with an SVG 'frame'. Despite positioning the SVG absolutely and setting height/width to 100%, there's still slivers of the parent div visible around the bottom and right edges.

html

<div class="container">
    <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" 
    xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
    viewBox="-144 2 502 609" style="enable-background:new -144 2 
    502 609;" xml:space="preserve" preserveAspectRatio="none">

    <style type="text/css"></style>

    <path class="st0" d="M-144,2v608h501.2V2H-144z M354.5,608.5l- 
    496.2-12.2C-147,201.8-62.3,4.5,112.5,4.5S367.8,205.8,354.5,608.5z" 
    />
    </svg>  

</div>

css

html,
body {
  height: 100%;
  width: 100%;
  margin:0;
  padding:0;
    }

.container {
  width: 50%;
  height: 50%;
  top: 25%;
  margin:auto;
  background: pink;
  position: relative;

   }

svg {
  position: absolute;
  top:0;
  left:0;
  height: 100%;
  width: 100%;
}

.st0{
  fill: white;
}

https://jsfiddle.net/samseurynck/b2x58ahc/

I'd like the white SVG shape to completely mask out the pink div behind it, with no slivers of the div showing (on the bottom and right sides) like it is now. The slivers seem to scale up with the browser. I'm curious if this is even possible with SVG if the way I've tried it isn't working.


Solution

  • I've made a few changes to the path. While the viewBox="-144 2 502 609"the path goes to 501.2 instead of 502 (in x) and to 608.5 instead of 609 (in y). I've changed those numbers in your path.

    html,
    body {
      height: 100%;
      width: 100%;
      margin:0;
      padding:0;
    }
    
    .container {
      width: 50%;
      height: 50%;
      top: 25%;
      margin:auto;
      background: pink;
      position: relative;
    
       }
    
    svg {
      position: absolute;
      top:0;
      left:0;
      height: 100%;
      width: 100%;
    }
    
    .st0{
      fill: white;
    }
    <div class="container">
    
    <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
    	 viewBox="-144 2 502 609" style="enable-background:new -144 2 502 609;" xml:space="preserve" preserveAspectRatio="none">
    <style type="text/css">
    
    </style>
    <path class="st0" d="M-144,2v609h502V2H-144z M354.5,609l-496.2-12.2C-147,201.8-62.3,4.5,112.5,4.5S367.8,205.8,354.5,609z"
    	/>
    </svg>
    
    </div>