相容IE、Firefox的背景半透明內容不透明設定,firefox不透明

來源:互聯網
上載者:User

相容IE、Firefox的背景半透明內容不透明設定,firefox不透明

  首先要說明的是背景是內容的祖先元素。如果是兄弟節點那就沒有必要記錄這篇文章了。

  記錄一下,知其然也知其所以然。

IE8-特點:

  1.不支援"opcity:0.5;"這種寫法,只支援"filter:alpha(opacity=50)"。

  2.如果背景元素擁有css屬性設定了z-index的值(除預設值auto外),則內容會隨著背景元素一起半透明

  3.如果背景元素的內容元素本身或其祖先節點(這些祖先節點是背景元素的後代節點)沒有設定css定位屬性position:relative/absolute/fixed。則內容也會隨著背景元素一起半透明。

  4.支援"background:rgb(0,0,0)"設定背景顏色,但是不支援“background:rgba(0,0,0,0.5)”這種設定背景顏色的同事設定半透明的方式。

  

IE9+、chrome、Firefox特點:

  1.支援"opacity:0.5",但是內容會隨背景一起半透明

  1.支援“background:rgba(0,0,0,0.5)”這種設定背景顏色的同事設定背景半透明但內容不透明的方式。

  2.如果同事使用opacity=0.5和rgba不透明度為0.5,那麼效果是在元素為opacity所設定的不透明度的情況下背景再次按rgba的方式半透明。最終背景的半透明度為opacity*rgba=0.5*0.5=0.25。

  IE9+還有一個特點就是同時支援opcity:0.5和"filter:alpha(opacity=50)"這種方式。所以rgba和filter一起用會出現第三項的情況。

 

  所以綜合上述的瀏覽器特點,相容所有瀏覽器的方案是背景元素設定樣式為

    /*不能設定z-index,根據情況設定需要的background的顏色,chrome/firefox中第一句起作用;IE中後兩句起作用*/    .background{        background: rgba(0,0,0,0.5);/*firefox、chrome*/                background: #000\9;/*IE*/        filter: alpha(opacity=50);/*IE*/    }

  代碼解讀為在Firefox、chrome下後兩句不會被識別,所以使用rgba做背景半透明內容不透明;IE下第二句的background會覆蓋第一句話,所以最終結果是後兩句起到作用。

  

  內容元素的樣式為

/*可以設定元素或其祖先節點為定位樣式relative/absolute/fixed*/.content{    position:relative;  }

  完整執行個體

<!DOCTYPE html><html lang="ch-cn">  <head>  <meta charset="utf-8">    <style type="text/css">    html,body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, button, textarea, p, blockquote, th, td{      margin: 0;      padding:0;    }    *{      -webkit-box-sizing: border-box;      -moz-box-sizing: border-box;      box-sizing: border-box;    }    /*不能設定z-index,根據情況設定需要的background的顏色,chrome/firefox中第一句起作用;IE中後兩句起作用*/    .background{        background: rgba(0,0,0,0.5);/*firefox、chrome*/                background: #000\9;/*IE*/        filter: alpha(opacity=50);/*IE*/    }    /*可以設定元素或其祖先節點為定位樣式relative/absolute/fixed*/    .content{        position:relative;      }    </style>  </head>  <body>    <div style="width:50px;height:50px;border:1px solid;background: red;"></div>    <div class="background" style="width: 200px;height: 200px;">        <div class="content" style="width: 50px;height:50px;background: red;"></div>    </div></html>

  如下

  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.