Set opacity of background image without affecting child elements

前端 未结 15 2214
闹比i
闹比i 2020-11-22 01:39

Is it possible to set the opacity of a background image without affecting the opacity of child elements?

Example

All links in the footer need a custom bull

15条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 02:22

    Another option is CSS Tricks approach of inserting a pseudo element the exact size of the original element right behind it to fake the opaque background effect that we're looking for. Sometimes you will need to set a height for the pseudo element.

    div {
      width: 200px;
      height: 200px;
      display: block;
      position: relative;
    }
    
    div::after {
      content: "";
      background: url(image.jpg);
      opacity: 0.5;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      position: absolute;
      z-index: -1;   
    }
    

提交回复
热议问题