问题在Firefox和Chrome中都存在,我有一个带有纯色背景的canvas和一个带有纯色/图像背景的div。该div的margin覆盖在canvas上方,但是div不会显示在canvas上面。有趣的是,如果div中有文本,则文本将正确显示。这意味着这是两个浏览器中的浏览器错误。以下是一些代码,供那些想要尝试的人使用。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
#d{background-color:#111;margin-top:-150px;z-index:999999;}
</style>
<script type="text/javascript">
function load() {
var c = document.getElementById("c").getContext("2d");
c.fillStyle = "rgba(255, 200, 200, 1)";
c.fillRect(0, 0, c.canvas.width, c.canvas.height);
}
</script>
</head>
<body onload="load()">
<canvas id="c" width="500" height="300"></canvas>
<div id="d" style="width:500px;height:300px"></div>
</body>
</html>
那么,有人有任何解决方法吗?或者我是错过了HTML5规范中说这是正确的东西吗?
顺便提一下,请不要问我为什么要使用边距而不是固定/绝对等替代方案。我需要边距。
top
、right
等属性,你的 div 的位置就不会改变。 - webdesserts