透明的背景图像

RAV*_*ADA 14 css opacity

我有以下css.

   #mypass{
                background-image: url("Images/logo.png");
                background-attachment: fixed;
                background-position:140px 100px ;
                background-size: 100px;
                background-repeat: no-repeat;
                opacity:0.5;
                fileter:alpha(opacity=40);
                color: blue;
        }
Run Code Online (Sandbox Code Playgroud)

我用这个作为

<div id="mypass">
      <center><h2>PRAGATI ENGINEERING COLLEGE</h2>
    <h5>1-378,ADB Road,Surampalem-533437,E.G.Dist.,ph:08852-252233</h5></center>

      <%    out.println("________________________________________________________");
        String photo="Image/"+id+".jpeg";
       System.out.println(photo);

       String yy="";
       int y=((1900+d.getYear())-(Integer.parseInt(id.substring(0, 2))+2000))+1 ;
       switch(y){
          case 1:   yy=y+" st";break;
          case 2:   yy=y+" rd";break;
          case 3:   yy=y+" rd";break;
          case 4:   yy=y+" th";break;
          default: yy=y+" th";
       }

       int branch=Integer.parseInt(id.substring(6,8));
       System.out.println(branch);
       switch(branch){
         case 12:yy+=" IT";break;
         case 05:yy+=" CSE";break;
         case 03:yy+=" MECH";break;
         case 02:yy+=" EEE";break;
         case 04:yy+=" ECE";break;
         default:yy+="PEC";
       }
    %>
      <h2 class="buspass" style="color:#FF33CC"><a class=title onclick="javascipt:window.print(), width=400, height=400" onmouseover="this.style.cursor='hand'">BusPass</a></h2>
      <img class="printright" src="<%=photo%>" width="90" height="90" />
      <table>
         <!-- <tr><td>RollNumber</td><td>: <%=id%></td></tr> -->
          <tr ><td>Name</td><td style="color:black">: <%=name%></td></tr>
          <tr><td>Class</td><td style="color:black">: <%=yy%></td></tr>
          <tr><td>AcadamicYear</td><td style="color:black">: <%=s%></td></tr>
          <tr><td>Stage</td><td style="color:black">: <%=pickuppoint%></td></tr>
          <tr><td>BusRoute</td><td style="color:black">: <%=routecode%></td></tr>
        <!--  <tr><td>SeatNo</td><td>: <%=seatno%></td></tr>-->
        <!--  <tr><td>IssueDate</td><td>: <%=ddd%></td></tr> -->
      <!--    <tr><td>ValidTill</td><td>: <%=valid%></td></tr> -->
      </table>
      <h3 class="printrightdown">INCHARGE</h3>
    </div>
Run Code Online (Sandbox Code Playgroud)

这适用于所有使用该类的元素.相反,我想使用不透明度和滤镜只对背景图像.我怎么能这样做?

Que*_*tin 37

您无法使用CSS调整背景图像的半透明度.

您可以调整整个元素(opacity)或普通背景的半透明度rgba(),但不能调整背景图像的半透明度.

使用本机支持半透明的图像格式(如PNG),并在图像本身中嵌入所需的半透明效果.


Bir*_*sky 6

需要具有半透明背景的元素:

#myPass
Run Code Online (Sandbox Code Playgroud)

这就是如何在不触及HTML的情况下,为元素添加可控制的透明背景:

#myPass::before {
    content: "";
    position: absolute;
    top: 0; right: 0;
    bottom: 0: left: 0;
    background: url(image.jpg) no-repeat top center;
    opacity: 0.5;
}
Run Code Online (Sandbox Code Playgroud)

唯一需要注意的是#myPass元素必须定位:

#myPass {
    position: relative; /* absolute is good too */
}
Run Code Online (Sandbox Code Playgroud)


小智 5

刚刚自己遇到了这个问题。如果将 linear-gradient 与背景图片 url 结合使用,则可以控制图片的不透明度。通过保持两个“渐变”相同,您可以在不使用不透明度的情况下获得过滤器/不透明度效果。

IE

 .bg-image {
        background: linear-gradient(rgba(255,255,255,0.5), rgba(255,255,255,0.5)), url(path-to-image.jpg) center no-repeat;
        background-size: cover;
      }
Run Code Online (Sandbox Code Playgroud)