CSS Grayscale to Color

An image that sits in black and white and smoothly fades into full color on hover or keyboard focus.

  • grayscale
  • filter
  • image hover
  • black and white
  • gallery
  • logos
HTML
<a class="gray-hover" href="#">
  <img src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 60 40'%3E%3Cdefs%3E%3ClinearGradient id='s' x2='0' y2='1'%3E%3Cstop offset='0' stop-color='%23f472b6'/%3E%3Cstop offset='1' stop-color='%23fdba74'/%3E%3C/linearGradient%3E%3C/defs%3E%3Crect width='60' height='40' fill='url(%23s)'/%3E%3Ccircle cx='42' cy='15' r='7' fill='%23fef08a'/%3E%3Cpath d='M0 40V26l12-8 10 7 9-5 14 9 15-6v17z' fill='%237c3aed'/%3E%3Cpath d='M0 40v-8l16-5 12 4 14-6 18 6v9z' fill='%234c1d95'/%3E%3C/svg%3E" alt="Purple hills under a pink sunset" width="240" height="160">
</a>
CSS
.gray-hover {
  display: block;
  width: 240px;
  overflow: hidden;
  border-radius: 16px;
}

.gray-hover img {
  display: block;
  width: 100%;
  height: auto;
  filter: grayscale(1) contrast(1.05);
  transition: filter 0.5s ease;
}

.gray-hover:hover img,
.gray-hover:focus-visible img {
  filter: none;
}

.gray-hover:focus-visible {
  outline: 3px solid #7c3aed;
  outline-offset: 3px;
}

@media (prefers-reduced-motion: reduce) {
  .gray-hover img {
    transition: none;
  }
}

About this CSS Grayscale to Color

This CSS Grayscale to Color is 30 lines of pure CSS (0.5 KB) with no JavaScript and no dependencies. It uses transitions, filters, keyboard focus styles and reduced-motion support. It works in current versions of Chrome, Edge, Safari and Firefox. It works in any stack: plain HTML, React, Vue, Svelte, Astro, WordPress or Webflow.

CSS features used

Customize it

The colors in this snippet. Search and replace them to match your brand:

  • #7c3aed

How to use this grayscale to color

  1. Copy the HTML and paste it where you want the element to appear.
  2. Copy the CSS into your stylesheet, or inside a <style> tag.
  3. Keep the .gray-hover class name, or rename it in both places. Then tweak colors, sizes and timing to match your design.
  4. Using React or Next.js? Change class to className in the HTML. The CSS works as is, including in Tailwind projects (put it in your global stylesheet).

Building with an AI tool?

“Copy for AI” copies a short prompt for Cursor, Claude Code, Lovable, Bolt or v0. Your AI tool opens the Markdown version of this snippet and reads the HTML, CSS and setup notes there. If your tool can't open links, with the code instead. More ways to use copy CSS with AI.

Feedback