Categories: Css3

Convert an image to grayscale in CSS

Share

Want to display a color bitmap in grayscale with just HTML/CSS? A quite little effort is sufficient for a lazy person to do this.

We all know that CSS filters aren’t yet widely supported for all cases but, they are actually impressive and a basic need for web environment imagery. This allow you to modify the display of images in a variety of ways and like one of those ways is being displaying the images as a grayscale.

And doing so requires a most minimal of CSS change. Also you can even animate the image to or from grayscale. Amazing right?

It is possible to do this with the help of SVG and Canvas but, seems like with CSS it doesn’t put extra effort on this to work right now.

We now have a cross-browser solution as CSS filters has landed in the Webkit.

Read Also: Apply CSS to half of a character

Suppose, we have a following image HTML:

<div id="imgGrey"><img src="image.jpg"></div>

So, by default this will display the original image with actual color.
But, if we want to display the image as a greyscale and hovering mouse, we want to display the actual color the the following code can help.

#imgGrey img {
filter: gray; /* For IE6-9 */filter: grayscale(1); /* For Microsoft Edge and Firefox 35+ */-webkit-filter: grayscale(1); /* For Google Chrome, Safari 6+ & Opera 15+ */}

/* Now disable grayscale on hover */#imgGrey img:hover {
filter: none;
-webkit-filter: grayscale(0);
}

CSS filters also allow much more than just adjusting the grayscale and remember that if the user chooses to download the image, then this will be downloaded in its original colored displays. So, for online display purposes, CSS filters can do the little trick!

See the DEMO here.

View Comments

  • The images shown do not actually take these properties in your post. It's just an image that's got a colored side and a gray side.

    That's pretty misleading, and sad considering I spent some of my time trying to figure out why this wasn't working in IE11 on my site, and it was working here (it isn't really working here).

    • This is just an image i.e. shown in the article. Actually I described the code over there. For a better view I have included the DEMO link at the bottom of the article. You can check from there.

Recent Posts

Overview of Reputation, Services, and Features of IplWin

IplWin stands as a reliable and enthralling platform for Indian punters, offering a captivating blend…

1 day ago

Blogging Brilliance: Driving Traffic and Engagement with Quality Content

Introduction In today's online age, consumers are constantly bombarded with information. They crave valuable content…

1 day ago

How Assisted Living Gives Seniors More Freedom

In today’s rapidly aging society, finding a living situation that provides older adults with both…

1 day ago

Should you go for a Refurbished Mac?

If you wish to purchase a Mac, then it is strongly suggested to consider purchasing…

2 days ago

How Many Types of Bits are Used in Drilling Operations?

Oil drilling is a complex process that involves several components, including the drilling bit. The…

2 days ago

Best Watches to Match Your Business Look

Watches can be more than a fashion. They can be a symbol, especially in the…

3 days ago