Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVG shape transparency with solid color as a background

I have an svg container which I used as a background, and inside it a circle is drawn

basically, this is what I've done:

<svg width="200" height="200" style="background-color:green">
    <circle cx="100" cy="100" r="80" stroke="black" stroke-width="2" fill="transparent" />
</svg>

it creates something like this

You can see the circle is transparent but it still has the green background from the svg tag, how can I make the circle really transparent so it can appear as a hole (in this case it will be white since the page background is white), to make it clear this is what I want to display: enter image description here

Is there any way that can make this happen?

like image 879
Eldon Lesley Avatar asked Oct 20 '14 09:10

Eldon Lesley


2 Answers

You could use a mask. Here you can see the red background cut through the rect.

<svg width="200" height="200" style="background-color:red">
    <mask id="mask">
        <rect fill="white" width="100%" height="100%"/>
        <circle cx="100" cy="100" r="80" stroke="black" stroke-width="2" fill="black" />
    </mask>
    <rect mask="url(#mask)" fill="green" width="100%" height="100%"/>
</svg>
like image 111
Robert Longson Avatar answered Nov 18 '22 02:11

Robert Longson


I've added visible example of @Rober solution

.svgClass {
  position: absolute;
  top: 0;
}
<div>
  The text should be visible within the circle
</div>

<div class="svgClass">
  <svg width="100%" height="100%" >
    <mask id="mask">
      <rect fill="white" width="100%" height="100%" fill-opacity="1" />
      <circle cx="100" cy="100" r="90" />
    </mask>
    <rect mask="url(#mask)" fill="green" width="100%" height="100%" fill-opacity="1"/>
  </svg>
</div>
like image 25
Vladyslav Babenko Avatar answered Nov 18 '22 03:11

Vladyslav Babenko