DEV Community

Jaydeep Pipaliya
Jaydeep Pipaliya

Posted on

4

How to Apply a Duotone Effect to Images with CSS

Understanding CSS Filters

The core of this technique is the filter CSS property. CSS filters allow us to apply graphical effects like blurring, brightness, contrast, and more to any element. For creating a duotone effect, we'll use a combination of grayscale() and sepia(), topped off with a color overlay using blend-mode.

Setting Up Your HTML Document

Let's start by setting up a basic HTML structure. We'll include an image that we want to apply the duotone effect to. For demonstration purposes, I'll use a placeholder image. Feel free to replace it with your own.



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Duotone Image Effect</title>
<style>
body {
background: #333; /* Dark background for contrast /
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.duotone-img {
width: 300px;
height: auto;
filter: grayscale(100%) sepia(100%);
mix-blend-mode: screen;
}
.duotone-overlay {
position: absolute;
background-color: #008080; /
Teal overlay */
mix-blend-mode: color;
width: 300px;
height: auto;
}
</style>
</head>
<body>
<div class="duotone-overlay"></div>
<img src="your-image-url-here" class="duotone-img" />
</body>
</html>

Enter fullscreen mode Exit fullscreen mode




RESULTS

Output of the Code
How It Works

In the CSS, we first convert the image to grayscale and then apply a sepia filter to add a warm tone. The real magic happens with the .duotone-overlay element. By setting a background color and using mix-blend-mode: color, we overlay the image with a color tint, achieving the duotone effect.

Thank you for reading, and happy coding! 🌈

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay