Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Placing a <div> within a <canvas>

Tags:

html

canvas

I take a div within a canvas element like following:

<canvas>     <div></div> </canvas> 

Here both of them has height and width. But here I can't see the div!

Is it not possible to take a div or p within a canvas?

like image 285
thecodeparadox Avatar asked Apr 23 '11 11:04

thecodeparadox


People also ask

Can I put a div inside a canvas?

You cannot place elements inside a canvas (and have both displayed); they are only displayed if the browser does not understand the canvas element.

Can you put HTML elements inside a canvas?

Because browsers will display either a canvas element or HTML controls that you put inside that element, but not both, you must place your controls outside of your canvas elements. To make it appear as though HTML controls are inside a canvas, you can use CSS to place the controls above the canvas.

What is div in canvas?

Div will make end of a line. (span not | span is almost same like the div) Canvas is used to draw 2D grapics in your HTML page with scripts however it can't be used as element to group other items. 7th March 2019, 9:09 AM.

Can you put a div inside a div?

To horizontally and vertically center a div within a div on a page, you can use the position, top, left, and margin properties — if you know the width and height of the divs. To start, wrap a div element in another div element in your HTML.


1 Answers

You cannot place elements inside a canvas (and have both displayed); they are only displayed if the browser does not understand the canvas element.

If you would like to position elements over the same area as a canvas, here is one technique (among many) that would let you do it:

HTML

<div id="canvas-wrap">   <canvas width="800" height="600"></canvas>   <div id="overlay"></div> </div> 

CSS

#canvas-wrap { position:relative } /* Make this a positioned parent */ #overlay     { position:absolute; top:20px; left:30px; } 

Here's another technique, which lets the content of the div flow normally and makes the canvas a background to the content:

CSS

#canvas-wrap { position:relative; width:800px; height:600px } #canvas-wrap canvas { position:absolute; top:0; left:0; z-index:0 } 
like image 140
Phrogz Avatar answered Sep 29 '22 06:09

Phrogz