Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Div containing canvas have got a strange bottom margin of 5px

Tags:

html

canvas

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) :

result

You can also see this exact same example (very simple) in this JSfiddle

Why does this happen and how can we prevent it ?

like image 997
Guian Avatar asked Apr 04 '13 09:04

Guian


People also ask

What is the difference between canvas and Div?

Html5 Canvas is an artbook with a pencil. A Div is the desk you put the Canvas on to.

Can you put a div in 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.


1 Answers

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; } 
like image 86
Matt Cain Avatar answered Sep 18 '22 14:09

Matt Cain