簡體   English   中英

用透明背景色覆蓋背景圖像(全屏)

[英]Cover background-image with transparent background-color (full screen)

我想用透明顏色覆蓋我的背景圖像,但是顏色不覆蓋背景圖像。

這是我的演示: http : //jsfiddle.net/farna/73kx2/

CSS代碼:

.overlay{
    background: rgba(0,0,255,0.5);
    margin: 0 ;
    width: 100%;
    height: 100%;
    top: 0px;
    left: 0px;
}

添加position: fixed為您的CSS規則:

http://jsfiddle.net/73kx2/1/

現場演示

為此,您要做的就是在身體上使用Pseudo-elements-CSS 在這里我正在使用body:after

1.風格:

body{
    position:relative;
    background: url(http://8pic.ir/images/cgnd518gxezm1m2blqo7.jpg) no-repeat center center fixed;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
    width:100%;
    height:100%;
    margin:0
}
body:after{
    position:fixed;
    content:"";
    top:0;
    left:0;
    right:0;
    bottom:0;
    background:rgba(0,0,255,0.5);
    z-index:-1;
}

在此處輸入圖片說明

這是你的HTML

2.標記:

<body>
      <div class="overlay">
          <nav>
             <ul>
                <li><a href="#portfolio">SHOP</a></li>
                <li><a href="#about">ABOUT</a></li>
                <li><a href="#contact">PRESS</a></li>
              </ul>
           </nav>
      </div>
</body>

為您的HTML和正文添加height:100% 如下更新您的CSS。

body, html{height:100%; margin:0; padding:0;}
.overlay{
background: rgba(0,0,255,0.5);
margin: 0;
width: 100%;
height: 100%;
padding:0;
}
ul{margin:0;}

演示

添加位置:固定在疊加層上:

.overlay{
    background: rgba(0,0,255,0.5);
    margin: 0 ;
    width: 100%;
    height: 100%;
    top: 0px;
    position: absolute;
    left: 0px;
}

演示

更新了CSS

.overlay{
    background: rgba(0,0,255,0.5);
    margin: 0 ;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    position: absolute;
}

100% height繼承自parent 因此,您還必須將其添加到body ,以及body's html

但是最簡單的創建方法是,將top: 0; left: 0; bottom: 0; right: 0; position: absolute | fixed;覆蓋為top: 0; left: 0; bottom: 0; right: 0; position: absolute | fixed; top: 0; left: 0; bottom: 0; right: 0; position: absolute | fixed;

使用以下代碼以獲得最佳結果

<html>
    <head>
        <style type="text/css">

            html,body {
                height: 100%;
                width: 100%
                margin: none;
                padding: none;
            }

            #background {
                width: 100%;
                height: 100%;
                position: fixed;
                left: 0px;
                top: 0px;
                z-index: -99999;
                -webkit-user-select: none;
                -khtml-user-select: none;
                -moz-user-select: none;
                -o-user-select: none;
                user-select: none;
            }

            #background img {
                width: 100%;
                height: 100%;
            }

            #main{ z-index:10;}
        </style>
    </head>
    <body>
        <div id="main">
            content here
        </div>
        <div id="background"><img src="bg.jpg"></div>
    </body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM