How to create a symbol from another symbol using viewBox properly? - svg

The following SVG does not display as I expect it to.
The brief description/ intent is:
I create a symbol which is a large square comprised of smaller rectangles
I then create "slices" of this large square in smaller symbols using viewBox and preserveAspectRatio
I then display some of these symbols with <use>
The problem is that the "sliced" symbols which touch the right or bottom edge are not full sized, and instead appear as small slivers.
How can I fix this?
What it looks like (click for full image):
Interestingly, if I change the main <symbol> to a <g> instead, I get a very different looking result, which suffers from the same problem
What it should look like (click for full image):
Files available at this gist,
also copied below for your convenience.
This illustrates the problem:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<symbol id="box">
<!--top left-->
<rect x="0" y="0" height="100" width="100"
style="fill:#0000ff" />
<!--top middle-->
<rect x="100" y="0" height="100" width="300"
style="fill:#008888" />
<!--top right-->
<rect x="400" y="0" height="100" width="100"
style="fill:#00ff00" />
<!--middle left-->
<rect x="0" y="100" height="300" width="100"
style="fill:#888800" />
<!--middle middle-->
<rect x="100" y="100" height="300" width="300"
style="fill:#2a2a2a" />
<!--middle right-->
<rect x="400" y="100" height="300" width="100"
style="fill:#ff0000" />
<!--bottom left-->
<rect x="0" y="400" height="100" width="100"
style="fill:#000000" />
<!--bottom middle-->
<rect x="100" y="400" height="100" width="300"
style="fill:#ff0088" />
<!--bottom right-->
<rect x="400" y="400" height="100" width="100"
style="fill:#8800ff" />
</symbol>
<symbol id="box-top-left" viewBox="0 0 100 100" preserveAspectRatio="none">
<use xlink:href="#box" style="overflow:none;" />
</symbol>
<symbol id="box-top-middle" viewBox="100 0 400 100" preserveAspectRatio="none">
<use xlink:href="#box" style="overflow:none;" />
</symbol>
<symbol id="box-top-right" viewBox="400 0 500 100" preserveAspectRatio="none">
<use xlink:href="#box" style="overflow:none;" />
</symbol>
<symbol id="box-middle-left" viewBox="0 100 100 400" preserveAspectRatio="none">
<use xlink:href="#box" style="overflow:none;" />
</symbol>
<symbol id="box-middle-middle" viewBox="100 100 400 400" preserveAspectRatio="none">
<use xlink:href="#box" style="overflow:none;" />
</symbol>
<symbol id="box-middle-right" viewBox="400 100 500 400" preserveAspectRatio="none">
<use xlink:href="#box" style="overflow:none;" />
</symbol>
<symbol id="box-bottom-left" viewBox="0 400 100 500" preserveAspectRatio="none">
<use xlink:href="#box" style="overflow:none;" />
</symbol>
<symbol id="box-bottom-middle" viewBox="100 400 400 500" preserveAspectRatio="none">
<use xlink:href="#box" style="overflow:none;" />
</symbol>
<symbol id="box-bottom-right" viewBox="400 400 500 500" preserveAspectRatio="none">
<use xlink:href="#box" style="overflow:none;" />
</symbol>
</defs>
<use xlink:href="#box-top-right" x="0" y="0" height="100" width="100" />
<use xlink:href="#box-top-middle" x="100" y="0" height="100" width="300" />
<use xlink:href="#box-top-middle" x="400" y="0" height="100" width="100" />
<use xlink:href="#box-middle-right" x="0" y="100" height="300" width="100" />
<use xlink:href="#box-middle-middle" x="100" y="100" height="300" width="300" />
<use xlink:href="#box-middle-middle" x="400" y="100" height="300" width="100" />
<use xlink:href="#box-bottom-right" x="0" y="400" height="100" width="100" />
<use xlink:href="#box-bottom-middle" x="100" y="400" height="100" width="300" />
<use xlink:href="#box-bottom-middle" x="400" y="400" height="100" width="100" />
</svg>
This is what it should look like:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<symbol id="box">
<!--top left-->
<rect x="0" y="0" height="100" width="100"
style="fill:#0000ff" />
<!--top middle-->
<rect x="100" y="0" height="100" width="300"
style="fill:#008888" />
<!--top right-->
<rect x="400" y="0" height="100" width="100"
style="fill:#00ff00" />
<!--middle left-->
<rect x="0" y="100" height="300" width="100"
style="fill:#888800" />
<!--middle middle-->
<rect x="100" y="100" height="300" width="300"
style="fill:#2a2a2a" />
<!--middle right-->
<rect x="400" y="100" height="300" width="100"
style="fill:#ff0000" />
<!--bottom left-->
<rect x="0" y="400" height="100" width="100"
style="fill:#000000" />
<!--bottom middle-->
<rect x="100" y="400" height="100" width="300"
style="fill:#ff0088" />
<!--bottom right-->
<rect x="400" y="400" height="100" width="100"
style="fill:#8800ff" />
</symbol>
<symbol id="box-top-left" viewBox="0 0 100 100" preserveAspectRatio="none">
<use xlink:href="#box" style="overflow:none;" />
</symbol>
<symbol id="box-top-middle" viewBox="100 0 400 100" preserveAspectRatio="none">
<use xlink:href="#box" style="overflow:none;" />
</symbol>
<symbol id="box-top-right" viewBox="400 0 500 100" preserveAspectRatio="none">
<use xlink:href="#box" style="overflow:none;" />
</symbol>
<symbol id="box-middle-left" viewBox="0 100 100 400" preserveAspectRatio="none">
<use xlink:href="#box" style="overflow:none;" />
</symbol>
<symbol id="box-middle-middle" viewBox="100 100 400 400" preserveAspectRatio="none">
<use xlink:href="#box" style="overflow:none;" />
</symbol>
<symbol id="box-middle-right" viewBox="400 100 500 400" preserveAspectRatio="none">
<use xlink:href="#box" style="overflow:none;" />
</symbol>
<symbol id="box-bottom-left" viewBox="0 400 100 500" preserveAspectRatio="none">
<use xlink:href="#box" style="overflow:none;" />
</symbol>
<symbol id="box-bottom-middle" viewBox="100 400 400 500" preserveAspectRatio="none">
<use xlink:href="#box" style="overflow:none;" />
</symbol>
<symbol id="box-bottom-right" viewBox="400 400 500 500" preserveAspectRatio="none">
<use xlink:href="#box" style="overflow:none;" />
</symbol>
</defs>
<use xlink:href="#box" x="0" y="0" height="500" width="500" />
</svg>

Solution found. My error was in not using the viewBox attribute in a <symbol> correctly.
It should be viewBox="minX minY width height"
Instead of viewBox="minx minY maxX maxY"
The working SVG is (click for full size):
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<g id="box">
<!--top left-->
<rect x="0" y="0" height="100" width="100"
style="fill:#0000ff" />
<!--top middle-->
<rect x="100" y="0" height="100" width="300"
style="fill:#008888" />
<!--top right-->
<rect x="400" y="0" height="100" width="100"
style="fill:#00ff00" />
<!--middle left-->
<rect x="0" y="100" height="300" width="100"
style="fill:#888800" />
<!--middle middle-->
<rect x="100" y="100" height="300" width="300"
style="fill:#2a2a2a" />
<!--middle right-->
<rect x="400" y="100" height="300" width="100"
style="fill:#ff0000" />
<!--bottom left-->
<rect x="0" y="400" height="100" width="100"
style="fill:#000000" />
<!--bottom middle-->
<rect x="100" y="400" height="100" width="300"
style="fill:#ff0088" />
<!--bottom right-->
<rect x="400" y="400" height="100" width="100"
style="fill:#8800ff" />
</g>
<symbol id="box-top-left" viewBox="0 0 100 100" preserveAspectRatio="none">
<use xlink:href="#box" style="overflow:none;" />
</symbol>
<symbol id="box-top-middle" viewBox="100 0 300 100" preserveAspectRatio="none">
<use xlink:href="#box" style="overflow:none;" />
</symbol>
<symbol id="box-top-right" viewBox="400 0 100 100" preserveAspectRatio="none">
<use xlink:href="#box" style="overflow:none;" />
</symbol>
<symbol id="box-middle-left" viewBox="0 100 100 300" preserveAspectRatio="none">
<use xlink:href="#box" style="overflow:none;" />
</symbol>
<symbol id="box-middle-middle" viewBox="100 100 300 300" preserveAspectRatio="none">
<use xlink:href="#box" style="overflow:none;" />
</symbol>
<symbol id="box-middle-right" viewBox="400 100 100 300" preserveAspectRatio="none">
<use xlink:href="#box" style="overflow:none;" />
</symbol>
<symbol id="box-bottom-left" viewBox="0 400 100 100" preserveAspectRatio="none">
<use xlink:href="#box" style="overflow:none;" />
</symbol>
<symbol id="box-bottom-middle" viewBox="100 400 300 100" preserveAspectRatio="none">
<use xlink:href="#box" style="overflow:none;" />
</symbol>
<symbol id="box-bottom-right" viewBox="400 400 100 100" preserveAspectRatio="none">
<use xlink:href="#box" style="overflow:none;" />
</symbol>
</defs>
<use xlink:href="#box-top-left" x="0" y="0" height="100" width="100" />
<use xlink:href="#box-top-middle" x="100" y="0" height="100" width="300" />
<use xlink:href="#box-top-right" x="400" y="0" height="100" width="100" />
<use xlink:href="#box-middle-left" x="0" y="100" height="300" width="100" />
<use xlink:href="#box-middle-middle" x="100" y="100" height="300" width="300" />
<use xlink:href="#box-middle-right" x="400" y="100" height="300" width="100" />
<use xlink:href="#box-bottom-left" x="0" y="400" height="100" width="100" />
<use xlink:href="#box-bottom-middle" x="100" y="400" height="100" width="300" />
<use xlink:href="#box-bottom-right" x="400" y="400" height="100" width="100" />
</svg>

Related

Is it possible to center align <rect>'s inside a <g> for SVG?

Is it possible to vertically center align all the rects inside a tag without having to adjust the y attributes on <rect>? (see snippet for example)
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3/org/1999/xlink" viewBox="0 0 225 38" preserveAspectRatio="none" width="100%" height="100%">
<g fill="black">
<rect x="10" y="1" width="6" height="5" />
<rect x="20" y="1" width="6" height="10" />
<rect x="30" y="1" width="6" height="20" />
<rect x="40" y="1" width="6" height="5" />
<rect x="50" y="1" width="6" height="15" />
</g>
</svg>
<h3>desired result</h3>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3/org/1999/xlink" viewBox="0 0 225 38" preserveAspectRatio="none" width="100%" height="100%">
<g fill="black">
<rect x="10" y="8" width="6" height="5" />
<rect x="20" y="6" width="6" height="10" />
<rect x="30" y="1" width="6" height="20" />
<rect x="40" y="8" width="6" height="5" />
<rect x="50" y="4" width="6" height="15" />
</g>
</svg>
No, it's not possible to center align <rect> elements.
But it is possible to center-align <line> elements and give them a stroke-width (note the viewBox is vertically centered around 0):
<svg viewBox="0 -19 225 38" width="100%" height="100%">
<g stroke="black">
<line x1="10" x2="16" stroke-width="5" />
<line x1="20" x2="26" stroke-width="10" />
<line x1="30" x2="36" stroke-width="20" />
<line x1="40" x2="46" stroke-width="5" />
<line x1="50" x2="56" stroke-width="15" />
</g>
</svg>
You could also achieve vertically centered <rect> elements by setting a transform: translate(0, -50%) css rule.
This approach also requires a transform-box: fill-box; (or content-box) property.
All <rect>elements get a y="50%" attribute to start at the vertical center of the svg viewBox.
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3/org/1999/xlink" viewBox="0 0 225 38" width="100%" height="100%" style="border:1px solid #ccc">
<style>
rect {
transform: translate(0, -50%);
transform-origin: center;
transform-box: fill-box;
}
</style>
<g fill="black" >
<rect x="10" y="50%" width="6" height="5" />
<rect x="20" y="50%" width="6" height="10" />
<rect x="30" y="50%" width="6" height="20" />
<rect x="40" y="50%" width="6" height="5" />
<rect x="50" y="50%" width="6" height="15" />
</g>
</svg>
Browser support for transform-box is decent. (See caniuse).
However, if you need legacy browser (e.g. ie11) support, #ccprog's answer is a more robust solution.

Changeing an SVG from 238 to 266 and now I'm stuck

This was originally 238, I want to change it to 266, and now I'm stuck.
I've done this before, but I forgot how to do it.
I know 7 goes into 266 38 times. See, I forgot how to do all this math, how you're supposed to calculate it.
I'm confused now.
It should all fit evenly I think, or does it not?
<svg width="266" height="266" viewBox="0 0 266 266">
<rect x="0" y="0" width="266" height="266" fill="blue" />
<rect x="7" y="7" width="224" height="224" fill="black" />
<rect x="14" y="14" width="210" height="210" fill="red" />
<rect x="21" y="21" width="196" height="196" fill="black" />
<rect x="28" y="28" width="182" height="182" fill="yellow" />
<rect x="35" y="35" width="168" height="168" fill="black" />
<rect x="42" y="42" width="154" height="154" fill="orange" />
<rect x="49" y="49" width="140" height="140" fill="black" />
<rect x="56" y="56" width="126" height="126" fill="lime" />
<rect x="63" y="63" width="112" height="112" fill="black" />
<rect x="70" y="70" width="98" height="98" fill="teal" />
<rect x="77" y="77" width="84" height="84" fill="black" />
<rect x="84" y="84" width="70" height="70" fill="silver" />
<rect x="91" y="91" width="56" height="56" fill="black" />
<rect x="98" y="98" width="42" height="42" fill="#1155cc" />
<rect x="105" y="105" width="28" height="28" fill="black" />
<rect x="112" y="112" width="14" height="14" fill="gold" />
</svg>
Assuming the original looked like this:
<svg width="238" height="238" viewBox="0 0 238 238">
<rect x="0" y="0" width="238" height="238" fill="blue" />
<rect x="7" y="7" width="224" height="224" fill="black" />
<rect x="14" y="14" width="210" height="210" fill="red" />
<rect x="21" y="21" width="196" height="196" fill="black" />
<rect x="28" y="28" width="182" height="182" fill="yellow" />
<rect x="35" y="35" width="168" height="168" fill="black" />
<rect x="42" y="42" width="154" height="154" fill="orange" />
<rect x="49" y="49" width="140" height="140" fill="black" />
<rect x="56" y="56" width="126" height="126" fill="lime" />
<rect x="63" y="63" width="112" height="112" fill="black" />
<rect x="70" y="70" width="98" height="98" fill="teal" />
<rect x="77" y="77" width="84" height="84" fill="black" />
<rect x="84" y="84" width="70" height="70" fill="silver" />
<rect x="91" y="91" width="56" height="56" fill="black" />
<rect x="98" y="98" width="42" height="42" fill="#1155cc" />
<rect x="105" y="105" width="28" height="28" fill="black" />
<rect x="112" y="112" width="14" height="14" fill="gold" />
</svg>
Then all you have to do to scale it up to 266x266 is to update the width and height attributes.
<svg width="266" height="266" viewBox="0 0 238 238">
Because the SVG has a viewBox, the browser will scale the contents automatically for you.
<svg width="266" height="266" viewBox="0 0 238 238">
<rect x="0" y="0" width="238" height="238" fill="blue" />
<rect x="7" y="7" width="224" height="224" fill="black" />
<rect x="14" y="14" width="210" height="210" fill="red" />
<rect x="21" y="21" width="196" height="196" fill="black" />
<rect x="28" y="28" width="182" height="182" fill="yellow" />
<rect x="35" y="35" width="168" height="168" fill="black" />
<rect x="42" y="42" width="154" height="154" fill="orange" />
<rect x="49" y="49" width="140" height="140" fill="black" />
<rect x="56" y="56" width="126" height="126" fill="lime" />
<rect x="63" y="63" width="112" height="112" fill="black" />
<rect x="70" y="70" width="98" height="98" fill="teal" />
<rect x="77" y="77" width="84" height="84" fill="black" />
<rect x="84" y="84" width="70" height="70" fill="silver" />
<rect x="91" y="91" width="56" height="56" fill="black" />
<rect x="98" y="98" width="42" height="42" fill="#1155cc" />
<rect x="105" y="105" width="28" height="28" fill="black" />
<rect x="112" y="112" width="14" height="14" fill="gold" />
</svg>

Alpha Transparent Gradient in Inline SVG Defs Element

I have this CODEPEN and here are my issues:
I am not understanding why the gradient I applied and referenced as my mask fill like so, doesn't render as it should. It should go from fully opaque to fully transparent. For the gradient I am using: http://angrytools.com/gradient/?0_800080,100_450045&0_0,100_100&l_180:
<mask id="myMask" x="0" y="0" width="100%" height="100%">
<rect x="0" y="0" width="100%" height="100%" fill="url(#grad1)" />
</mask>
In addition I don't understand why if I remove the fill="blue" attribute from my use element like so:
<use xlink:href="#myText" mask="url(#myMask)" />
The text appears black as if no gradient was applied. The gradient I defined is purple..
Thanks!
if you just want to apply your gradient to your text, there is no need to use masks, because gradients support the stop-opacity property.
<svg xmlns="http://www.w3.org/2000/svg" width="200px" height="200px">
<defs>
<linearGradient id="lgrad" x1="100%" y1="50%" x2="0%" y2="50%">
<stop offset="0%" style="stop-color:rgb(128,0,128);stop-opacity:0" />
<stop offset="100%" style="stop-color:rgb(69,0,69);stop-opacity:1" />
</linearGradient>
<text x="100" y="120" text-anchor="middle" id="myText" font-size="50">Hello</text>
</defs>
<use xlink:href="#myText" fill="url(#lgrad)" />
</svg>
you only need masks if you want to seperate the opacity from your fills:
<svg xmlns="http://www.w3.org/2000/svg" width="200px" height="200px">
<defs>
<linearGradient id="lgrad" x1="100%" y1="50%" x2="0%" y2="50%">
<stop offset="0" stop-color="black" />
<stop offset="1" stop-color="white" />
</linearGradient>
<mask id="myMask" x="0" y="0" width="100%" height="100%">
<rect x="0" y="0" width="100%" height="100%" fill="url(#lgrad)" />
</mask>
<text x="100" y="120" text-anchor="middle" id="myText" font-size="50">Hello</text>
</defs>
<g mask="url(#myMask)">
<use xlink:href=" #myText" transform="translate(0,-50) " fill="red " />
<use xlink:href="#myText" transform="translate(0,0)" fill="green" />
<use xlink:href="#myText" transform="translate(0,50)" fill="blue" />
</g>
</svg>
masks turn colors into opacity information. going from black(totally transparent) to white (totally opaque)
<svg xmlns="http://www.w3.org/2000/svg" width="200px" height="200px">
<defs>
<mask id="myMask" x="0" y="0" width="100%" height="100%">
<rect x="0" y="0" width="50%" height="50%" fill="white" />
<rect x="50%" y="0" width="50%" height="50%" fill="#333" />
<rect x="0%" y="50%" width="50%" height="50%" fill="#aaa" />
<rect x="50%" y="50%" width="50%" height="50%" fill="white" />
<circle cx="50%" cy="50%" r="15%" fill="black" />
</mask>
<text x="100" y="120" text-anchor="middle" id="myText" font-size="50">Hello</text>
</defs>
<rect x="0" y="0" width="100%" height="100%" fill="beige" />
<g mask="url(#myMask)">
<use xlink:href="#myText" transform="translate(0,-50)" fill="red" />
<use xlink:href="#myText" transform="translate(0,0)" fill="green" />
<use xlink:href="#myText" transform="translate(0,50)" fill="blue" />
</g>
</svg>

svg groups and fill inheritance

My goal is to have the first group of 3 squares be blue and the next group of 3 squares be red. To minimize code, I want to take advantage of grouping in SVG. All the rectangles remain blue. I have tried inline styling on the second group element and on the use element, but that doesn't solve the problem. Thank you for your assistance.. Here's the code:
<svg width="1000" height="800"
xmlns="http://www.w3.org/2000/svg" version="1.1"
xmlns:xlink="http://www.w3.org/1999/xlink">
<style>
.blue {fill:blue;}
.red {fill:red;}
</style>
<g id="g1" class="blue">
<rect x="0" y="0" width="100" height="100" />
<rect x="0" y="105" width="100" height="100" />
<rect x="0" y="210" width="100" height="100" />
</g>
<g class="red">
<use xlink:href="#g1" transform="translate(0,315)" />
</g>
Here are changes from above that now have it working:
<g class="blue">
<g id="g1">
<rect x="0" y="0" width="100" height="100" />
<rect x="0" y="105" width="100" height="100" />
<rect x="0" y="210" width="100" height="100" />
</g>
</g>
<g class="red" transform="translate(0,315)">
<use xlink:href="#g1" />
<!-- <g id="g1">
<rect x="0" y="0" width="100" height="100" />
<rect x="0" y="105" width="100" height="100" />
<rect x="0" y="210" width="100" height="100" />
</g> -->
</g>
... I commented out some of the old svg as well.
Here's the solution in a clearer form (some of the commented code above, actually came from further experimenting).
<svg width="1000" height="800"
xmlns="http://www.w3.org/2000/svg" version="1.1"
xmlns:xlink="http://www.w3.org/1999/xlink">
<style>
.blue {fill:blue;}
.red {fill:red;}
</style>
<g class="blue">
<g id="g1">
<rect x="0" y="0" width="100" height="100" />
<rect x="0" y="105" width="100" height="100" />
<rect x="0" y="210" width="100" height="100" />
</g>
</g>
<g class="red" transform="translate(0,315)">
<use xlink:href="#g1" />
</g>
Looks like styling needs to be done at a level above a group in my case and that the as Robert pointed out will retrieve the previous group as written.

svg: filtering background image, google chrome

I am struggling with an svg to blur background under text on Google Chrome 36.0.1985.125 linux. The svg is like
<svg width="500px" height="500px" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<filter id="myfilter" filterUnits="objectBoundingBox" x="0" y="0" width="100%" height="100%">
<feGaussianBlur result="blurOut" in="BackgroundImage" stdDeviation="2" />
<feBlend in2="blurOut" in="SourceGraphic" mode="normal" />
</filter>
</defs>
<g enable-background="new">
<text x="10" y="100" stroke="none" fill="red" fill-opacity="1" font-size="24">BACKGROUND</text>
<text x="20" y="100" stroke="none" fill="black" fill-opacity="1" font-size="26" filter="url(#myfilter)">text</text>
</g>
</svg>
Fiddle: http://jsfiddle.net/2o2trpc1/
Thus I would like to blur "BACKGROUND" behind "text", but "text" does not appear at all. Can someone please look at this what I am doing wrong? Where can I check that the browser version supports filtering background image?
thanks a lot,
Balazs
You will have to work around the lack of BackgroundImage. There are multiple ways to do that, if your code is as simple as the fiddle you posted something like this could work:
<body>
<svg width="500px" height="500px" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<filter id="blur" filterUnits="objectBoundingBox" x="0" y="0" width="100%" height="100%">
<feGaussianBlur result="blurOut" stdDeviation="2" />
</filter>
</defs>
<g>
<text x="10" y="100" stroke="none" fill="red" fill-opacity="1" font-size="24" filter="url(#blur)">BACKGROUND</text>
<text x="20" y="100" stroke="none" fill="black" fill-opacity="1" font-size="26">text</text>
</g>
</svg>
</body>
See fiddle.
Another option is to use <feImage xlink:href="#background"/> in the filter, instead of using BackgroundImage. This can bring in whatever element you want.
<body>
<svg width="500px" height="500px" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<filter id="myfilter" filterUnits="userSpaceOnUse" x="0" y="0" width="100%" height="100%">
<feImage xlink:href="#background"/>
<feGaussianBlur stdDeviation="3" />
<feBlend in="SourceGraphic" mode="normal" />
</filter>
<text id="background" x="10" y="100" stroke="none" fill="red" fill-opacity="1" font-size="24">BACKGROUND</text>
</defs>
<g>
<text x="20" y="100" stroke="none" fill="black" fill-opacity="1" font-size="26" filter="url(#myfilter)">text</text>
</g>
</svg>
</body>
See fiddle.

Resources