Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does jQuery .css('background-color') return rgba(0,0,0,0) for 'transparent'?

Tags:

jquery

css

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);
});
like image 723
Steve Avatar asked Dec 13 '13 19:12

Steve


People also ask

How do I make a background transparent in Rgba CSS?

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).

What is the CSS color code for transparent?

There is no hex code for transparency. For CSS, you can use either transparent or rgba(0, 0, 0, 0) .

How do you make a color transparent in Rgba?

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.

What is the correct jQuery code to set the background color of all?

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.


1 Answers

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.

like image 146
PSL Avatar answered Oct 01 '22 07:10

PSL