const foo = document.getElementById("foo");
const observer = new ResizeObserver((entries) => {
for (let entry of entries) {
const cr = entry.contentRect;
console.log = `Size: ${cr.width}px X ${cr.height}px`;
}
});
observer.observe(foo);
Source
https://github.com/roeib/JavaScript-snippets#notify-when-element-size-is-changed