The CSS filter
property applies graphical effects like blurring or color shifting to an element. The sepia()
function specifically adds a sepia tone to an image, making it appear warmer and more old-fashioned. The effect's intensity can be controlled by a value, where 1
or 100%
represents a full sepia effect, and 0
or 0%
leaves the image unchanged.
Example 1: Basic Sepia Effect
HTML
<img class="sepia-basic" src="image.jpg" alt="A landscape">
CSS
/* Applies a full sepia effect to the image */
.sepia-basic {
filter: sepia(100%);
}
Explanation:
This code applies a complete sepia effect to the image with the class sepia-basic
. The sepia(100%)
value converts all the colors in the image to their corresponding sepia tones, creating a classic, aged photograph look.
Example 2: Partial Sepia Effect
HTML
<img class="sepia-partial" src="image.jpg" alt="A portrait">
CSS
/* Applies a 50% sepia effect to the image */
.sepia-partial {
filter: sepia(0.5);
}
Explanation:
Here, the sepia(0.5)
function is used to apply a half-strength sepia filter. This results in a less intense, more subtle vintage effect compared to a full 100%
value.
Example 3: Sepia on Hover
HTML
<img class="sepia-hover" src="image.jpg" alt="Cityscape">
CSS
/* Smoothly transitions to a sepia effect on hover */
.sepia-hover {
transition: filter 0.3s ease-in-out;
}
.sepia-hover:hover {
filter: sepia(1);
}
Explanation:
This example demonstrates a common interactive effect. The image appears normal initially, but when the user hovers their mouse over it, a sepia filter is smoothly applied over 0.3 seconds.
Example 4: Combining Sepia with Other Filters
HTML
<img class="sepia-combined" src="image.jpg" alt="A flower">
CSS
/* Applies both a sepia and a brightness filter */
.sepia-combined {
filter: sepia(0.8) brightness(1.1);
}
Explanation:
The filter
property allows for multiple functions to be chained together. In this case, an 80% sepia effect is combined with a 10% increase in brightness, creating a unique, stylized look.
Example 5: Sepia on a Video Element
HTML
<video class="sepia-video" controls>
<source src="movie.mp4" type="video/mp4">
</video>
CSS
/* Applies a sepia effect to a video element */
.sepia-video {
filter: sepia(90%);
}
Explanation:
The filter
property is not limited to images. This code shows how to apply a 90%
sepia effect directly to an HTML5 video, giving the entire playback a vintage film aesthetic.
Example 6: Using calc()
with Sepia
HTML
<img class="sepia-calc" src="image.jpg" alt="An animal">
CSS
/* Uses calc() to determine the sepia intensity */
.sepia-calc {
filter: sepia(calc(0.25 * 2)); /* results in sepia(0.5) */
}
Explanation:
This demonstrates using the calc()
function within the sepia()
filter. While simple in this case, calc()
can be used for more complex calculations, potentially involving CSS variables for dynamic themeing.
Example 7: Sepia Filter on Text
HTML
<div class="sepia-text">
<h1>Vintage Heading</h1>
</div>
CSS
/* Applies a sepia filter to a parent container */
.sepia-text {
filter: sepia(0.7);
background-color: #fff; /* A background is needed for the filter to apply */
display: inline-block; /* Ensures the filter wraps the content */
color: #222;
}
Explanation:
This example applies a sepia filter to a div
element containing text. For the filter
to affect the text's color, the element needs a background. The sepia effect will tint both the background and the text content.