Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

svg viewbox should not resize the text fontSize?

I'm using svg with viewBox for fit to the container its working fine ,when i resize the container the svg circle and text are resizing and fit to container but i don't want to resize the text fontSize when i resize the container.I searched a lot but didn't find any valuable suggestions.

I need to resize div and svg circle should resize but text should not resize the font size and also text should move to along with the circle.

Any suggestions should be appreciated.

The following is the SVG i'm using in my application

<div id="container">
    <svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 920 620" preserveAspectRatio="none" style="overflow: hidden; position: relative;">
        <circle cx="100" cy="100" r="100" fill="green"></circle>
        <text x="100" y="100" text-anchor="middle" font="18px &quot;Arial&quot;" stroke="none" fill="#000000" font-size="20px" font-style="italic" font-weight="800" font-family="Times New Roman" opacity="1.0" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); text-anchor: middle; font-style: italic; font-variant: normal; font-weight: 800; font-size: 18px; line-height: normal; font-family: 'Times New Roman'; opacity: 1;">
            <tspan dy="5.828125" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);">Circle</tspan>
        </text>
        </svg>
</div>

Here is the Demo

Note: Resize the jsFiddle

like image 757
Karthi Keyan Avatar asked Aug 13 '13 11:08

Karthi Keyan


People also ask

How do you change text size in SVG?

You can not change the font size or font width because SVG is not a font. It is Scalable Vector Graphics. If you would have some text in your SVG then you could do something with the font from the text element.

What is an SVG viewBox?

The viewBox attribute defines the position and dimension, in user space, of an SVG viewport. The value of the viewBox attribute is a list of four numbers: min-x , min-y , width and height .


1 Answers

Move the viewbox out of the root svg tag and into a nested svg tag. Put the text outside the nested svg tag and the viewbox will not affect the text tag

<div id="container">
    <svg version="1.1" xmlns="http://www.w3.org/2000/svg" style="overflow: hidden; position: relative;">
        <svg viewBox="0 0 920 620" preserveAspectRatio="none">
            <circle cx="100" cy="100" r="100" fill="green"></circle>
        </svg>
        <text x="100" y="100" text-anchor="middle" font="18px &quot;Arial&quot;" stroke="none" fill="#000000" font-size="20px" font-style="italic" font-weight="800" font-family="Times New Roman" opacity="1.0" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); text-anchor: middle; font-style: italic; font-variant: normal; font-weight: 800; font-size: 18px; line-height: normal; font-family: 'Times New Roman'; opacity: 1;">
            <tspan dy="5.828125" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);">Circle</tspan>
        </text>
</svg>

Example

like image 148
Brandon Avatar answered Oct 04 '22 23:10

Brandon