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

后端 未结 14 1940
南笙
南笙 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;
    }
    <div class="bg-container">
       <div class="content">
         <h1>Background Opacity 0.4</h1>
       </div>
    </div>

    0 讨论(0)
  • 2020-11-22 08:25

    Well the only CSS way of doing only background transparency is via RGBa but since you want to use an image I would suggest using Photoshop or Gimp to make the image transparent and then using it as the background.

    0 讨论(0)
  • 2020-11-22 08:27

    Solution with 1 div and NO transparent image:

    You can use the multibackground CSS3 feature and put two backgrounds: one with the image, another with a transparent panel over it (cause I think there's no way to set directly the opacity of the background image):

    background: -moz-linear-gradient(top, rgba(0, 0, 0, 0.7) 0%, rgba(0, 0, 0, 0.7) 100%), url(bg.png) repeat 0 0, url(https://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png) repeat 0 0;
    
    background: -moz-linear-gradient(top, rgba(255,255,255,0.7) 0%, rgba(255,255,255,0.7) 100%), url(https://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png) repeat 0 0;
    
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(255,255,255,0.7)), color-stop(100%,rgba(255,255,255,0.7))), url(https://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png) repeat 0 0;
    
    background: -webkit-linear-gradient(top, rgba(255,255,255,0.7) 0%,rgba(255,255,255,0.7) 100%), url(https://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png) repeat 0 0;
    
    background: -o-linear-gradient(top, rgba(255,255,255,0.7) 0%,rgba(255,255,255,0.7) 100%), url(https://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png) repeat 0 0;
    
    background: -ms-linear-gradient(top, rgba(255,255,255,0.7) 0%,rgba(255,255,255,0.7) 100%), url(https://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png) repeat 0 0;
    
    background: linear-gradient(to bottom, rgba(255,255,255,0.7) 0%,rgba(255,255,255,0.7) 100%), url(https://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png) repeat 0 0;
    

    You can't use rgba(255,255,255,0.5) because alone it is only accepted on the back, so you I've used generated gradients for each browser for this example (that's why it is so long). But the concept is the following:

    background: tranparentColor, url("myImage"); 
    

    http://jsfiddle.net/pBVsD/1/

    0 讨论(0)
  • 2020-11-22 08:28

    Simple solution

    if you need to set the gradient to background-image only:

    background-image: url(IMAGE_URL); /* fallback for older browsers */
    
    background-image: linear-gradient(to bottom, rgba(0,0,0,0.6) 0%,rgba(0,0,0,0.6) 100%), url(IMAGE_URL);
    
    0 讨论(0)
  • 2020-11-22 08:29

    Two methods:

    1. Convert to PNG and make the original image 0.2 opacity
    2. (Better method) have a <div> that is position: absolute; before #main and the same height as #main, then apply the background-image and opacity: 0.2; filter: alpha(opacity=20);.
    0 讨论(0)
  • 2020-11-22 08:30

    I have used the answer of @Dan Eastwell and it works very well. The code is similar to his code but with some additions.

    .granddata {
        position: relative;
        margin-left :0.5%;
        margin-right :0.5%;
    }
    .granddata:after {
        content : "";
        display: block;
        position: absolute;
        top: 0;
        left: 0;
        background-image: url("/Images/blabla.jpg");
        width: 100%;
        height: 100%;
        opacity: 0.2;
        z-index: -1;
        background-repeat: no-repeat;
        background-position: center;
        background-attachment:fixed;
    }
    
    0 讨论(0)
提交回复
热议问题