【发布时间】:2012-01-01 10:03:37
【问题描述】:
我在 HTML 文档中使用 SVG。由于某种原因,在 Chrome 中,任何 <foreignObject> 元素中的任何内容都会出现在 <svg> 元素的父元素的左上角;好像<foreignObject> 元素是绝对定位的一样。我在 Firefox 中没有这个问题。
这可能是什么原因造成的?我该如何解决?
这是我的测试用例:(the example is also on JsFiddle)
<!DOCTYPE html>
<html>
<head>
<title>SVG bug in Chrome?</title>
<style type="text/css">
code {
background: #FFFAEE;
}
pre code {
display:block;
}
.widget-body {
background:yellow;
position: relative; /* This is the problem! */
}
</style>
<body>
<h1>SVG bug in Chrome?</h1>
<div>
<p>
The elemts in the <foreignObject> are not positioned properly unless the <code>.widget-body</code> rule is changed to:
<pre><code>.widget-body {
background:yellow;
/* position: relative; /* This is the problem! */
position: static;
}</code></pre>
</p>
<h2>The Example:</h2>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="1000" height="800">
<g>
<g transform="scale(1) translate(100, 200)" style="cursor: move;"><foreignobject pointer-events="fill" width="300" height="350">
<body xmlns="http://www.w3.org/1999/xhtml" style="margin: 0px; height: 100%;">
<table style="border-collapse: collapse; font-size: 11px; color: rgb(119, 68, 0); font-family: Arial,Helvetica; font-weight: normal; border-style: none;">
<tbody>
<tr>
<td style="text-align: center; vertical-align: middle; white-space: nowrap;">
<div style="width:300px;height:350px;position:static;">
<div class="widget" style="width: 300px;">
<div style="-moz-user-select: none;">
<span>My Widget Title</span>
</div>
<div>
<div class="widget-body" style="width: 298px; height: 323px;">
<div style="width: 298px; height: 323px;">
<div style="width: 298px; height: 323px;">
This position of this yellow square <br />should approximately (100, 200)
<div style="position:absolute;bottom:0;right:0;background:red;color:white;font-weight:bold;">
This red square <br />should be <br />in the bottom right corner <br />of the yellow square.
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</body>
</foreignobject></g>
</g>
</svg>
</div>
</body>
</html>
我希望看到的(我在 FireFox 中看到的)是这样的:
我在 Chrome(Fedora 和 Android 平板电脑上的 15.0.874.121)中得到的是:
我对 HTML 内容的控制很少,因为我同时使用 JavaScript 框架来支持丰富的应用程序和预先存在的小部件。
【问题讨论】:
-
Chrome 中的错误?你能在你的问题中添加一个简单的测试用例吗?
-
哇!我真的问过这个问题?完全忘记了! @robertc - 我会尝试创建一个测试用例。
-
@robertc - 添加了一个示例/测试用例。
-
对我来说很好。但我有
dev版的chrome。17.0.942.0 dev-m你用的是什么版本?可能是您的版本中的这个错误? -
@antyrat - 版本 15.0.874.121。对我来说听起来是个好消息;未来版本的 Chrome 可能不会有这个问题! :D
标签: html css google-chrome webkit svg