Darken background image on hover

后端 未结 14 1364
囚心锁ツ
囚心锁ツ 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:05

    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

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

    Add css:

    .image{
        opacity:.5;
    }
    
    .image:hover{
        // CSS properties
        opacity:1;
    }
    
    0 讨论(0)
  • 2020-12-22 23:09

    try this

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

    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

    <div class="image"></div>
    
    0 讨论(0)
  • 2020-12-22 23:12

    The simplest way to do it without adding an extra overlay element, or using two images, is to use the :before or :after selector.

    .image {
        position: relative;
    }
    .image:hover:after {
        content: ""; 
        position: absolute;
        width: 100%;
        height: 100%;
        background: rgba(0,0,0,0.1);
        top: 0;
        left:0;
    }
    

    This will not work in older browsers of course; just say it degrades gracefully!

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

    If you want to darken the image, use an overlay element with rgba and opacity properties which will darken your image...

    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


    If anyone one to save an extra element in the DOM than you can use :before or :after pseudo as well..

    Demo 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-index to overlay content over the darkened div element. Demo 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;
    }
    
    0 讨论(0)
  • 2020-12-22 23:18
    .image:hover {
       background: #000;
        width: 58px;
        height: 58px;
        border-radius:60px;
    
    }
    

    You will get darken

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