Set opacity of background image without affecting child elements

前端 未结 15 2564
闹比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:38

    #footer ul li {
      position: relative;
      opacity: 0.99;
    }
    
    #footer ul li::before {
      content: "";
      position: absolute;
      width: 100%;
      height: 100%;
      z-index: -1;
      background: url(/images/arrow.png) no-repeat 0 50%;
      opacity: 0.5;
    }
    

    Hack with opacity .99 (less than 1) creates z-index context so you can not worry about global z-index values. (Try to remove it and see what happens in the next demo where parent wrapper has positive z-index.)
    If your element already has z-index, then you don't need this hack.

    Demo of this technique.

提交回复
热议问题