I'm trying to place a div over a canvas in HTML5 (as a HUD of sorts). I'm using z-index
, it's not working. How can I achieve this using any CSS?
.HUD {
z-index:100;
}
canvas {
z-index:1;
}
We wrap the canvas and div in a container element, which is position: relative . Then the canvas and div are set to position: absolute . This is a great answer... but... When I have a large <div> the canvas element bleeds right thru the div, no matter what the z-index, the background color or background-image?
You can use the CSS position property in combination with the z-index property to overlay an individual div over another div element. The z-index property determines the stacking order for positioned elements (i.e. elements whose position value is one of absolute , fixed , or relative ).
You cannot place elements inside a canvas (and have both displayed); they are only displayed if the browser does not understand the canvas element.
Use z-index and top . This will layer the div on bottom, the image and then the span (overlay) on top. To set the positioning from the top edge, use top , which can be used with negative numbers if you need it to be higher on the Y axis than it's parent.
Try this:
#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>
We wrap the canvas
and div
in a container element, which is position: relative
. Then the canvas
and div
are set to position: absolute
.
try something like this
<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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With