Skip to content

Tailwind CSS - Grayscale

The grayscale filter utility in Tailwind CSS allows you to convert an element to shades of gray. It’s a powerful tool for creating visual effects, indicating disabled states, or for aesthetic design choices. This is part of Tailwind’s broader set of filter utilities.

Tailwind provides two primary classes for applying or removing a grayscale effect.

ClassCSS Properties
grayscalefilter: grayscale(100%);
grayscale-0filter: grayscale(0);
  • grayscale: Applies a full 100% grayscale filter, making the element completely black and white.
  • grayscale-0: Removes any grayscale effect, restoring the element to its original colors. This is particularly useful for reversing the effect on hover or other states.

Let’s see how to use these classes to create an interactive effect where a photo is initially grayscale and reveals its color on hover. This demonstrates the power of combining utility classes with state variants like hover:.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tailwind CSS Grayscale Example</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
/* For demo purposes: a placeholder image service */
.placeholder-img { content: url('https://picsum.photos/seed/tech/400/300'); }
</style>
</head>
<body class="bg-slate-100 flex items-center justify-center min-h-screen p-4 font-sans">
<div class="w-full max-w-sm mx-auto">
<h1 class="text-2xl font-bold text-slate-800 text-center mb-6">Grayscale Filter Utilities</h1>
<div class="grid grid-cols-1 md:grid-cols-2 gap-8">
<!-- Card 1: Static Grayscale -->
<div class="bg-white rounded-lg shadow-lg overflow-hidden">
<img alt="A random landscape completely in grayscale" class="w-full h-48 object-cover grayscale placeholder-img">
<div class="p-4">
<h3 class="font-semibold text-slate-700">Static Grayscale</h3>
<p class="text-sm text-slate-500 mt-1">Using `grayscale`</p>
</div>
</div>
<!-- Card 2: Interactive Grayscale -->
<div class="bg-white rounded-lg shadow-lg overflow-hidden">
<img alt="A random landscape that turns to color on hover" class="w-full h-48 object-cover grayscale hover:grayscale-0 transition-all duration-300 cursor-pointer placeholder-img">
<div class="p-4">
<h3 class="font-semibold text-slate-700">Hover Effect</h3>
<p class="text-sm text-slate-500 mt-1">Using `grayscale hover:grayscale-0`</p>
</div>
</div>
</div>
</div>
</body>
</html>

Modern Tailwind CSS, with its Just-in-Time (JIT) compiler, allows you to use arbitrary values. If 100% grayscale is too strong, you can apply a custom amount directly in your HTML. This provides incredible flexibility without needing to modify your configuration file.

The syntax uses square brackets [] to enclose the custom value. For example:

<!-- Apply a 50% grayscale filter -->
<img class="grayscale-[50%]" src="..." alt="...">
<!-- Apply a 25% grayscale filter on hover -->
<img class="grayscale-0 hover:grayscale-[25%]" src="..." alt="...">

While visually interesting, ensure that grayscale filters do not hinder usability. For instance, if you use color to convey information (e.g., in charts or status indicators), applying a grayscale filter could remove that context. Use it thoughtfully to enhance, not detract from, the user experience.