I'm facing a strange problem.
in short :
When we put a canvas in a div and set the size of the canvas, the div is automatically 5px bigger than the canvas whereas I expect it to get the exact same size.
this question is a following of this answer so I'll take the same example, the issue has been reproduced in firefox and in google chrome. (didn't try other browsers)
<div> <canvas height="300px" width="200px"></canvas> </div>
CSS :
div { border: 2px solid blue; /* demo purposes */ display: inline-block; } canvas { background-color: khaki; /* demo purposes */ }
result (see the white space in the div) :
You can also see this exact same example (very simple) in this JSfiddle
Why does this happen and how can we prevent it ?
Html5 Canvas is an artbook with a pencil. A Div is the desk you put the Canvas on to.
You cannot place elements inside a canvas (and have both displayed); they are only displayed if the browser does not understand the canvas element.
You can prevent it from happening by adding display: block
to the css for the canvas element.
i.e:
canvas { background-color: khaki; /* demo purposes */ display: block; }
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