最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why svg text disappears when setting x and y to 0? - Stack Overflow

programmeradmin4浏览0评论

I just started reading about svg and I came up with the following question

I am creating a simple svg with a text inside as shown below.

From my reading I understood that x and y of the text tag declares the position of the text inside the svg space.

Why when I set both x and y to 0 the text does not appear and when I change x and y to 10 for example it is displayed? Isn't x=0 and y=0 meaning the top left corner of the svg tag?

Thanks

<svg width="200" height="100">
   <text x="0" y="0">hello</text>
</svg>

I just started reading about svg and I came up with the following question

I am creating a simple svg with a text inside as shown below.

From my reading I understood that x and y of the text tag declares the position of the text inside the svg space.

Why when I set both x and y to 0 the text does not appear and when I change x and y to 10 for example it is displayed? Isn't x=0 and y=0 meaning the top left corner of the svg tag?

Thanks

<svg width="200" height="100">
   <text x="0" y="0">hello</text>
</svg>
Share Improve this question asked Feb 14, 2014 at 10:53 NikosDimNikosDim 1,5283 gold badges25 silver badges42 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

You're correct, (0,0) is indeed the top left corner of the SVG area (at least before you start transforming the coordinates).

However, your text element <text x="0" y="0">hello</text> is positioned with the leftmost end of its baseline at (0,0), which means the text will appear entirely off the top of the SVG image.

Try this: change your text tag to <text x="0" y="0">goodbye</text>. You should now be able to see the descending parts of the 'g' and 'y' at the top of your SVG.

You can shift your text down by one line if you provide a y coordinate equal to the line height, for example:

<svg width="200" height="100">
   <text x="0" y="1em">hello</text>
</svg>

Here's a JSFiddle link for you to play with.

To make <text> behave in a more standard way, you can use dominant-baseline: hanging like so:

<text x="0" style="dominant-baseline: hanging;">Hello</text>

You can see examples of different values of this property here.

发布评论

评论列表(0)

  1. 暂无评论