I have not been able to find information about whether it is possible to getComputedStyle
of an element that is not in the browser DOM.
My testing suggests that elements not inserted in the browser DOM do not get a computed style.
var el = document.createElement("span")
el.id = "greeting"
el.textContent = "Hi there"
el.setAttribute("style", "background-color: yellow;")
console.log("Element in JS memory:", window.getComputedStyle(el).backgroundColor)
document.querySelector("body").appendChild(el)
console.log("Element in browser dom:", window.getComputedStyle(document.querySelector("#greeting")).backgroundColor)
But it does not strike me as unreasonable that it would be possible to ask the browser to calculate styles for documents or elements that in JavaScript memory. Or there might be some package that does this. But I haven't found any.
Is this possible (without having to write complex css computation logic myself)?