Convert an image to grayscale in HTML/CSS

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

    Support for CSS filters has landed in Webkit. So we now have a cross-browser solution.

    img {
      filter: gray; /* IE6-9 */
      -webkit-filter: grayscale(1); /* Google Chrome, Safari 6+ & Opera 15+ */
      filter: grayscale(1); /* Microsoft Edge and Firefox 35+ */
    }
    
    /* Disable grayscale on hover */
    img:hover {
      -webkit-filter: grayscale(0);
      filter: none;
    }


    What about Internet Explorer 10?

    You can use a polyfill like gray.

提交回复
热议问题