Search code examples
csshtmlinheritanceoverlayopacity

Remove inheritance of opacity from parent?


I have a div tag. I want to remove the children's inheritance of #overlay's opacity

Here is my code:

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

And this my css code.

#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;
}

I want to have like this site background: http://www.ehsanakbari.ir/

How can I do this?


Solution

  • You cannot stop children elements from inheriting their parent's opacity.

    Instead, you could use an rgba value for the parent's background color instead of opacity:

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

    See this SO question for more info.