Place text inside of the svg polygon - css

I have a polygon with a shape of a triangle. But the text inside of the polygon is not rendering. Any help is appreciated. This is what I tried so far, and I am not able to figure out why the text isn't rendering. Can I actually place a text inside of a polygon with points like this?
<svg xmlns="http://www.w3.org/2000/svg" width="25" height="25" viewBox="0 0 64 64">
<polygon fill="#F3BC23" stroke="#F3BC23" stroke-width="1" points="30,4 4,60 60,60"/><polygon>
<text x="10" y="14" text-anchor="middle" fill="white" font-size="10"></text>
Thanks in advance.

There are a number of problems with your SVG.
The points attribute in the <polygon> wasn't formatted properly. It is missing commas.
You had a stray <polygon> tag at the end of that line.
You had no text in your <text> element.
The position of the text was not over the polygon. White text on a white background would be invisible.
I also increased the font-size so that the text was visible.
<svg xmlns="http://www.w3.org/2000/svg" width="25" height="25" viewBox="0 0 64 64">
<polygon fill="#F3BC23" stroke="#F3BC23" stroke-width="1" points="30,4,4,60,60,60"/>
<text x="32" y="50" text-anchor="middle" fill="white" font-size="30">X</text>
</svg>

You Can Use the <text>
Here is Your Code
<svg>
<text x="10" y="10">text</text>
</svg>

Related

Rotate svg text around central text position using css

I want to apply a single css rotation transformation to a set of elements in an SVG, such that each element is rotated independently, without having to calculate the centre of each element in the css. For example, I have an SVG that looks like the picture on the left, and want to apply css to achieve the effect on the right
I'm writing the svg myself, and am creating something like this
<svg baseProfile="full" height="200" version="1.1" width="200" xmlns="http://www.w3.org/2000/svg">
<text transform="translate(50 100)" text-anchor="middle">Text 1</text>
<text transform="translate(100 100)" text-anchor="middle">Text 2</text>
</svg>
When I apply a css rotation, e.g. by inserting <style>text {transform: rotate(10deg)}</style>, it seems to overwrite the first transformation, and the rotated text is placed in the top left corner:
I can modify the svg to use `x="X" y="Y" instead of a transform attribute, but that results in the transformation being applied around a different centre instead:
<svg baseProfile="full" height="200" version="1.1" width="200" xmlns="http://www.w3.org/2000/svg">
<style>text {transform: rotate(10deg)}</style>
<text x="50" y="100" text-anchor="middle">Text 1</text>
<text x="100" y="100" text-anchor="middle">Text 2</text>
</svg>
How can I structure the svg so that I can apply a rotation which works independently on each element without overwriting the initial transform?
This is a possible solution:
-The text has x="0" y="0" and is rotated with CSS.
-You put the text in the <defs>.
-You use the text and the <use> element has the x and y values you need.
text{transform:rotate(90deg)}
<svg baseProfile="full" viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
<defs>
<text id="a" text-anchor="middle" >Text 1</text>
<text id="b" text-anchor="middle" >Text 2</text>
</defs>
<use xlink:href="#a" x="50" y="50" />
<use xlink:href="#b" x="100" y="50" />
</svg>
Yet another solution (inspired by the comment of Robert Longson) would wrapping the rotated text in a g element and translate the g
text{transform:rotate(90deg)}
<svg baseProfile="full" viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
<g transform="translate(50,50)"><text text-anchor="middle" >Text 1</text></g>
<g transform="translate(100,50)"><text text-anchor="middle" >Text 2</text></g>
</svg>

Why isn't my SVG text centered vertically in Microsoft Edge?

I have a SVG that is well presented in Firefox and Chrome, but in Edge the text doesn't center vertically.
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64">
<rect fill="#ff0000" x="0" y="0" width="64" height="64" rx="8" ry="8" />
<text font-size="32px" alignment-baseline="middle" dominant-baseline="center" fill="#fff" text-anchor="middle" lengthAdjust="spacingAndGlyphs" textLength="85%" x="32" y="32">
dev
</text>
</svg>
This is what it looks like when its well presented:
And this is how Edge is presenting it:
This is what you can do: You keep alignment-baseline="baseline". This works correctly on Edge. Then you offset the text on y with dy = font-size/4
Also I've removed lengthAdjust="spacingAndGlyphs" textLength="85%"from your code since this was offseting the text to the right in Edge.
svg{width:90vh;}
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64">
<rect fill="#ff0000" x="0" y="0" width="64" height="64" rx="8" ry="8" />
<text font-size="32px" dy="8" dominant-baseline="baseline" text-anchor="middle" fill="#fff" x="32" y="32">dev</text>
</svg>
For the most reliable cross-browser behaviour, you should consider not using the x-baseline attributes at all. Instead, position the text baseline exactly where you want it.
For example:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64">
<rect fill="#ff0000" x="0" y="0" width="64" height="64" rx="8" ry="8" />
<text font-size="32px" fill="#fff" text-anchor="middle" lengthAdjust="spacingAndGlyphs" textLength="85%" x="32" y="39">
dev
</text>
</svg>

Make text inside svg centered using scss

I'm trying to put my text centered to triangle, I tried to make the position absolute and then top:0; right:0; left:0; bottom:0; nothing changed.
<svg class="intro go" viewbox="0 0 200 86">
<text text-anchor="start" x="10" y="30" class="text text-stroke" clip-path="url(#text1)">TEST</text>
<text text-anchor="start" x="10" y="30" class="text text-stroke text-stroke-2" clip-path="url(#text1)">TEST</text>
<defs>
<clipPath id="text1">
<text text-anchor="start" x="10" y="30" class="text">TEST</text>
</clipPath>
</defs>
</svg>
here is my code: https://codepen.io/anon/pen/ePEdEZ
I'm not sure what you meant by "centered to triangle"?
Did you mean centered to rectangle? If so:
Change the coordinates of the text from x="10" y="30" to x="100" y="43".
Then set text-anchor="start" to text-anchor="middle". And add alignment-baseline="middle" to your text. The text is now centered in the box.
Here is a JSFiddle: https://jsfiddle.net/1mLey8v4/
You could also set alignment-baseline and text-anchor by scss.
Looks cool btw!

Image clip to text

How can I clip an image to text? I would prefer a method that is available in any browser. I tried background-clip but it not available in all modern browsers.
Add a clip-path to an image and you'll see a clipped image.
I've made it look more like your example by adding an additional copy of the image with low opacity behind the clipped image.
<svg width="100%" height="100%">
<defs>
<clipPath id="sample" clipPathUnits="userSpaceOnUse">
<text x="85" y="330" font-family="sans-serif" font-size="380" font-weight="bold">I CAM</text>
</clipPath>
</defs>
<image transform="scale(0.4)" xlink:href="http://netghost.narod.ru/gff/sample/images/png/marble24.png" x="20" y="20" width="1419" height="1001" opacity="0.3" />
<image transform="scale(0.4)" xlink:href="http://netghost.narod.ru/gff/sample/images/png/marble24.png" x="20" y="20" width="1419" height="1001" clip-path="url(#sample)" />
</svg>

Is it possible to use a background image on the stroke of an SVG element?

Just as the question asks - I'm trying to figure out whether or not it's possible to use some kind of pattern or repeated background image for the stroke of an SVG path.
Is this doable? Or are you limited to colors only?
TIA!
You can use a <pattern> as a stroke e.g.
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<pattern id="p1" patternUnits="userSpaceOnUse" width="32" height="32">
<image xlink:href="http://phrogz.net/tmp/alphaball-small.png" width="32" height="32" />
</pattern>
</defs>
<rect stroke-width="32" stroke="url(#p1)" width="200" height="200" fill="none"/>
</svg>
The pattern can contain <svg> drawing elements or (as here) an image, or both.
You can use the property stroke-dasharray for "patterns" in the stroke:
<line stroke-dasharray="5, 5" x1="10" y1="10" x2="190" y2="10" />
With this you can specify the length of strokes and gaps between. For some examples have a look at the MDN example page.

Resources