如何在画布CSS上叠加div

时间:2014-11-07 02:52:48

标签: html css html5 html5-canvas

我试图在HTML5的画布上放置div(作为各种HUD)。我使用z-index,它无效。如何使用任何CSS实现此目的?

    .HUD {
        z-index:100;
    }

    canvas {
        z-index:1;
    }

2 个答案:

答案 0 :(得分:21)

试试这个:

#container {
  position: relative;
}
#container canvas, #overlay {
  position: absolute;
}
canvas {
  border: 1px solid black;
}
<div id="container">
  <canvas></canvas>
  <div id="overlay">This div is over the canvas</div>
</div>

我们将canvasdiv包装在容器元素position: relative中。然后,canvasdiv设置为position: absolute

答案 1 :(得分:1)

尝试这样的事情

<div class="container_div" style="position:relative;">
    <div class="hover_div" style="position:absolute; width:25px !important; display:block;z-index:9999">                            
        <img class="some_class" src="/settings_icon.png" style="height: 20px; display: block;">
    </div>
<canvas width="180" height="270" style="width: 180px; height: 270px;"></canvas>
</div>