Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resetting the opacity of a child element - Maple Browser (Samsung TV App)

I have an issue with creating a transparent element which has a child element. Using this code the child element gets the opacity value from the parent element.

I need to reset/set the child's element opacity to an arbitrary value. Browser of reference is Maple Browser (for a Samsung TV Application).

 .video-caption {         position: absolute;         top:50px;         width: 180px;         height: 55px;         background-color: #000;         -khtml-opacity:.40;          -moz-opacity:.40;          -ms-filter:"alpha(opacity=40)";         filter:alpha(opacity=40);         filter: progid:DXImageTransform.Microsoft.Alpha(opacity=0.4); /*just for testing*/         opacity:.40;      }     .video-caption > p {         color: #fff !important;         font-size: 18px;         -khtml-opacity:1;          -moz-opacity:1;          -ms-filter:"alpha(opacity=100)";         filter:alpha(opacity=100);         filter: progid:DXImageTransform.Microsoft.Alpha(opacity=1);         opacity:1;      } 

EDIT MARKUP

<div> <img id="videothb-1" src="https://xxxxx/splash.jpg"> <div class="video-caption"> <p>Description here</p> </div> </div> 
like image 241
GibboK Avatar asked Nov 22 '12 08:11

GibboK


People also ask

How do you change the opacity of an element in a child?

If you want it fully visible, make it 1. If you want it 50%, make it 0.5. So if you want your background colour on your parent element to be 50% opaque, it'll be 0.5, or background-color:rgba(255,255,255,0.5). And if you want your button to be completely opaque, it'll be 1, or background-color:rgba(255,255,255,1).

How do you change the opacity of an element's background without affecting the child elements or text content?

Answer: Use the CSS RGBA colors There is no CSS property like "background-opacity" that you can use only for changing the opacity or transparency of an element's background without affecting its child elements.


2 Answers

The problem you probably have (based on looking at your selectors) is that opacity affects all child elements of a parent:

div {     background: #000;     opacity: .4;     padding: 20px; }  p {     background: #f00;     opacity: 1; }​ 

http://jsfiddle.net/Kyle_/TK8Lq/

But there is a solution! Use rgba background values and you can have transparency wherever you want :)

div {     background: rgba(0, 0, 0, 0.4);     /*opacity: .4;*/     padding: 20px; }  p {     background: rgba(255, 0, 0, 1);     /*opacity: 1;*/ }​ 

http://jsfiddle.net/Kyle_/TK8Lq/1/


For text, you can just use the same rgba code, but set to the color property of CSS:

color: rgba(255, 255, 255, 1); 

But you must use rgba on everything for this to work, you have to remove the opacity for all parent elements.

http://jsfiddle.net/Kyle_/TK8Lq/2/

like image 164
Kyle Avatar answered Oct 21 '22 12:10

Kyle


Kyle's solution works fine.

In addition, if you don't like to set your colors using RGBA, but rather using HEX, there are solutions.

With LESS for exemple, you could use a mixin like:

.transparentBackgroundColorMixin(@alpha,@color) {   background-color: rgba(red(@color), green(@color), blue(@color), @alpha); } 

And use it like

.myClass {     .transparentBackgroundColorMixin(0.6,#FFFFFF); } 

Actually this is what a built-in LESS function also provide:

.myClass {     background-color: fade(#FFFFFF, 50%); } 

See How to convert HEX color to rgba with Less compiler?

like image 41
Sebastien Lorber Avatar answered Oct 21 '22 13:10

Sebastien Lorber