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

ぐ巨炮叔叔 提交于 2019-11-27 06:56:52

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/

Sebastien Lorber

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?

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!