首頁 > web前端 > css教學 > 主體

css3為背景圖層加顏色遮罩的方法

青灯夜游
發布: 2021-01-20 15:49:04
轉載
3606 人瀏覽過

css3為背景圖層加顏色遮罩的方法

(學習影片分享:css影片教學

在開發中,有時會遇到需要在背景層加色遮罩的項目,現在特定總結一下給背景圖層加顏色遮罩的方法。

css3為背景圖層加顏色遮罩的方法

方法一:透過定位疊加(注意層級)

<div class="wrap1">
     <div class="inner"> </div>
</div>
.wrap1 {
    position: relative;
    width: 1200px;
    height: 400px;
    background: rgba(0, 0, 0, .5);
}

.wrap1 .inner {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    background: url(ban8.jpg) no-repeat center center;
    background-size: cover;
    z-index: -1;
}
登入後複製

方法二:透過偽類別元素疊加

<div class="wrap2"></div>
.wrap2 {
    position: relative;
    width: 1200px;
    height: 400px;
    background: url(ban8.jpg) no-repeat center center;
    background-size: cover;
}

.wrap2::before {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    top: 0;
    background-color: rgba(0, 0, 0, .5);
    z-index: 2;
}
登入後複製

方法三:CSS3顏色疊加background-blend-mode:multiply;(正片疊底)

<div class="wrap3"></div>
.wrap3 {
    position: relative;
    width: 1200px;
    height: 400px;
    background: url(ban8.jpg) rgba(0, 0, 0, .5) no-repeat center center;
    background-blend-mode: multiply;
}
登入後複製

#拓展:背景模糊加上顏色疊加

css3為背景圖層加顏色遮罩的方法

.wrap4 {
    position: relative;
    width: 1200px;
    height: 400px;
    background: url(ban8.jpg) rgba(0, 0, 0, .5) no-repeat center center;
    background-blend-mode: multiply;
    filter: blur(2px);
    overflow: hidden;
}
登入後複製

更多程式相關知識,請造訪:程式設計入門! !

以上是css3為背景圖層加顏色遮罩的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:jb51.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!