純css實現背景圖片半透明內容不透明的方法-opacity屬性正確使用


效果圖如下

<div class="bottom-transparent">
  <div class="btn-area flex">
    <el-button type class="search_but">企業招聘需求快速登記</el-button>
    <div class="msg">已有帳號快速,立即登錄?</div>
    <svg-icon icon-class="cancel" class="icon icon-svg-cancel" />
  </div>
</div>

效果不佳的CSS即opacity錯誤用法,會把左側背景圖標編程透明, 中間按鈕顏色失真

.bottom-transparent {
  height: 180px;
  width: 100%;
  background: rgba(0, 0, 0, 1) url("/static/img/icon_flat-big.png");
  background-repeat: no-repeat;
  background-position: 5% 50%;
  bottom: 0;
  margin-bottom: 2px;
  position: fixed;
  z-index: 100;
  opacity: 0.6;
}

有幸查到一篇正解,並且實踐有效 參考

使用SASS, 關鍵CSS 第11~22, 第19行 

一定要設置position:absolute,這樣才能設置z-index,讓背景處於內容的下一層
 1 // 底部透明條
 2 .bottom-transparent {
 3   height: 180px;
 4   width: 100%;
 5   background: url("/static/img/icon_flat-big.png");
 6   background-repeat: no-repeat;
 7   background-position: 5% 50%;
 8   bottom: 0;
 9   margin-bottom: 2px;
10   position: fixed;
11  .btn-area::before {
12     content: "";
13     background: rgba(0, 0, 0, 1);
14     opacity: 0.6; /* 透明度設置 */
15     z-index: -1;
16     background-size: 500px 300px;
17     width: 100%;
18     height: 180px;
19     position: absolute;
20     top: 0px;
21     left: 0px;
22   }
23   .btn-area {
24     height: 180px;
25     justify-content: center;
26     align-items: center;
27     .msg {
28       opacity: 1;
29       font-size: 16px;
30       color: #fff;
31       margin-right: 20px;
32     }
33     .icon {
34       &.icon-svg-cancel {
35         font-size: 36px;
36         color: #000;
37       }
38     }
39   }

其它知識點: CSS3 中 偽元素::before

開發環境 vue 、 vue element UI

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM