Mai*_*tor 3 html css svg cross-browser
请注意以下代码段:
<svg style="" width="60" height="60">
<rect
x = "0"
y = "0"
rx = "1"
ry = "1"
width = "17"
height = "15"
fill = "rgb(254,199,186)"
stroke = "rgb(152,119,111)"
stroke-width = "1">
</rect>
<text
x = "8"
y = "8"
fill = "rgb(50,39,37)"
font-size = "16"
font-family = "monospace"
alignment-baseline = "middle"
text-anchor = "middle">
a
</text>
</svg>Run Code Online (Sandbox Code Playgroud)
Chrome会将该代码段呈现为:

而在FireFox上,这是结果:

如何以跨浏览器兼容的方式复制此代码段?
Firefox不支持" alignment-baseline ".尝试使用属性" 显性基线 ",它应该适用于两者(Chrome和Firefox但不适用于IE,见下文).
看看这个老答案
根据SVG规范,alignment-baseline仅适用于"tspan","textPath","tref"和"altGlyph".我的理解是它用于抵消它们上面的"文本"对象.我认为你所寻找的是显性基线.
它适用于Chrome和Firefox,但不适用于IE.如果你还想在IE中使用垂直居中的文本,你必须使用这样的工作方式:
<svg style="" width="60" height="60">
<rect
x = "0"
y = "0"
rx = "1"
ry = "1"
width = "17"
height = "15"
fill = "rgb(254,199,186)"
stroke = "rgb(152,119,111)"
stroke-width = "1">
</rect>
<text
id = "default-text"
x = "8"
y = "8"
fill = "rgb(50,39,37)"
font-size = "16"
font-family = "monospace"
alignment-baseline = "middle"
dominant-baseline = "middle"
text-anchor = "middle">
a
</text><script>
window.onload = function() {
var text = document.getElementById("default-text"),
bbox = text.getBBox(),
actualHeight = (4 - bbox.y),
fontSize = parseInt(window.getComputedStyle(text)["fontSize"]),
offsetY = (actualHeight / 2) - (bbox.height - fontSize);
text.setAttribute("transform", "translate(0, " + offsetY + ")");
}
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1061 次 |
| 最近记录: |