SVG中心文字圈

时间:2015-01-24 17:57:47

标签: css svg

我正试图用svg。

将文字放在一个圆圈中心

文字的大小将是动态的。

感谢的 阿维

plunker

我的代码:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" style="shape-rendering:geometricPrecision; text-rendering:geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd; clip-rule:evenodd" viewBox="0 0 500 500">

  <g id="UrTavla">
      <circle style="fill:url(#toning);stroke:#010101;stroke-width:1.6871;stroke-miterlimit:10;" cx="250" cy="250" r="245">

      </circle>
      <text x="50%" y="50%" stroke="#51c5cf" stroke-width="2px" dy=".3em"> Look, I’m centered!Look, I’m centered! </text>
  </g>
</svg>

6 个答案:

答案 0 :(得分:59)

只需将text-anchor="middle"添加到text元素。

<强> Plunker

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" style="shape-rendering:geometricPrecision; text-rendering:geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd; clip-rule:evenodd"
viewBox="0 0 500 500">
  <g id="UrTavla">
    <circle style="fill:url(#toning);stroke:#010101;stroke-width:1.6871;stroke-miterlimit:10;" cx="250" cy="250" r="245">
    </circle>
    <text x="50%" y="50%" text-anchor="middle" stroke="#51c5cf" stroke-width="2px" dy=".3em">Look, I’m centered!Look, I’m centered!</text>
  </g>
</svg>

答案 1 :(得分:13)

也许,也可以使用alignment-baseline =&#34; middle&#34;,text-anchor =&#34; middle&#34;:

       

  <text x="50%" y="50%" stroke="#51c5cf" stroke-width="2px" dy=".3em" text-anchor="middle" alignment-baseline="middle"> Look, I’m centered!Look, I’m centered! </text>

这是一个很好的资源: http://apike.ca/prog_svg_text_style.html

干杯

答案 2 :(得分:7)

当您要绘制不以容器为中心的circle时,建议和接受的解决方案是无效的!

仅当SVG元素包含以viewPort为中心的圆时,才能在x="50%"标签上使用y="50%" text

如果要绘制3个圆,则还必须更改(x,y)文本坐标,以使其等于(cx,cy)圆坐标,如下例所示。

<svg height="350" width="350">
    <circle cx="110" cy="110" r="100"
            stroke="red"
            stroke-width="3"
            fill="none"
            />
    <text x="110" y="110" 
          text-anchor="middle"
          stroke="red"
          stroke-width="1px"
          > Label
    </text>
    <circle cx="240" cy="110" r="100" 
            stroke="blue" 
            stroke-width="3"
            fill="none"
            />
    <text x="240" y="110" 
          text-anchor="middle" 
          stroke="blue" 
          stroke-width="1px"
          > Ticket
    </text>
    <circle cx="170" cy="240" r="100" 
            stroke="green" 
            stroke-width="3" 
            fill="none"
            />
    <text x="170" y="240" 
          text-anchor="middle" 
          stroke="green" 
          stroke-width="1px"
          > Vecto
    </text>
</svg>

备注:我已经提出要改善已接受的答案,但这已被作者拒绝。因此,我没有其他解决方案可以发布我的答案!

答案 3 :(得分:1)

一种适用于非中心圆的更简单的解决方案是将圆和文本放入已翻译的组中。

这样,您就无需在文本上重复坐标。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Centered texts</title>
  </head>

  <body ng-controller="MainCtrl">
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 500 500">
      <g transform="translate(300, 300)" >
        <circle fill="none" stroke="black" stroke-width="1px" r="120"/>
        <text stroke="blue" stroke-width="1px" text-anchor="middle" alignment-baseline="central">Look, I’m centered!</text>
      </g>
      
      <g transform="translate(150, 150)" >
        <circle fill="none" stroke="black" stroke-width="1px" r="120"/>
        <text stroke="blue" stroke-width="1px" text-anchor="middle" alignment-baseline="central">Look, I’m also centered!</text>
      </g>
    </svg>
  </body>
</html>

答案 4 :(得分:1)

使用alignment-baseline="central"的浏览器之间的行为不一致。值得注意的是,Chrome可以正确定位,但Firefox不能正确定位。如果您使用dominant-baseline="central",则两者将正确显示。

      <svg viewBox="0 0 500 500" role="img" xmlns="http://www.w3.org/2000/svg">
        <g id="myid">
        <circle stroke="darkgray" stroke-width="1px" fill="lightgray"  cx="250" cy="250" r="245"/>
        <text style="font: bold 10rem sans-serif;" text-anchor="middle" dominant-baseline="central" x="50%" y="50%">Centered</text> 
        </g>     
      </svg> 

答案 5 :(得分:0)

使文本在SVG圆中居中非常容易。

<svg height="300" width="300">
      <circle cx="120" cy="120" r="30%" fill="#117da9" />
      <text x="50" y="120" fill="white">Center Text in SVG Circle</text>
</svg>

您需要做的就是更改标签的x和y值,直到给定的文本在圆心对齐。 例如,这里的x和y值为 x =“ 50” y =“ 120”