1

I am trying to set the width/height of a canvas element to fill the container it is in. Currently it works, but when it fills the body, scrollbars are added.

let masterCanvas = document.querySelector('canvas');
let parent = masterCanvas.parentElement;
masterCanvas.width = parent.offsetWidth;
masterCanvas.height = parent.offsetHeight;

I have the following styles:

<style>
    body, html { padding: 0; margin: 0; width: 100vw; height: 100vh; }
    * { box-sizing: border-box; }
</style>

And the following body:

<body>
    <canvas></canvas>
</body>

Here is a fiddle that demonstrates this: https://jsfiddle.net/ynkcfsud/

Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338

1 Answers1

1

The canvas element behaves like an image, so it is an inline element. You'll need to apply display: block to the canvas.

Domino
  • 6,314
  • 1
  • 32
  • 58