javascript - SVG 文本位于矩形奇怪的布局之上

标签 javascript css html svg

我尝试将 SVG 文本(当然包装在 <text> 中)放置在 SVG <rect> 上.

我想让它看起来像是在我的矩形中居中的文本。

但奇怪的是我到了那里,它看起来不像我想象的那样。

看起来像这样

enter image description here

当我以为它会是那样的时候

enter image description here

这有什么问题吗?

首先我认为等于xy<text><rect>会工作,但就像下图所示

enter image description here

我认为 y="50%" in text 将强制文本在中间某处锚定。但我只能在 y="80%" 处实现如您所见。

原始标记在这里

<svg height="500" width="500" class="ng-scope">

  <svg height="50" width="393.703125" y="0">

    <g>
      <rect x="0" y="0" height="50" width="393.703125" style="fill: #A8A8A8">
      </rect>
      <text font-family="Airal" font-size="45" x="0" y="50%" inline-size="200">
        TEST TEXT IN SVG
      </text>
    </g>
  </svg>

</svg>

最佳答案

y 属性默认应用于文本bottom line - 因此文本的 y 位置和行的 y 位置之间存在差异、矩形或其他形状。

您可以使用 alignment-baseline 属性,例如用 middle 来达到更好的效果。 Here's the w3c description有更多选择。

<svg height="500" width="500" class="ng-scope">

  <svg height="50" width="393.703125" y="0">

    <g>
      <rect x="0" y="0" height="50" width="393.703125" style="fill: #A8A8A8">
      </rect>
      <text font-family="Airal" font-size="45" x="0" y="50%" inline-size="200" alignment-baseline="middle">
        TEST TEXT IN SVG
      </text>
    </g>
  </svg>

</svg>

关于javascript - SVG 文本位于矩形奇怪的布局之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38837633/

相关文章:

javascript - 如何根据下拉值过滤/呈现项目?

javascript - "react doesn' t 存在”在 Node.js 命令提示符中

php - 仅在为空时删除 DIV

iphone - HTML 音频播放导致 iPhone 闪烁

javascript - 如何让 jscolor colorpicker 在 div 上工作?

html - 尝试使用 '•' 在我的导航栏子菜单中放置元素符号 •

css - 如何在 css 中实现无填充 Sprite ?

javascript - JQuery 无法找到分配给新附加的 HTML 字符串的 ID

javascript - 如何在特定的 div 中显示时间(由 JavaScript 生成)?

javascript - 使用 Javascript 以编程方式取消标记 FB 照片