Search code examples
cssbackground-image

Obscure a div with an image in CSS


Is there a way to fake a foreground-image analagous to a background-image in CSS? Instead of existing behind other elements in a div, it would obscure everything inside the element.


Solution

  • Use a pseudo-element (::before won't work in IE≤7):

    .obscured{     
        position: relative;
    }
    .obscured::before{
        position: absolute; /* maximize the pseudo-element */
        top: 0;
        right: 0;
        left: 0;
        bottom: 0;
        background-color:rgba(0,0,0,0.3); /* use background-image here */
        content: " "; /* it needs actual content, an empty space is enough */
    }
    

    See also: