詳解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;
}
web前端開發學習Q-q-u-n: 731771211,分享學習的方法和須要注意的小細節,不停更新最新的教程和學習方法(詳細的前端項目實戰教學視頻,PDF)

方法二:經過僞類元素疊加

<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;
}
相關文章
相關標籤/搜索