Bernice
Bernice

Reputation: 2592

Overlay a canvas over a div

I have a div and I want to overlay a canvas exactly on top of it with the same width, height, padding and margins. Although I am using position: absolute like most questions here recommend and z-index the canvas is still showing underneath the div. This is the code I have until now.

<div id ="editor-section">  
     <div class="editable" id="editor"></div>
</div> 

// the canvas is created / removed dynamically on connection / disconection 
hub.client.broadcastSomeoneConnected = function (connectionId, someoneConnected) 
{
    if (someoneConnected) {
        var canvas = document.createElement("canvas");
        canvas.id = connectionId;
        canvas.className = 'canvases';
        canvas.style.border = '2px solid red';
        canvas.style.zIndex = zindex;  
        zindex++;

        var parentDiv = document.getElementById("editor-section");
        parentDiv.appendChild(canvas);

    } else { // someone disconnected

        var canvas = document.getElementById(connectionId);
        canvas.parentNode.removeChild(canvas);
    }
 }

 // css for all canvases
 .canvases {
     width:60%; 
     height:700px;
     border:1px solid;
     position:absolute;
     padding: 5%;
     margin-left:20px;
     pointer-events: none;
 }

 // css for editor div
 #editor {
     padding: 5%;
     margin-left:20px;
     border: 2px solid black;
     overflow-y:scroll;
     height: "700px";
     width: "100%";
     background-color: white;
     margin-bottom:30px;
 }

PS: zindex is initialized globally because I need multiple canvases layered on top of each other and the editor eventually depending on the number of people connected

What am I doing wrong here? Thanks

Upvotes: 8

Views: 16957

Answers (1)

nice ass
nice ass

Reputation: 16729

Almost there. Move width and height on the container, make it relative, and the editor and canvas absolutely positioned at 0,0 with 100% width/height. The zIndex of your editor must be lower than the zIndex of your canvas. fiddle

Upvotes: 12

Related Questions