Convert an image to grayscale in HTML/CSS

前端 未结 25 1453
青春惊慌失措
青春惊慌失措 2020-11-22 10:22

Is there a simple way to display a color bitmap in grayscale with just HTML/CSS?

It doesn\'t need to be IE-compatible (and I imagine it won\'t be) -- if

相关标签:
25条回答
  • 2020-11-22 10:51

    support for native CSS filters in webkit has been added from the current version 19.0.1084.46

    so -webkit-filter: grayscale(1) will work and which is easier than SVG approach for webkit...

    0 讨论(0)
  • 2020-11-22 10:51

    Try this jquery plugin. Although, this is not a pure HTML and CSS solution, but it is a lazy way to achieve what you want. You can customize your greyscale to best suit your usage. Use it as follow:

    $("#myImageID").tancolor();
    

    There's an interactive demo. You can play around with it.

    Check out the documentation on the usage, it is pretty simple. docs

    0 讨论(0)
  • 2020-11-22 10:53

    A new way to do this has been available for some time now on modern browsers.

    background-blend-mode allows you to get some interesting effects, and one of them is grayscale conversion

    The value luminosity , set on a white background, allows it. (hover to see it in gray)

    .test {
      width: 300px;
      height: 200px;
        background: url("http://placekitten.com/1000/750"), white; 
        background-size: cover;
    }
    
    .test:hover {
        background-blend-mode: luminosity;
    }
    <div class="test"></div>

    The luminosity is taken from the image, the color is taken from the background. Since it is always white, there is no color.

    But it allows much more.

    You can animate the effect setting 3 layers. The first one will be the image, and the second will be a white-black gradient. If you apply a multiply blend mode on this, you will get a white result as before on the white part, but the original image on the black part (multiply by white gives white, multiplying by black has no effect.)

    On the white part of the gradient, you get the same effect as before. On the black part of the gradient, you are blending the image over itself, and the result is the unmodified image.

    Now, all that is needed is to move the gradient to get this effect dynamic: (hover to see it in color)

    div {
        width: 600px;
        height: 400px;
    }
    
    .test {
        background: url("http://placekitten.com/1000/750"), 
    linear-gradient(0deg, white 33%, black 66%), url("http://placekitten.com/1000/750"); 
        background-position: 0px 0px, 0px 0%, 0px 0px;
        background-size: cover, 100% 300%, cover;
        background-blend-mode: luminosity, multiply;
        transition: all 2s;
    }
    
    .test:hover {
        background-position: 0px 0px, 0px 66%, 0px 0px;
    }
    <div class="test"></div>

    reference

    compatibility matrix

    0 讨论(0)
  • 2020-11-22 10:56

    Maybe this way help you

    img {
        -webkit-filter: grayscale(100%); /* Chrome, Safari, Opera */
        filter: grayscale(100%);
    }
    

    w3schools.org

    0 讨论(0)
  • 2020-11-22 10:57

    You can use one of the functions of jFunc - use the function "jFunc_CanvasFilterGrayscale" http://jfunc.com/jFunc-functions.aspx

    0 讨论(0)
  • 2020-11-22 10:58

    Here's a mixin for LESS that will let you choose any opacity. Fill in the variables yourself for plain CSS at different percentages.

    Neat hint here, it uses the saturate type for the matrix so you don't need to do anything fancy to change the percentage.

    .saturate(@value:0) {
        @percent: percentage(@value);
    
        filter: url("data:image/svg+xml;utf8,<svg%20xmlns='http://www.w3.org/2000/svg'><filter%20id='grayscale'><feColorMatrix%20type='saturate'%20values='@value'/></filter></svg>#grayscale"); /* Firefox 3.5+ */
        filter: grayscale(@percent); /* Current draft standard */
        -webkit-filter: grayscale(@percent); /* New WebKit */
        -moz-filter: grayscale(@percent);
        -ms-filter: grayscale(@percent);
        -o-filter: grayscale(@percent);
    }
    

    Then use it:

    img.desaturate {
        transition: all 0.2s linear;
        .saturate(0);
        &:hover {
            .saturate(1);
        }
    }
    
    0 讨论(0)
提交回复
热议问题