Can I set background image and opacity in the same property?

后端 未结 14 2131
南笙
南笙 2020-11-22 07:58

I can see in CSS references how to set image transparency and how to set a background image. But how can I combine these two in order to set a transparent background image?<

14条回答
  •  [愿得一人]
    2020-11-22 08:22

    You can use CSS psuedo selector ::after to achieve this. Here is a working demo.

    .bg-container{
      width: 100%;
      height: 300px;
      border: 1px solid #000;
      position: relative;
    }
    .bg-container .content{
      position: absolute;
      z-index:999;
      text-align: center;
      width: 100%;
    }
    .bg-container::after{
      content: "";
      position: absolute;
      top: 0px;
      left: 0px;
      width: 100%;
      height: 100%;
      z-index:-99;
      background-image: url(https://i.stack.imgur.com/Hp53k.jpg);
      background-size: cover;
      opacity: 0.4;
    }

    Background Opacity 0.4

提交回复
热议问题