I think since CSS 3
you can use the Background
as Background-color
attribute. It says the displayed color is based on the Desktop-Color, so it can variate from client to client.
alert(document.body.style.backgroundColor);
returns just background
, instead of a color-value.
So how to get the RGB-/Hex-Value of the used color in the specific element?
Asked
Active
Viewed 71 times
-1

HelloToYou
- 335
- 5
- 14
-
1What do you get if you use `getComputedStyle()`? – Barmar Jan 05 '17 at 20:49
-
document.body.style.backgroundColor works for me just fine – Serg Chernata Jan 05 '17 at 20:52
-
`backgroundColor` returns the background color, not the full background. And `background-color` was already defined in CSS 2.1 – Oriol Jan 05 '17 at 20:55
-
thanks to all, `getComputedStyle` worked for me :) – HelloToYou Jan 05 '17 at 20:58
-
I would like to offer some advice: If you can find a way to avoid it, don't set set styles using JS - instead, add/remove classes to be selected and styled by CSS. Helps with [SoC](https://en.wikipedia.org/wiki/Separation_of_concerns#HTML.2C_CSS.2C_JavaScript) for easier code maintenance – BCDeWitt Jan 05 '17 at 21:23
1 Answers
1
// $0 is your htmlElement
window.getComputedStyle($0, null).getPropertyValue('background-color')

Yerko Palma
- 12,041
- 5
- 33
- 57
-
1
-
@Oriol good to know, anyway, I'll leave it there for compatibility – Yerko Palma Jan 05 '17 at 21:00