为什么svg中的文字不显示?

时间:2012-10-16 19:46:57

标签: svg

我有一个简单的svg元素,

<svg height="100" width="710">
  <rect width="700" height="50"/>
    <rect width="70" height="50" style="fill: rgb(0, 0, 255);">
      <text y="0" style="fill: white;">-0.123994</text>
    </rect>
    <rect width="70" height="50" style="fill: rgb(255, 0, 0);" transform="translate(630,0)">
    <text y="50">0.387869</text>
  </rect>
</svg>

为什么text元素中没有一个出现?这就是它所显示的: enter image description here

1 个答案:

答案 0 :(得分:11)

您无法将text标记放在rect中。如果要在rect元素中显示文本,则应将它们放在一个组中。

<svg height="100" width="710">
    <g>
    <rect width="70" height="50" style="fill: rgb(0, 0, 255);"></rect>
      <text y="0" style="fill: white;">-0.123994</text>
    </g>
    <g>
    <rect width="70" height="50" style="fill: rgb(255, 0, 0);" transform="translate(630,0)"></rect>
    <text y="50">0.387869</text>
   </g>

</svg>

请调整文本的坐标以显示在rect。