`grayscale()` CSS function
The grayscale() CSS function converts the input image to grayscale. It is used as a value for the filter and backdrop-filter properties.
The grayscale() CSS function converts the input image to grayscale. It is used as a value for the filter and backdrop-filter properties.
CSS Demo: grayscale()
```css interactive-example-choice filter: grayscale(0);
```css interactive-example-choice
filter: grayscale(0.2);
```css interactive-example-choice filter: grayscale(60%);
```css interactive-example-choice
filter: grayscale(1);
```html interactive-example
## Syntax
```css
/* No grayscale conversion */
grayscale(0)
/* Complete grayscale conversion */
grayscale()
grayscale(1)
grayscale(100%)
/* 60% grayscale conversion */
grayscale(.6)
grayscale(60%)
Parameters
- <number> or <percentage>
- : Specifies the amount of the conversion. A value of
100%is completely grayscale, while a value of0%leaves the input unchanged. Values between0%and100%are linear multipliers on the effect. Values above100%are allowed, but the effect is clamped to100%. The initial value for interpolation is0. The default value is1.
Return value
A filter-function value.
Formal syntax
Examples
Using the SVG filter
This example shows three images for comparison: an image with a grayscale() filter function applied, an image with the equivalent SVG filter applied, and the original image.
SVG
The SVG feColorMatrix filter element can also be used to convert content to grayscale. We set the values for the color matrix, then we can reference the SVG effect by the ID of the filter:
```html live-sample___svg-filter
The following declarations produce the same effect:
```css
filter: grayscale(0.7);
filter: grayscale(70%);
filter: url("#grayscale70"); /* with embedded SVG */
filter: url("fileName.svg#grayscale70"); /* external SVG */
CSS
We include CSS that will convert elements to grayscale based on their css-filter or svg-filter class:
```css live-sample___svg-filter .css-filter { filter: grayscale(0.7); }
.svg-filter { filter: url("#grayscale70"); }
```css hidden live-sample___svg-filter
th,
td {
padding: 5px;
}
```html hidden live-sample___svg-filter
grayscale() filter |
SVG filter equivalent | Original image |
|---|---|---|
|
|
|
```
100%
Specifications
Browser compatibility
See also
- CSS filter effects module
- The other filter-function functions available to be used in values of the filter and backdrop-filter properties include:
- blur()
- brightness()
- contrast()
- drop-shadow()
- hue-rotate()
- invert()
- opacity()
- saturate()
- sepia()