CSS控制元素背景透明度總結

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   os   

方法一:CSS3的background rgba
filter:progid:DXImageTransform.Microsoft.gradient(enabled=‘true‘,startColorstr=‘#7F000000‘,endColorstr=‘#7F000000‘);background:rgba(0,0,0,0.5);

常用的方法是rgba來實現背景透明度 相容ie6 ie7 ie8的做法是用ie的濾鏡實現

有個小問題就是通過這種方法實現背景透明度的話在ie系列下如果元素需要綁定事件,滑鼠移動到透明地區總是不能準確擷取元素。

方法二:opacity

在實際案例中opacity我們通常用在改變元素的透明度,而不是背景透明度。

opacity:0.5;filter: alpha(opacity=50);*zoom:1;

IE4-IE7使用filter: alpha(opacity=50),但要同時使該元素擁有hasLayout;

這種方法實現的話裡麵包含的元素也會被改變透明度。

有幸看了一下163的代碼發現有個方法可以讓子項目100%透明度的辦法,就是設定position: relative;

<!DOCTYPE><html><head>    <title>opacity</title>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <style>        body{background-color: red;}        .box{opacity:0.5;filter: alpha(opacity=50);*zoom:1;background-color: #000;width: 500px;height: 500px;text-align: center;line-height: 500px;font-size: 2em; color: #fff;}        .box p{position: relative;}    </style></head><body><div class="box">    <p>文字123文字123文字</p></div></body></html>

 

最終解決方案

如果產品需求只是簡單的改變背景透明度,方法一就足夠了。

但是如果還需要給有背景透明的的元素再綁定事件的話就要做一下調整了。

支援CSS3的瀏覽器用rgba,ie就用filter: alpha(opacity=50),子項目設定position: relative;

.box{background-color:#000;filter:alpha(opacity=50);background:rgba(0,0,0,0.5);}.box p{position:relative;}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.