I have a simple HTML canvas
that covers the entire page which displays fine by itself.
const canvas = document.getElementById("mainCanvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
window.addEventListener("resize",
function() {
canvas.width = innerWidth;
canvas.height = innerHeight;
}
);
body {
background-color: red;
margin: 0;
padding: 0;
}
#mainCanvas {
display: block;
background-color: blue;
}
<canvas id = "mainCanvas"></canvas>
However, when I try to add a div
element below it, immediately a horizontal scroll bar appears even though all of the elements are less than the total width (and therefore there shouldn't be any horizontal overflow). For example:
const canvas = document.getElementById("mainCanvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
window.addEventListener("resize",
function() {
canvas.width = innerWidth;
canvas.height = innerHeight;
}
);
body {
background-color: red;
margin: 0;
padding: 0;
}
#mainCanvas {
display: block;
background-color: blue;
}
#info {
width: 50px;
height: 50px;
background-color: green;
}
<canvas id = "mainCanvas"></canvas>
<div id = "info"></div>
I know I can fix the problem of the horizontal scrollbar showing up by using overflow-x: hidden
but I want to understand why it appears in the first place.