从父级删除不透明度的继承?

时间:2014-01-12 21:00:38

标签: css html inheritance overlay opacity

我有一个div标签。我想删除孩子对#overlay不透明度

的继承

这是我的代码:

<body id="bg">
    <div id="overlay">
        <header id="colortext">dgjdhgjdhd</header>
    </div>
</body>

这是我的css代码。

#bg{
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-size: cover;
    background-image: url(../Images/bg.jpg);
}
#overlay{
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 10000;
    background-color: #000;
    -moz-opacity:0.5;
    -khtml-opacity: 0.5;
    opacity: 0.5;
}
#colortext{
    background-image:none;
    margin-top: 7px;
    margin-left: 16px;
    top: 2%;
    left: 2%;
    color: #FFFFFF;
    text-align: left;
    font-size: xx-large;
    opacity:1 !important;
}

我希望有这样的网站背景:http://www.ehsanakbari.ir/

我该怎么做?

1 个答案:

答案 0 :(得分:2)

您无法阻止子元素继承其父级的不透明度,因为这是在渲染后完成的

相反,请使用rgba值:

#overlay{
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 10000;
    background-color: rgba(0,0,0,0.5);
           // red, green, blue, opacity
}

有关详细信息,请参阅this SO question