tags:

views:

72

answers:

4

In this code (that uses jQuery), the following line in the html

#canvas { height: 500px; background: white; }

sets the height on the canvas to draw on. But if you change the height to a percentage value like this:

#canvas { height: 20%; background: white; }

the canvas doesn't display at all. Why is that? Thanks for reading.

+2  A: 

You can use percent (%) if you put a parent with specific value.

See my revision.

There, I put a div parent with 300px of height. The canvas have 100% of this.

Topera
+5  A: 

I imagine it's because your canvas' containing div has no height value.

treefrog
A: 

If the percentage doesn't work, then it simply means that the parent element doesn't have a height. In this case, you'd like to give both html and body a height.

html, body { height: 100%; }

Setting it on body only won't work since its height in turn depends on the html one.

See also this revision of your demo.

BalusC
+1  A: 

The parent container needs to have a height - else the browser doesn't know what to calculate the height from. It's thinking...20% of what? Makes sense to us but not to it.

Ross