[英]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规则:
为此,您要做的就是在身体上使用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.