I have a box here - http://jsfiddle.net/U68p3/2/ - with a transparent background. When I read the background with jQuery's .css('background-color') it returns
rgba(0, 0, 0, 0)
which is not very helpful if my code is looking for a match with 'transparent'.
Why is jQuery doing this and is there a way I can make it return 'transparent'?
Thanks.
$(function() {
var bkgnd = $('#box').css('background-color');
console.log('background-color is ' + bkgnd);
});
Transparency using RGBA In addition to RGB, you can use an RGB color value with an alpha channel (RGBA) - which specifies the opacity for a color. An RGBA color value is specified with: rgba(red, green, blue, alpha). The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque).
There is no hex code for transparency. For CSS, you can use either transparent or rgba(0, 0, 0, 0) .
So how do you use RGBA? rgba (100%, 0%, 0%, 1); The fourth value denotes alpha and needs to be between 0.0 (absolute transparency) and 1.0 (absolute opacity). For example, 0.5 would be 50% opacity and 50% transparency.
To set the background color using jQuery, use the jQuery css() property. We will set background color on mouse hover with the jQuery on() method.
It is not jquery, the computed value for the color are represented in RGBa (Red, Blue, Green, Alpha - for opacity) and not in as color names (like red, blue, orange, transparent etc) or as hex values. According to the specs transparency is represented as rgb(0, 0, 0)
.
if the value is translucent, the computed value will be the rgba() corresponding one. If it isn't, it will be the rgb() corresponding one. The transparent keyword maps to rgb(0,0,0).
So instead of looking for this specific value you can add a specific css rule just to include transparency and add that class to the element and use .hasClass
or .is
of that class to check if the element is transparent.
It seems like different browsers represent it in different ways, IE, FF gives the value as transparency
so it is anyways better not to rely on this value representation for any logic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With