CSS opacity and child elements

丶灬走出姿态 提交于 2019-11-26 08:19:12

问题


<style type=\"text/css\">
div#foo {
    background: #0000ff;
    width: 200px;
    height: 200px;

    opacity: 0.30;
    filter: alpha(opacity = 30);
}
div#foo>div {
    color: black;
    opacity:1;
    filter: alpha(opacity = 100);
}
</style>

<div id=\"foo\">
    <div>Lorem</div>
    <div>ipsum</div>
    <div>dolor</div>
</div>

In the above example, the opacity of div#foo is inherited by child elements, causing the text to become nearly unreadable. I suppose it\'s wrong to say it is inherited, the opacity is applied to the parent div and the children are part of that, so attempting to override it for the child elements doesn\'t work because technically they are opaque.

I typically just use an alpha png background image in such cases, but today i\'m wondering if there\'s a better way to make a background of a div semi-transparent without affecting the contents.


回答1:


You may use rgba().

div#foo
{
    background: rgba(0, 0, 255, 0.3);
}

To make it work in old Internet Explorers use CSS PIE. There are some limitations, but those are handled in a backwards compatible way: the RGB value will be rendered correctly and the opacity will be ignored.




回答2:


The best way is setting transparent png to background..




回答3:


If you use opacity, you'd have to put them in separate DIV's and then line them up together. The background DIV would have the lower opacity, and foreground DIV would have your content with 100% opacity.



来源:https://stackoverflow.com/questions/2561460/css-opacity-and-child-elements

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