I have a <div>
whose transparency of its background-color (and not its contents) I'd like to change. A remote API sends me this colour:
#abcdef
and I tell jQuery (1.9) to apply this colour via .css
:
$('div').css('background-color', '#abcdef');
The resultant div has a background-color
style of not #abcdef
, but rather its RGB representation of the same colour.
background-color: rgb(171, 205, 239);
(Just an observation; not part of the problem)
The project requirement has been changed such that I will now need to change the transparency of the background as well, to a set percentage (50%). My desired background-color
attribute is thus
background-color: rgba(171, 205, 239, 0.5);
, however, I cannot find a way to set this background-color attribute using just jQuery, a hex colour code, and still apply an alpha value. opacity
affects contents of the div as well as the background, so it is not an option.
$('div').css('background-color', '#abcdef')
.css('opacity', 0.5); // undesirable opacity changes to div's content
Given the string #abcdef
, is it possible only by calculation (e.g. hex2dec) that I will be able to apply a background opacity to a div if I am given only a hex colour string?