Skip to content

Instantly share code, notes, and snippets.

@greggman
Created November 20, 2022 00:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greggman/dee48b7642bf7b1e0a5e2fc9b80e6fe0 to your computer and use it in GitHub Desktop.
Save greggman/dee48b7642bf7b1e0a5e2fc9b80e6fe0 to your computer and use it in GitHub Desktop.
Display size is not a multiple of CSS size
#parent {
display: flex;
width: 99px;
justify-content: space-between;
}
#parent>* {
flex: 1 1 auto;
height: 20px;
min-width: 0;
}
#left {
background-color: red;
}
#right {
background-color: blue;
}
pre {
margin: 0;
}
<div id="parent">
<div id="left"></div>
<div id="right"></div>
</div>
async function main() {
log('devicePixelRatio:', devicePixelRatio);
const parent = document.querySelector('#parent');
parent.style.width = `${99}px`;
const left = document.querySelector('#left');
const right = document.querySelector('#right');
const observer = new ResizeObserver(logSizes);
observer.observe(left);
observer.observe(right);
};
/**
* @param {ResizeObserverEntry[]} entries
*/
function logSizes(entries) {
if (!entries[0].devicePixelContentBoxSize) {
log('WARNING!!! browser does not support getting the actual display size');
}
for (const entry of entries) {
const size = entry.devicePixelContentBoxSize || entry.contentBoxSize;
log('id:', entry.target.id.padEnd(5), 'displayWidth:', size[0].inlineSize, 'cssWidth:', entry.target.getBoundingClientRect().width);
}
}
function log(...args) {
const elem = document.createElement('pre');
elem.textContent = args.join(' ');
document.body.appendChild(elem);
}
main();
{"name":"Display size is not a multiple of CSS size","settings":{},"filenames":["index.html","index.css","index.js"]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment