To accomplish this task, create a separate container for both the canvas
and the button
. Here is an example of how you can achieve this:
.canvas-container{
position: relative;
width: 500px;
height: 500px;
}
#canvas {
position: absolute;
background-color: lightgrey;
}
#button {
position: absolute;
left: 30%;
top: 40%;
}
<div class="canvas-container">
<canvas id='canvas' width='500px' height='500px'></canvas>
<button id='button'>Click me</button>
</div>
Make sure to set the width
and height
of the canvas-container
to match the dimensions of the canvas itself, otherwise the button's position will be affected.