I have a parent element and a child element. On window resize event, the child element has its width and height set to the parents' offsetWidth and offsetHeight, however on every other resize both the width and height are off by 12px on Firefox and 15px on Edge.
Here's the code, when you resize the page, there's a white gap on the right:
const parent = document.getElementById("parent");
const child = document.getElementById("child");
const matchDimensions = () => {
child.style.width = parent.offsetWidth + "px";
child.style.height = parent.offsetHeight + "px";
};
matchDimensions();
window.addEventListener("resize", matchDimensions);
html, body, #parent {
width: 100%;
height: 100%;
margin: 0;
}
#child {
background: red;
}
<div id="parent">
<div id="child"></div>
</div>
Weirdly, it only happens if I scale the page down but not if I scale it up. Also, removing margin: 0 stops this from happening but I need it in order to have the parent fill the entire page.
The reason I'm using JS to match the child to the parent is because in reality the child is a canvas which doesn't automatically resize with the parent with just CSS.
Possible the browser has some default styling getting in the way since it changes behavior with the size of the viewport.
You could try a css reset (one example) or normalize to see if that solves the issue.