Darken background image on hover

后端 未结 14 1366
囚心锁ツ
囚心锁ツ 2020-12-22 23:03

How would I darken a background image on hover without making a new darker image?

CSS:

.image {
  background: url(\         


        
相关标签:
14条回答
  • 2020-12-22 23:18

    I would add a div around the image and make the image change in opacity on hover and add an inset box shadow to the div on hover.

    img:hover{
        opacity:.5;
    }
    .image:hover{
        box-shadow: inset 10px 10px 100px 100px #000;
    }
    
    <div class="image"><img src="image.jpg" /></div>
    
    0 讨论(0)
  • 2020-12-22 23:18

    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

    0 讨论(0)
  • 2020-12-22 23:19

    Just try this code.

    img:hover
    {
        box-shadow: 0 25px 50px -25px rgba(0, 0, 0, .5) inset; 
        -webkit-box-shadow: 0 25px 50px -25px rgba(0, 0, 0, .5) inset; 
        -moz-box-shadow: 0 25px 50px -25px rgba(0, 0, 0, .5) inset; 
        -o-box-shadow: 0 25px 50px -25px rgba(0, 0, 0, .5) inset;
    }
    
    0 讨论(0)
  • 2020-12-22 23:20

    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;
    }
    
    0 讨论(0)
  • 2020-12-22 23:22

    If you have to use the current image and get a darker image then you need to create a new one. Else you can simply reduce the opacity of the .image class and the in the .image:hover you can put a higher value for opacity. But then the image without hover would look pale.

    The best way would be to create two images and add the following :

    .image {
        background: url('http://cdn1.iconfinder.com/data/icons/round-simple-social-icons/58/facebook.png');
        width: 58px;
        height: 58px;
        opacity:0.9;   
    }
    .image:hover{
        background: url('http://cdn1.iconfinder.com/data/icons/round-simple-social-icons/58/facebook_hover.png');
    }
    }
    
    0 讨论(0)
  • 2020-12-22 23:27

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

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

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

    0 讨论(0)
提交回复
热议问题