日本語

How to Make a Duotone Image with CSS

​​CSS​HTML,SVG

A duotone maps the dark parts of an image to one color and the bright parts to another, and blends the in-between brightness across the two colors. In CSS you can build it in two ways: apply an SVG filter with filter: url(), or stack colors with mix-blend-mode. The SVG filter gave the two colors I specified. With mix-blend-mode, the bright color came out slightly off.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I made a 256×192 pixel image with a colorful landscape on top and a black-to-white gradient strip underneath, applied each duotone, captured it with Playwright, and measured the pixel colors numerically. I did not test Firefox, Safari, or real smartphones. The browser support notes come from the MDN browser compatibility data (@mdn/browser-compat-data 8.1.8).

Here, the dark color is #1f2a30 (a navy close to black), and the bright color is #00c8e8 (a light cyan).

Three images stacked vertically. The first, "Original image", shows a blue-to-orange sky, a yellow sun, a green hill, and a red square house, with a black-to-white strip below. The second, "SVG filter", and the third, "CSS blend", show the same picture drawn only in navy and cyan, and the strip below has also changed from navy to cyan. The third is slightly brighter and looks grayish
The original image, and the result of making it two-color with each method. The strip at the bottom is a black-to-white gradient in the original

Making two exact colors with an SVG filter

Place a hidden <svg> in the HTML, define a filter in it, and apply it to the image with CSS filter.

html
<svg width="0" height="0" style="position: absolute" aria-hidden="true">
  <filter id="duotone" color-interpolation-filters="sRGB">
    <!-- First, make it grayscale -->
    <feColorMatrix type="saturate" values="0" />
    <!-- Map black to #1f2a30 and white to #00c8e8, and blend the tones in between -->
    <feComponentTransfer>
      <feFuncR type="table" tableValues="0.122 0" />
      <feFuncG type="table" tableValues="0.165 0.784" />
      <feFuncB type="table" tableValues="0.188 0.910" />
    </feComponentTransfer>
  </filter>
</svg>

<img class="duotone-svg" src="photo.png" width="256" height="192" alt="">
css
.duotone-svg { filter: url(#duotone); }

The filter changes the colors in two steps.

The first step, <feColorMatrix>, uses type="saturate" with values="0", which sets the color saturation to 0 and makes the image grayscale. In the Filter Effects specification, the resulting brightness is the sum of red, green, and blue multiplied by 0.2126, 0.7152, and 0.0722. CSS grayscale(1) uses the same coefficients.

The second step, <feComponentTransfer>, replaces each of red, green, and blue with the values set by its child elements <feFuncR>, <feFuncG>, and <feFuncB>. With type="table", two numbers in tableValues map 0 (black) to the first number and 1 (white) to the second, with a straight line between them. Because the image is already grayscale, every pixel lands somewhere on that line between the dark and the bright color.

The values in tableValues are colors written from 0 to 1, which means the hex value divided by 255.

Red Green Blue
Dark #1f2a30 31 ÷ 255 = 0.122 42 ÷ 255 = 0.165 48 ÷ 255 = 0.188
Bright #00c8e8 0 ÷ 255 = 0 200 ÷ 255 = 0.784 232 ÷ 255 = 0.910

In the test environment, I measured the gradient strip below. The left end, which was black, came out as (31, 42, 47), and the right end, which was white, came out as (0, 199, 232). #1f2a30 is (31, 42, 48) and #00c8e8 is (0, 200, 232), so both differ by no more than 1. I compared every pixel, including the landscape, against the color you get by interpolating between the two colors using the grayscale brightness. The largest difference was 1.33.

In the test environment's Chromium, the filter gave the same colors even when the <svg> was hidden with style="display: none".

Forgetting color-interpolation-filters="sRGB" shifts the colors

The <filter> in the code above includes color-interpolation-filters="sRGB". Without it, the filter does not produce the colors you specify.

color-interpolation-filters sets the color space in which the filter calculates, and its initial value is linearRGB. In linearRGB, brightness is converted to values proportional to light intensity before the calculation. The numbers in tableValues are also treated as values in that space, so even if you write #1f2a30 divided by 255, the color on screen is not #1f2a30. According to the specification, CSS filter functions such as grayscale() always calculate in sRGB regardless of this property.

Two images, one above the other. The top one, labeled "color-interpolation-filters="sRGB" set", shows the navy-to-cyan picture, and the left end of the strip below is nearly the dark navy. The bottom one, labeled "Not set (initial value linearRGB)", is overall a washed-out grayish cyan, and the left end of its strip is gray
The same tableValues filter, with only the color-interpolation-filters setting changed

In the test environment, I applied the filter with only color-interpolation-filters removed. The gradient strip colors were as follows.

Position in strip sRGB set Not set (linearRGB)
Left end (black in the original) (31, 42, 47) (98, 113, 119)
Middle (gray 128 in the original) (15, 121, 140) (86, 148, 158)
Right end (white in the original) (0, 199, 232) (0, 229, 245)

Without the setting, the black turned gray, and the whole image looked washed out. For a duotone filter, set color-interpolation-filters="sRGB" on the <filter>.

Making a two-color image with CSS alone using mix-blend-mode

You can also do it with CSS only, without an <svg> in the HTML. Set a bright background color on an element wrapping the image, then stack a grayscale image and a layer painted with the dark color (::after) on top, in order, using mix-blend-mode.

html
<div class="duotone-blend">
  <img src="photo.png" width="256" height="192" alt="">
</div>
css
.duotone-blend {
  position: relative;
  background: #00c8e8; /* Color for the bright parts */
}
.duotone-blend img {
  filter: grayscale(1);
  mix-blend-mode: multiply;
}
.duotone-blend::after {
  content: "";
  position: absolute;
  inset: 0;
  background: #1f2a30; /* Color for the dark parts */
  mix-blend-mode: screen;
}

The Compositing and Blending specification defines the color values from 0 to 1. multiply gives the product of the backdrop and the source colors, and screen gives "backdrop + source − backdrop × source". Blending the grayscale image onto the cyan background with multiply keeps black as black and turns white into cyan. Then blending the navy layer on top with screen turns what was black into navy.

In the test environment, the left end of the gradient strip (black in the original) measured (31, 42, 48), the same as #1f2a30. However, the right end (white in the original) measured (31, 209, 236), which is brighter than #00c8e8's (0, 200, 232). Because the navy layer also goes through screen over the bright parts, the white areas become the result of screening navy over cyan. In this example the red channel of the bright color is 0 and the navy's red is 31, so red was 31 at all five measured points on the strip. I compared every pixel against the colors calculated with the multiply and screen formulas. The largest difference was 1.21, so the layers blended exactly as the formulas predict.

The dark color comes out as specified, but the bright color is the result of screening the two colors together. If you need the bright color exactly as specified, use the SVG filter.

Which one to use

Here is a summary of the differences between the two methods.

SVG filter mix-blend-mode
Color for the originally black areas The dark color you specify The dark color you specify
Color for the originally white areas The bright color you specify The two colors screened together
Where the code goes <svg> in the HTML, filter in the CSS CSS only (the HTML needs an element wrapping the image)
How colors are written Written as numbers from 0 to 1 Written as normal CSS colors

According to the MDN browser compatibility data, the CSS filter property itself is supported from Chrome 53, Edge 12, Firefox 35, and Safari 9.1, and mix-blend-mode is supported from Chrome 41, Edge 79, Firefox 32, and Safari 8. <feColorMatrix> and <feComponentTransfer> are supported from Chrome 5, Edge 12, Firefox 3, and Safari 6.

​Posted by 86