如何在 IE9 中垂直居中 SVG 文本
为了在SVG中垂直对齐文本,必须使用主导基线
属性。 这已经在SO上进行了讨论(在svg 中对齐文本,是规格。
My problem is with IE9 which apparently does not support 主要基础
和一堆其他东西。
您对如何近似主导基线有任何想法:IE9中的中央
吗?
这是在FF和Chrome中起作用的样本。它在IE9,Opera 11中不起作用。Windows上的Safari不支持Central
,而是支持中间
仍然很好。
<?xml version="1.0"?>
<svg width="300" height="300" xmlns="http://www.w3.org/2000/svg">
<path d="M 10 100 h 290" stroke="blue" stroke-width=".5" />
<text x="40" y="100" font-size="16" style="dominant-baseline: auto;">
XXX dominant-baseline: auto; XXX
</text>
<path d="M 10 200 h 290" stroke="blue" stroke-width=".5" />
<text x="40" y="200" font-family="sans-serif" font-size="15" style="dominant-baseline: central;">
XXX dominant-baseline: central XXX
</text>
</svg>
In order to align text vertically in SVG one has to use the dominant-baseline
attribute.
This has already been discussed on SO (Aligning text in SVG) and is part of the specification.
My problem is with IE9 which apparently does not support dominant-baseline
and a bunch of other things.
Do you have any ideas on how to approximate dominant-baseline: central
in IE9?
Here is a sample that works in FF and Chrome. It does not work in IE9, Opera 11. Safari on Windows doesn't support central
, but supports middle
which is still good.
<?xml version="1.0"?>
<svg width="300" height="300" xmlns="http://www.w3.org/2000/svg">
<path d="M 10 100 h 290" stroke="blue" stroke-width=".5" />
<text x="40" y="100" font-size="16" style="dominant-baseline: auto;">
XXX dominant-baseline: auto; XXX
</text>
<path d="M 10 200 h 290" stroke="blue" stroke-width=".5" />
<text x="40" y="200" font-family="sans-serif" font-size="15" style="dominant-baseline: central;">
XXX dominant-baseline: central XXX
</text>
</svg>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 IE 中实现此目的的一种方法是设置与字体大小相关的位置:
设置“dy”属性将向上移动文本(如果值为负)或向下移动(如果值为正)。在 IE 中设置“text-anchor”属性可以使文本在 x 轴上居中。虽然这可能有些骇人听闻,但 IE 对 SVG 的支持也是如此!
One way to accomplish this in IE is to set the position related to the size of the font:
Setting the "dy" attribute will shift the text up (if value is negative) or down (if value is positive). Setting the "text-anchor" attribute centers the text on the x axis just fine in IE. Although this might hackish, but so is IE's support of SVG!
这是一个巨大的黑客攻击,但我们可以通过考虑字体大小来近似垂直中间位置。
该规范定义了
central
,如下所示:我们可以采用已知字体大小的
EM 框
并测量其边界框来计算中心。显然,代码可以更加简洁,但这只是一个概念验证。
This is a giant hack, but we can approximate the vertical middle position by taking the font size into account.
The specification defines
central
like that:We can take an
EM box
of known font size and measure its bounding box to compute the center.Obviously, the code can be much cleaner, but this is just a proof-of-concept.
您可以尝试基线移位,看看它在 IE9 中是否有效:
Firefox 似乎不支持基线移位,但 Webkit 和 Opera 支持。
You could try baseline-shift to see if that works in IE9:
Firefox doesn't seem to support baseline-shift though, but Webkit and Opera do.