Convert an image to grayscale in HTML/CSS

前端 未结 25 1511
青春惊慌失措
青春惊慌失措 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:59

    For Firefox you don't need to create a filter.svg file, you can use data URI scheme.

    Taking up the css code of the first answer gives:

    filter: url("data:image/svg+xml;utf8,<svg%20xmlns='http://www.w3.org/2000/svg'><filter%20id='grayscale'><feColorMatrix%20type='matrix'%20values='0.3333%200.3333%200.3333%200%200%200.3333%200.3333%200.3333%200%200%200.3333%200.3333%200.3333%200%200%200%200%200%201%200'/></filter></svg>#grayscale"); /* Firefox 3.5+ */
    filter: grayscale(100%); /* Current draft standard */
    -webkit-filter: grayscale(100%); /* New WebKit */
    -moz-filter: grayscale(100%);
    -ms-filter: grayscale(100%); 
    -o-filter: grayscale(100%);
    filter: gray; /* IE6+ */
    

    Take care to replace "utf-8" string by your file encoding.

    This method should be faster than the other because the browser will not need to do a second HTTP request.

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