HTML 中嵌入的 SVG 中存在大量空白

发布于 2024-12-14 15:53:30 字数 812 浏览 2 评论 0原文

我将 svg 嵌入到 html 中。

对于嵌入html中的svg,ie&chrome不支持xml:space=preserve。这样多个“ ”就会压缩为一个“ ”。将“”替换为 &nbsp 将保留多个“”并解决问题。

有更好的方法吗?谢谢。请参阅下面的示例 html:

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
<body>

<svg
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
version="1.1"
viewBox="0 0 1024 768" 
preserveAspectRatio="xMidYMid"
>

<text
x="0"
y="0"
id="textsvg"
font-family="Bitstream Vera Sans" 
font-size="100"
fill="black"
>

<tspan
x="0"
dy="100"
>
wel co     me vs wel&nbsp;co&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;me
</tspan>
</text>

</svg>
</body>
</html>

I have svg embeded in html.

For svg embeded in html, ie&chrome do not support xml:space=preserve. so multiple " " will condense to one " ". replace " " with   will keep multiple " " and soleve the problem.

Are there any better way to do it? Thank you. please see example html below:

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
<body>

<svg
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
version="1.1"
viewBox="0 0 1024 768" 
preserveAspectRatio="xMidYMid"
>

<text
x="0"
y="0"
id="textsvg"
font-family="Bitstream Vera Sans" 
font-size="100"
fill="black"
>

<tspan
x="0"
dy="100"
>
wel co     me vs wel co     me
</tspan>
</text>

</svg>
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自演自醉 2024-12-21 15:53:30

这适用于 Chrome 和 Firefox,但不适用于 IE9:

<!DOCTYPE HTML>
<html><head> 
  <meta charset="utf-8" /><title>Whitespace in SVG in HTML</title>
</head><body>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 100 100">
  <text font-size="10" fill="black">
    <tspan dy="10" xml:space="preserve">hi—h i—h  i—h     i</tspan>
  </text>
</svg>
</body></html>

即使是 SVG-in-XHTML 版本也不适用于 IE9:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head> 
  <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
  <title>Whitespace in SVG in HTML</title>
</head><body>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 100 100">
  <text font-size="10" fill="black">
    <tspan dy="10" xml:space="preserve">hi—h i—h  i—h     i</tspan>
  </text>
</svg>
</body></html>

This works for me in Chrome and Firefox, but not IE9:

<!DOCTYPE HTML>
<html><head> 
  <meta charset="utf-8" /><title>Whitespace in SVG in HTML</title>
</head><body>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 100 100">
  <text font-size="10" fill="black">
    <tspan dy="10" xml:space="preserve">hi—h i—h  i—h     i</tspan>
  </text>
</svg>
</body></html>

Even the SVG-in-XHTML version doesn't work in IE9:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head> 
  <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
  <title>Whitespace in SVG in HTML</title>
</head><body>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 100 100">
  <text font-size="10" fill="black">
    <tspan dy="10" xml:space="preserve">hi—h i—h  i—h     i</tspan>
  </text>
</svg>
</body></html>
前事休说 2024-12-21 15:53:30

您可以在文本元素上使用此 css 属性:

text {
空白:前;
}

You can use this css property on the text element :

text {
white-space: pre;
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文