82

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>
GibboK
  • 71,848
  • 143
  • 435
  • 658

3 Answers3

181

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/

Kyle
  • 65,599
  • 28
  • 144
  • 152
  • Thanks for your help, I need to set the color for the TEXT in p not the background... adapting your code to background: rgba(255, 255, 255, 1); I still have the same issue. Thanks for your time. – GibboK Nov 22 '12 at 09:08
  • Ah, I'll make a new fiddle for you. :) – Kyle Nov 22 '12 at 09:10
  • 1
    Updated the answer to use it for color, and that you must remove opacity from parent elements. – Kyle Nov 22 '12 at 09:12
  • it works great on desktop browser but not on Mepla, I'm testing on a Samsung SmartTv 2012 – GibboK Nov 22 '12 at 09:13
  • How old is the TV? Samsung used Maple from 2010 - 2011 and now it's a Webkit engine. – Kyle Nov 22 '12 at 09:17
  • I use both 2012 and 2011, I have the same issue on both platform. – GibboK Nov 22 '12 at 09:24
  • That's strange, both the Gecko and Webkit engines support this. Are you sure you've removed all the opacity ruleS? – Kyle Nov 22 '12 at 09:25
  • Yes I made it, it is very strange. – GibboK Nov 22 '12 at 09:26
  • Unless Samsung modified the engines to not support it (they may have, I hear the engines are highly modified) this should absolutely be working. It may be a caching issue. – Kyle Nov 22 '12 at 09:27
  • I'm using .video-caption > p {} as selector for the p element, could be this the issue? – GibboK Nov 22 '12 at 09:30
  • That's a direct descendant selector so if what you want is deeper down a DOM branch it won't get selected. Try removing the > and see what happens. – Kyle Nov 22 '12 at 09:31
  • Thanks for your hit, I was able to solve the problem, I made a reset of my machine :-) – GibboK Nov 22 '12 at 09:39
  • 2
    This is the best thing in the universe - working in ff 33 for linux mint. – user1063287 Nov 27 '14 at 23:44
7

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?

Community
  • 1
  • 1
Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419
1

Answer above works well, however for people who like using hex color codes, you can set transparency by hex color itself. EXP: #472d20b9 - the last two values set opacity for color while #472d20 will be the same color but without opacity. Note: Works fine in Chrome and Firefox, while Edge and IE doesn't. Haven't had a chance to test it in other browsers.

Max
  • 962
  • 5
  • 9