Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centering SVG text on IE11 with text-anchor: middle and alignment-baseline: middle / central

I need to perfectly center a text element in an SVG using IE11.

Desired result (all Evergreen browsers): enter image description here

IE11 result: enter image description here

// SVG CODE:

<svg xmlns="http://www.w3.org/2000/svg" width=100% height="100%" viewBox="0 0 130 130">
    <circle fill="dodgerblue" cx="50%" cy="50%" r="65"></circle>
    <text text-anchor="middle"
        alignment-baseline="central"
        font-size="75"
        font-family="Arial"
        x="50%" y="50%">1</text>
  </svg>

Codepen

like image 994
curtybear Avatar asked Feb 16 '18 17:02

curtybear


1 Answers

IE doesn't support alignment-baseline. The best cross-browser way to achieve what you want is to use the dy attribute with a fractional em value.

A value of around dy="0.35" works for Arial.

<svg xmlns="http://www.w3.org/2000/svg" width=100% height="100%" viewBox="0 0 130 130">
  <circle fill="dodgerblue" cx="50%" cy="50%" r="65"></circle>
  <text text-anchor="middle"
        font-size="75"
        font-family="Arial"
        x="50%" y="50%" dy="0.35em">1</text>
</svg>
like image 61
Paul LeBeau Avatar answered Nov 07 '22 22:11

Paul LeBeau