0

There are similar questions like this, but none are identical.

The goal is to scale text as its enclosing rectangle or SVG element scales.

In the example below, the enclosing SVG element is 200x300.

The goal is for the whole element to preserve proportionality, including text.

If you scale by 0.5 to 100x150, the text should scale accordingly.

However, changing the size to 100x150 scales the SVG element correctly, but the text remains the same size and no longer retains the same proportions to the enclosing container.

Code pen: https://codepen.io/anon/pen/BvLZKv?page=1&

<svg width="200" height="300">
  <g>
    <rect x="0" y="0" width="100%" height="100%" fill="red"></rect>
    <text x="50%" y="50%" font-family="Verdana" font-size="20" fill="blue" dominant-baseline="middle" text-anchor="middle">Hello</text>
  </g>
</svg>
Crashalot
  • 33,605
  • 61
  • 269
  • 439

1 Answers1

1

You can create the base svg and scale that entire svg with a scale value, then the text also will be scaled. Here is a demo in which I scaled the svg to .5, the entire svg with the text is scaled to half.

svg {
   transform: scale(.5);
  transform-origin: 0% 0%;
}
<svg width="200" height="300">
<g>
<rect x="0" y="0" width="100%" height="100%" fill="red"></rect>
<text x="50%" y="50%" font-family="Verdana" font-size="82" fill="blue" dominant-baseline="middle" text-anchor="middle">Hello</text>
</g>
</svg>
Crashalot
  • 33,605
  • 61
  • 269
  • 439
Nitha
  • 672
  • 5
  • 10
  • do you happen to know the answer to this? https://stackoverflow.com/questions/53829519/svg-how-to-center-multiple-lines-of-text-evenly-inside-of-element/53829632?noredirect=1#comment94510128_53829632 – Crashalot Dec 18 '18 at 11:15