Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css transition opacity of the background color

looking for clue, how to use the opacity in background color with transition?

I'm using rgba() function, but the transition is not working on hover.

.bx{
  background:rgba(255,0,0,0.5);
  width:100px; height:100px;
  position:relative;
  transition: opacity .5s ease-out;
  -moz-transition: opacity .5s ease-out;
  -webkit-transition: opacity .5s ease-out;
  -o-transition: opacity .5s ease-out;
}

.bx:hover{
  background:rgba(255,0,0,1);
}

.t{
  position:absolute; 
  bottom:0px;
}

HTML

<div class="bx"><div class="t">text</div></div>

Any idea, how can I use transition for .bx?

like image 561
faisaljanjua Avatar asked Aug 21 '13 09:08

faisaljanjua


People also ask

How do I set background color opacity in CSS?

To set the opacity of a background, image, text, or other element, you can use the CSS opacity property. Values for this property range from 0 to 1. If you set the property to 0, the styled element will be completely transparent (ie. invisible).

How do I change the color of the background opacity?

Changing the opacity of the background color only To achieve this, use a color value which has an alpha channel—such as rgba. As with opacity , a value of 1 for the alpha channel value makes the color fully opaque. Therefore background-color: rgba(0,0,0,. 5); will set the background color to 50% opacity.


1 Answers

In fact, opacity and rgba() are completely different.

Since you are using the rgba() as the background color by the background property, you'll need to use background as transition property as follows:

.bx {
  background: rgba(255,0,0,0.5);
  width:100px; height:100px;
  position: relative;

  -webkit-transition: background .5s ease-out;
  -moz-transition: background .5s ease-out;
  -o-transition: background .5s ease-out;
  transition: background .5s ease-out;
}

.bx:hover {
  background: rgba(255,0,0,1);
}

JSBin Demo.

like image 142
Hashem Qolami Avatar answered Sep 20 '22 23:09

Hashem Qolami