This was a surprise. The following code does not seem to give me the actual colors on the screen:
h1 = document.querySelector("h1");
window.getComputedStyle(h1).color
Gives rgb(0, 0, 0)
which I think is correct. However
window.getComputedStyle(h1).backgroundColor
gives rgba(0, 0, 0, 0)
. The actual background color I see on the screen is white.
The element I call h1 is visible on the screen. I was expecting to get the actual background color. The value I get above (in rgba) is not wrong, but not very useful either. It just tells me the background is fully transparent - and that is not a color.
How do I get the actual background color in RGB?
background-color
is a bit different from other values since it depends on so many other things thanbackground-color
of parent elements. Testing on another element, a<span>
, I get thisrgba(0,0,0,0)
again even though the actual background is yellow. There is noposition
,float
etc involved. The element is positioned within the box of the containing element that has the CSS yellow background. I can see the reason for the decision of returningrgba(0,0,0,0)
however. – Resurrectionism