Answers:
只需将此代码添加到您的图像CSS
body{
background:
/* top, transparent black, faked with gradient */
linear-gradient(
rgba(0, 0, 0, 0.7),
rgba(0, 0, 0, 0.7)
),
/* bottom, image */
url(http://fc02.deviantart.net/fs71/i/2011/274/6/f/ocean__sky__stars__and_you_by_muddymelly-d4bg1ub.png);
}
参考:linear-gradient()-CSS | MDN
更新:并非所有浏览器都支持RGBa,因此您应该具有“后备颜色”。这种颜色很可能是纯色(完全不透明),例如:background:rgb(96, 96, 96)
。请参阅此博客以获得RGBa浏览器支持。
在伪元素后使用:
.overlay {
position: relative;
transition: all 1s;
}
.overlay:after {
content: '\A';
position: absolute;
width: 100%;
height:100%;
top:0;
left:0;
background:rgba(0,0,0,0.5);
opacity: 1;
transition: all 0.5s;
-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
}
.overlay:hover:after {
opacity: 0;
}
签出我的笔>
设置background-blend-mode
为darken
实现目标的最直接,最短的方法,但是必须首先设置background-color
混合模式才能工作。
如果以后需要在javascript中操纵值,这也是最好的方法。
background: rgba(0, 0, 0, .65) url('http://fc02.deviantart.net/fs71/i/2011/274/6/f/ocean__sky__stars__and_you_by_muddymelly-d4bg1ub.png');
background-blend-mode: darken;
可能可以这样做 box-shadow
但是,我无法真正将其应用于图像。仅在纯色背景上
body {
background: #131418;
color: #999;
text-align: center;
}
.mycooldiv {
width: 400px;
height: 300px;
margin: 2% auto;
border-radius: 100%;
}
.red {
background: red
}
.blue {
background: blue
}
.yellow {
background: yellow
}
.green {
background: green
}
#darken {
box-shadow: inset 0px 0px 400px 110px rgba(0, 0, 0, .7);
/*darkness level control - change the alpha value for the color for darken/ligheter effect */
}
Red
<div class="mycooldiv red"></div>
Darkened Red
<div class="mycooldiv red" id="darken"></div>
Blue
<div class="mycooldiv blue"></div>
Darkened Blue
<div class="mycooldiv blue" id="darken"></div>
Yellow
<div class="mycooldiv yellow"></div>
Darkened Yellow
<div class="mycooldiv yellow" id="darken"></div>
Green
<div class="mycooldiv green"></div>
Darkened Green
<div class="mycooldiv green" id="darken"></div>
您可以将背景作为绝对和负z-index放置在容器中:http : //jsfiddle.net/2YW7g/
的HTML
<div class="main">
<div class="bg">
</div>
Hello World!!!!
</div>
的CSS
.main{
width:400px;
height:400px;
position:relative;
color:red;
background-color:transparent;
font-size:18px;
}
.main .bg{
position:absolute;
width:400px;
height:400px;
background-image:url("http://fc02.deviantart.net/fs71/i/2011/274/6/f/ocean__sky__stars__and_you_by_muddymelly-d4bg1ub.png");
z-index:-1;
}
.main:hover .bg{
opacity:0.5;
}
只需添加以下内容,即可使用:
background: -moz-linear-gradient(rgba(0,0,0,.7),rgba(0,0,0,.7));
background: -webkit-linear-gradient(rgba(0,0,0,.7),rgba(0,0,0,.7));
background: linear-gradient(rgba(0,0,0,.7),rgba(0,0,0,.7));
filter: unquote("progid:DXImageTransform.Microsoft.gradient( startColorstr='#b3000000', endColorstr='#b3000000',GradientType=0 )");
...用于跨浏览器支持70%的线性渐变叠加层。要增亮图像,您可以将所有都更改0,0,0
为255,255,255
。如果70%有点高,请继续进行更改.7
。并且,供将来参考,请查看以下内容:http : //www.colorzilla.com/gradient-editor/
对我来说,过滤器/渐变方法不起作用(可能是由于现有的CSS),所以我改用了:before
伪样式技巧:
.eventBannerContainer {
position: relative;
}
.eventBannerContainer:before {
background-color: black;
height: 100%;
width: 100%;
content: "";
opacity: 0.5;
position: absolute;
display: block;
}
/* make any immediate child elements above our darkening mask */
.eventBannerContainer > * {
position: relative;
}
display
的::before
是inline
它不会接受width
和height
。