DO YOU NEED A CONTENT WRITER FOR YOUR BUSINESS?

Your One-Stop Solution for All Content Needs! Click here for more!
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.

CSS Designer

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

Why is SEO a non-negotiable part of modern marketing?

In a world where digital presence is paramount, the question isn't whether you should do…

7 hours ago

Innovations in Hair Care: Exploring Breakthroughs for Lasting Results

Over the years, people have experimented with various methods to maintain healthy and beautiful hair.…

1 day ago

How To Strengthen Your Brand-Building Efforts?

Your brand more than developing an attractive and creative logo and infectious motto. It's the…

2 days ago

How To Be Successful And Maximize Compensation In A Personal Injury Case

Introduction Are you someone who has suffered from a personal injury and want to file…

2 days ago

What Are The Biggest Challenges Of Working From Home?

Operating from home has emerged as one of the most popular ways of doing jobs…

3 days ago

Art As An Asset: Will NFT’s Help You Survive Hyperinflation?

If the consequences of our society’s ever-growing debt are what worries you, then it is…

4 days ago