Html 悬停时使背景图像变暗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17481660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:48:26  来源:igfitidea点击:

Darken background image on hover

htmlcss

提问by John Smith

How would I darken a background imageon hover withoutmaking a new darker image?

如何在制作新的较暗图像的情况下在悬停时使背景图像变暗?

CSS:

CSS:

.image {
  background: url('http://cdn1.iconfinder.com/data/icons/round-simple-social-icons/58/facebook.png');
  width: 58px;
  height: 58px;
}

JSFiddle link: http://jsfiddle.net/qrmqM/1/

JSFiddle 链接:http: //jsfiddle.net/qrmqM/1/

采纳答案by PSL

How about this, using an overlay?

这个怎么样,使用叠加层?

.image:hover > .overlay {
    width:100%;
    height:100%;
    position:absolute;
    background-color:#000;
    opacity:0.5;
    border-radius:30px;
}

Demo

演示

回答by Sabba Keynejad

Similar, but again a little bit different.

相似,但又有点不同。

Make the image 100% opacity so it is clear. And then on img hover reduce it to the opacity you want. In this example, I have also added easing for a nice transition.

使图像 100% 不透明度,使其清晰。然后在 img 悬停上将其降低到您想要的不透明度。在这个例子中,我还添加了缓动以实现良好的过渡。

img {
    -webkit-filter: brightness(100%);
}

img:hover {
    -webkit-filter: brightness(70%);
    -webkit-transition: all 1s ease;
    -moz-transition: all 1s ease;
    -o-transition: all 1s ease;
    -ms-transition: all 1s ease;
    transition: all 1s ease;
}

That will do it, Hope that helps.

这样就可以了,希望有所帮助。

Thank you Robert Byers for your jsfiddle

谢谢罗伯特·拜尔斯为您的jsfiddle

回答by Mr. Alien

If you want to darken the image, use an overlay element with rgbaand opacityproperties which will darken your image...

如果您想使图像变暗,请使用带有rgbaopacity属性的叠加元素,这将使您的图像变暗...

Demo

演示

<div><span></span></div>

div {
    background-image: url(http://im.tech2.in.com/gallery/2012/dec/stockimage_070930177527_640x360.jpg);
    height: 400px;
    width: 400px;
}

div span {
    display: block;
    height: 100%;
    width: 100%;
    opacity: 0;
    background: rgba(0,0,0,.5);
    -moz-transition: all 1s;
    -webkit-transition: all 1s;
    transition: all 1s;
}

div:hover span {
    opacity: 1;
}

Note: Am also using CSS3 transitions for smooth dark effect

注意:我也在使用 CSS3 过渡来获得平滑的暗效果



If anyone one to save an extra element in the DOM than you can use :beforeor :afterpseudo as well..

如果有人在 DOM 中保存一个额外的元素而不是你可以使用:before:after伪的..

Demo 2

演示 2

div {
    background-image: url(http://im.tech2.in.com/gallery/2012/dec/stockimage_070930177527_640x360.jpg);
    height: 400px;
    width: 400px;
}

div:after {
    content: "";
    display: block;
    height: 100%;
    width: 100%;
    opacity: 0;
    background: rgba(0,0,0,.5);
    -moz-transition: all 1s;
    -webkit-transition: all 1s;
    transition: all 1s;
}

div:hover:after {
    opacity: 1;
}


Using some content over the darkened overlay of the image

在图像变暗的覆盖层上使用一些内容

Here am using CSS Positioning techniques with z-indexto overlay content over the darkened divelement. Demo 3

这里使用 CSS 定位技术z-index将内容覆盖在变暗的div元素上。 演示 3

div {
    background-image: url(http://im.tech2.in.com/gallery/2012/dec/stockimage_070930177527_640x360.jpg);
    height: 400px;
    width: 400px;
    position: relative;
}

div:after {
    content: "";
    display: block;
    height: 100%;
    width: 100%;
    opacity: 0;
    background: rgba(0,0,0,.5);
    -moz-transition: all 1s;
    -webkit-transition: all 1s;
    transition: all 1s;
    top: 0;
    left: 0;
    position: absolute;
}

div:hover:after {
    opacity: 1;
}

div p {
    color: #fff;
    z-index: 1;
    position: relative;
}

回答by Vucko

You can use opacity:

您可以使用不透明度

.image {
    background: url('http://cdn1.iconfinder.com/data/icons/round-simple-social-icons/58/facebook.png');
    width: 58px;
    height: 58px;
    opacity:0.5;
}

.image:hover{
    opacity:1;
}

JSFiddle

JSFiddle

回答by Alon Gouldman

you can use this:

你可以使用这个:

box-shadow: inset 0 0 0 1000px rgba(0,0,0,.2);

as seen on: https://stackoverflow.com/a/24084708/8953378

如上所示:https: //stackoverflow.com/a/24084708/8953378

回答by Rory

I was able to achieve this more easily than the above answers and in a single line of code by using the new FilterCSS option.

通过使用新的FilterCSS 选项,我能够比上述答案更容易地实现这一点,并且在一行代码中。

It's compatibility in modern browsersis pretty good - 95% at time of writing, though less than the other answers.

在现代浏览器中兼容性非常好 - 在撰写本文时为 95%,但低于其他答案。

img:hover{
  filter: brightness(50%);
}
<img src='https://via.placeholder.com/300'>

回答by Falguni Panchal

try this

尝试这个

http://jsfiddle.net/qrmqM/6/

http://jsfiddle.net/qrmqM/6/

CSS

CSS

.image {
    background: url('http://cdn1.iconfinder.com/data/icons/round-simple-social-icons/58/facebook.png');
    width: 58px;
    height: 58px;
      opacity:0.4;
filter:alpha(opacity=40); /* For IE8 and earlier */
}
.image:hover{
    background: url('http://cdn1.iconfinder.com/data/icons/round-simple-social-icons/58/facebook.png');
    width: 58px;
    height: 58px;

    border-radius:100px;
  opacity:1;
            filter:alpha(opacity=100);

}

HTML

HTML

<div class="image"></div>

回答by Mohit Bhansali

Add css:

添加css:

.image{
    opacity:.5;
}

.image:hover{
    // CSS properties
    opacity:1;
}

回答by enam

Try following code:

尝试以下代码:

.image {
    background: url('http://cdn1.iconfinder.com/data/icons/round-simple-social-icons/58/facebook.png');
    width: 58px;
    height: 58px;
    opacity:0.2;
}

.image:hover{
    opacity:1;
}

回答by Praveen

.image:hover {
   background: #000;
    width: 58px;
    height: 58px;
    border-radius:60px;

}

You will get darken

你会变黑