css - 圆圈中的 SVG 中心文本

标签 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>

最佳答案

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>

关于css - 圆圈中的 SVG 中心文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28128491/

相关文章:

html - 插入 div 以仅包含滚动条

jquery - 将样式应用于 kendoui ListView 所选元素

css - 如何让 LESS 将 CSS 规则与逗号结合起来

css - <symbol> 的 <use> 元素的填充颜色不会改变颜色

javascript - 如何根据实时数据平滑地动画前端表示?

jquery - 具有 [属性 ="value"] 规则的 CSS 不适用于 jquery-svg 图像

javascript - SVG 可聚焦属性不起作用

html - 为什么第二个 flexbox 容器(里面有一个 SVG)向上移动?

javascript - 同时设置 scrollLeft 和 scrollTop

jquery - 切换 div 时填充空白区域