text-align=“middle” _only_ in y-direction?

霸气de小男生 提交于 2019-11-29 12:37:32

The baseline-related attributes, as you have discovered, are not universally supported (yet).

One possible solution is to use a method similar to the old CSS trick for vertical centering.

Set the y coord of the text to the vertical centre of the object you want to center on. Then use dy with an em value to adjust the text verically.

<text x="0" y="100" font-family="Verdana" font-size="20pt" dy="0.35em">Some TEXT</text>

The amount of dy won't be 0.5em as you might think because the visual centre of the font won't be exactly half the em height. It will vary from font to font. But once you find a good dy value for a particular font, it should work for any font-size.

Demo here: http://jsfiddle.net/js88W/1/

Try changing the font-size value to confirm it stays centred.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!