51

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?

Phrogz
  • 296,393
  • 112
  • 651
  • 745
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164

1 Answers1

100

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 }
Phrogz
  • 296,393
  • 112
  • 651
  • 745
  • 12
    @BharatPatil No one cares about IE9. Market share below 3%. This is 3% of IE versions only, so if you count among all browsers it is even less. – wst Jun 01 '15 at 13:38
  • 2
    One year later, according to https://www.netmarketshare.com/browser-market-share.aspx?qprid=2&qpcustomd=0 IE9 has 6.67% of *total* desktop market share. – pabrams Jan 21 '16 at 16:42
  • 1
    IE8, 9 and 10 went end of life in January 2016, meaning Microsoft doesn't support them other than under specific extended life licenses and there are no security patches. At end 2018 you can probably ignore them unless you work for an enterprise you know still has them. IE11 is still alive and kicking of course. https://www.microsoft.com/en-gb/windowsforbusiness/end-of-ie-support – Rich N Nov 07 '18 at 14:27