EN ES
Home > Web development > CSS Tutorials > How to Apply Filters to Images Using the Filter Property in CSS

How to Apply Filters to Images Using the Filter Property in CSS

Diego Cortés
Diego Cortés
September 28, 2024
How to Apply Filters to Images Using the Filter Property in CSS

CSS filters are a powerful tool that allow you to modify the appearance of images on your website. With the filter property, you can apply styling effects such as blur, brightness, contrast, and more. In this article, we will explore how to use the filter property in CSS effectively, so you can enhance your web projects.

What is the Filter Property in CSS?

The filter property in CSS allows you to apply visual effects to elements on a web page, such as images, videos, and other graphical elements. This property is supported by most modern browsers and provides a simple way to add visual effects without the need for external tools or image editing applications.

Syntax of the Filter Property

The basic syntax of the filter property is as follows:

selector {
    filter: filter-function(value);
}

Where filter-function can be one of the various available filter functions and value is the value you assign.

Common Filter Effects

Next, we will discuss some of the most common filter effects that you can apply to images using the filter property.

1. Blur

The blur effect makes the image appear more diffuse, which can be useful for creating a soft background or directing attention to other elements on the page.

img {
    filter: blur(5px);
}

2. Brightness

The brightness() function allows you to adjust the brightness of the image. A value less than 1 darkens the image, while a value greater than 1 lightens it.

img {
    filter: brightness(1.5);
}

3. Contrast

The contrast adjusts the difference between the light and dark areas of an image. Similar to brightness, a value less than 1 reduces contrast, while a value greater than 1 increases it.

img {
    filter: contrast(200%);
}

4. Grayscale

The grayscale() function converts the image to grayscale. A value of 100% produces a completely black-and-white image.

img {
    filter: grayscale(100%);
}

5. Sepia

The sepia() filter applies a brown tint to the image, giving it a vintage look.

img {
    filter: sepia(100%);
}

Combining Multiple Filters

You can apply several filters to the same image by combining them in the filter property. It’s important to remember that the order in which you apply them can affect the final result.

img {
    filter: brightness(1.2) contrast(1.5) sepia(0.5);
}

Practical Example

Below is a practical example of how to apply different filters to a gallery of images using HTML and CSS.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Image Filters with CSS</title>
    <style>
        img {
            width: 300px;
            height: auto;
            margin: 10px;
            transition: filter 0.5s;
        }

        img:hover {
            filter: brightness(1.2) contrast(1.3) blur(2px);
        }
    </style>
</head>
<body>
    <h1>Image Gallery</h1>
    <img src="image1.jpg" alt="Image 1">
    <img src="image2.jpg" alt="Image 2">
    <img src="image3.jpg" alt="Image 3">
</body>
</html>

Conclusions

The CSS filter property allows you to apply a wide variety of visual effects to your images simply and effectively. From blurs to brightness and contrast adjustments, filters can enhance the user experience on your website. Don’t forget to consider SEO best practices when working with images to ensure better performance in search engines. Start experimenting with filters in your next project and transform your images!

Diego Cortés
Diego Cortés
Full Stack Developer, SEO Specialist with Expertise in Laravel & Vue.js and 3D Generalist

Categories

Page loaded in 30.41 ms