Disable opacity on child element when parent element has opacity

后端 未结 6 1988
灰色年华
灰色年华 2020-12-05 06:30

I have a container with an opacity of 0.8. At the background, I have an image that shines through the content div. Now, I have a photo of my client in this

6条回答
  •  一个人的身影
    2020-12-05 06:54

    As the other answers state, this is not possible using opacity, that is, with this method.

    A workaround/hack would be to add position: relative; z-index:2; to the parent element contentContainer. Then to add another element which has the opacity and other stuff on it. This is particularly useful if you have an image as the background

    So your markup should look a little like this:

    HTML

    Content ... Photo

    CSS

    #contentContainer { position: relative; z-index 2; }
    #contentBackground {
        position: absolute;
        display: block;
        z-index: 1;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        background: /* stuff */;
    }
    

    Note the z-index property. These are important for making sure that the background element sits below the parent and doesn't overlap the content preventing click events.

    This method could also be used with pseudo elements (:before or :after) for which you'd need to add content: '';.

提交回复
热议问题