欧美一区二区三区,国内熟女精品熟女A片视频小说,日本av网,小鲜肉男男GAY做受XXX网站

用opacity設置元素背景的透明度

老白4年前839瀏覽0評論

一、設置元素背景透明度

opacity可以用來設置元素背景的透明度;它需要0~1之間的值

0表示完全透明(opacity:0);

1表示完全不透明(opacity:1);

0.5表示半透明(opacity:0.5);

代碼演示:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>opactity</title>
    <style>
    .box1{
                position:relative;
        width:200px;height:200px;
        background-color: #00f;
    }
         .box2{
              position:absolute;
              top:80px;
              left:80px;
              width:200px;
              height:200px;
              background-color:#0f0;
       }
       .box3{
             position:relative;
              width:200px;
              height:200px;
              background-color:#f00;
              z-index:1;
}
</style>
</head>
<body>
    <div></div>
       <div></div>
       <div></div>
</body>
</html>

對比一下元素在設置同名之前的表現效果:

設置透明度的效果

.box1{
                 position:relative;
             width:200px;height:200px;
            background-color: #00f;
            z-index:10;
            opacity:0.5;
     }
          .box2{
               position:absolute;
               top:80px;
               left:80px;
               width:200px;
               height:200px;
               background-color:#0f0;
               z-index:5;
               opacity:0.5;
        }
        .box3{
               position:relative;
               width:200px;
               height:200px;
              background-color:#f00;
             z-index:1;
               opacity:0.5;
 }

表現效果:

 

 

二、瀏覽器兼容性問題:

 


 opacity屬性在IE8及其以下的瀏覽器中不支持

 


 

為了實現透明效果,IE8及其以下的瀏覽器需要使用如下標簽代替:

alpha(opacity=透明度)

透明度選擇一個0~100之間的值

0表示完全透明(filter:alpha(opacity=0);)

100表示完全不透明(filter:alpha(opacity=100);)

50表示半透明(filter:alpha(opacity=50);)

這種方式支持IE6

filter:alpha(opacity=50);

.box1{
                position:relative;
        width:200px;height:200px;
        background-color: #00f;
                z-index:10;
                opacity:0.5;
                filter:alpha(opacity=10);
    }
.box2{position:absolute;
      top:80px;
      left:80px;
      width:200px;
      height:200px;
      background-color:#0f0;
      z-index:5;
      opacity:0.5;
      filter:alpha(opacity=50);}
.box3{
      position:relative;
      width:200px;
      height:200px;
      background-color:#f00;
      z-index:1;
opacity:0.5;
filter:alpha(opacity=80)}

表現效果:在IE8及其以下的瀏覽器也可以很好地適應

 

 因為filter:alpha(opacity=透明度)  這條元素寫在下面,所以  filter:alpha(opacity=透明度)  的優(yōu)先級要高于 opacity:0.5;  的優(yōu)先級。最終表現效果不是opacity:0.5