SVG中心文字圈

אVי*_*אVי 47 css svg

我正试图用svg将文本放在一个圆圈中心.

文本的大小将是动态的.

谢谢你的Avi

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>
Run Code Online (Sandbox Code Playgroud)

Wea*_*.py 68

只需添加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>
Run Code Online (Sandbox Code Playgroud)

  • 这不应该是公认的答案,因为它取决于圆本身居中,此外它还使用取决于字体大小的 y 偏移将文本垂直居中。相反,“text”元素的“x”和“y”属性应等于“circle”元素的“cx”和“cy”属性,以及属性“textAnchor='middle'”和“alignmentBaseline=”应将“central”添加到“text”元素中。有关详细信息,请参阅下面的替代答案。 (8认同)
  • 如果他们的圈子不在整个svg中心怎么办?那么你如何将文本置于圆圈中心?例如http://plnkr.co/edit/bFeiKl5B67rjwhM028os?p=preview (4认同)
  • 效果很好!但为什么神奇数字.3em for dy没有其他值? (3认同)
  • @Michael在这种情况下,我会使用JavaScript来使文本居中 (2认同)

sch*_*ebe 14

当你想绘制一个circle不以容器为中心的解决方案时,建议和接受的解决方案是INVALID !

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

如果要绘制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>
Run Code Online (Sandbox Code Playgroud)

备注:我已提议改进已接受的答案,但这已被作者拒绝.所以我没有其他解决方案来发布我的答案!

  • 它适用于大圆圈,但我们没有看到文本没有很好地垂直居中。正如其他答案所说,您必须添加 `alignment-baseline="middle"` 或 `dominant-baseline="middle"` (3认同)

Ric*_*lpe 13

也许,也可以使用alignment-baseline ="middle",text-anchor ="middle":

  <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>
Run Code Online (Sandbox Code Playgroud)

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

干杯

  • 如果使用`alignment-baseline ="central"`,则不需要`dy`属性 (5认同)
  • 如果您使用 `dominant-baseline="central"` 而不是 `alignment-baseline`,它也可以在 Firefox 中使用。(/sf/answers/1496119481/) (5认同)

smo*_*re4 11

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

火狐:

铬合金:

svg { height:140px }
Run Code Online (Sandbox Code Playgroud)
<h4>alignment-baseline AND dominant-baseline settings:</h4>
<svg viewBox="0 0 48 48">
    <circle stroke="darkgray" stroke-width="1px" 
            fill="lightgray"  cx="50%" cy="50%" r="48%"/>
    <line x1="0" y1="50%" x2="100%" y2="50%" stroke="blue" stroke-width="1"/>
    <text text-anchor="middle" alignment-baseline="central" x="50%" y="50%">central</text> 
</svg>
<svg viewBox="0 0 48 48">
    <circle stroke="darkgray" stroke-width="1px" 
            fill="lightgray"  cx="50%" cy="50%" r="48%"/>
    <line x1="0" y1="50%" x2="100%" y2="50%" stroke="blue" stroke-width="1"/>
    <text text-anchor="middle" alignment-baseline="middle" x="50%" y="50%">middle</text> 
</svg>
<svg viewBox="0 0 48 48">
    <circle stroke="darkgray" stroke-width="1px" 
            fill="lightgray"  cx="50%" cy="50%" r="48%"/>
    <line x1="0" y1="50%" x2="100%" y2="50%" stroke="blue" stroke-width="1"/>
    <text text-anchor="middle" dominant-baseline="central" x="50%" y="50%">central</text> 
</svg>
<svg viewBox="0 0 48 48">
    <circle stroke="darkgray" stroke-width="1px" 
            fill="lightgray"  cx="50%" cy="50%" r="48%"/>
    <line x1="0" y1="50%" x2="100%" y2="50%" stroke="blue" stroke-width="1"/>
    <text text-anchor="middle" dominant-baseline="middle" x="50%" y="50%">middle</text> 
</svg>
Run Code Online (Sandbox Code Playgroud)


Vin*_*ius 6

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

这样你就不需要在文本上重复坐标。

<!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>
Run Code Online (Sandbox Code Playgroud)