CSS filter Property

CSS filter Property

CSS filter property applies visual effects to an element.

Note: Filters are commonly used to adjust the rendering of images, backgrounds, and borders.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> img{ width: 350px; height: 200px; -webkit-filter: grayscale(100%); /* Safari 6.0 - 9.0 */ filter: grayscale(100%); } </style> </head> <body> <h1>CSS filter Property</h1> <img src="car-left.png"> </body> </html>

Syntax

Using CSS

element{ filter: blur(5px); }

Using Javascript

object.style.filter="blur(5px)";

Animatable

Yes, filter property is animatable. CSS Animatable Properties Reference.

Default Value

Default value for CSS filter property is none.

Property Value

The following table provides a list of values for CSS filter property.

ValueExplanation
noneSpecifies no effects.
blurApplies a blur effect to the image.
The value should be in px.
For example, blur(5px).
brightnessAdjusts the brightness of the image.
The value should be in % and should not be in negative.
For example, brightness(200%)
contrastAdjusts the contrast of the image.
The value should be in % and should not be in negative.
For example, contrast(200%)
drop-shadowApplies a drop shadow effect to the image.
The value should be in the order of drop-shadow(h-shadow v-shadow blur spread color).
For example, drop-shadow(3px 6px 5px #8c8c8c)
Here some of the parameters are optional
h-shadow - Required
v-shadow - Required
blur - Optional
spread - Optional
color - Optional
grayscaleConverts the image to grayscale.
The value should be in % and should not be in negative.
For example, grayscale(100%).
hue-rotateApplies a hue rotation on the image.
The value should be in deg (degree) and should not exceed 360deg.
For example, grayscale(255deg).
invertInverts the samples in the image.
The value should be in % and should not be in negative.
For example, invert(100%).
opacitySets the opacity level for the image.
The value should be in % and should not be in negative.
For example, opacity(100%).
saturateSaturates the image.
The value should be in % and should not be in negative.
For example, opacity(100%).
Negative values are not allowed.
sepiaConverts the image to sepia.
The value should be in % and should not be in negative.
For example, sepia(100%).

All in One

In the following example, we will demonstrate all values of CSS filter property.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> img{ width: 250px; height: 125px; } #point{ -webkit-filter: none; /* Safari 6.0 - 9.0 */ filter: none; } #point1{ -webkit-filter: blur(5px); filter: blur(5px); } #point2{ -webkit-filter: brightness(200%); filter: brightness(200%); } #point3{ -webkit-filter: drop-shadow(3px 6px 10px #8c8c8c); filter: drop-shadow(3px 6px 10px #8c8c8c); } #point4{ -webkit-filter: grayscale(100%); filter: grayscale(100%); } #point5{ -webkit-filter: hue-rotate(120deg); filter: hue-rotate(120deg); } #point6{ -webkit-filter: invert(100%); filter: invert(100%); } #point7{ -webkit-filter: opacity(20%); filter: opacity(20%); } #point8{ -webkit-filter: sepia(100%); filter: sepia(100%); } #point9{ -webkit-filter: sepia(100%) blur(5px); filter: sepia(100%) blur(5px); } </style> </head> <body> <h1>CSS filter Property</h1> <h2>filter: none (default)</h2> <img id="point" src="car-left.png"> <h2>filter: blur(5px)</h2> <img id="point1" src="car-left.png"> <h2>filter: brightness(200%)</h2> <img id="point2" src="car-left.png"> <h2>filter: drop-shadow(3px 6px 10px #8c8c8c)</h2> <img id="point3" src="car-left.png"> <h2>filter: grayscale(100%)</h2> <img id="point4" src="car-left.png"> <h2>filter: hue-rotate(120deg)</h2> <img id="point5" src="car-left.png"> <h2>filter: invert(100%)</h2> <img id="point6" src="car-left.png"> <h2>filter: opacity(20%)</h2> <img id="point7" src="car-left.png"> <h2>filter: sepia(100%)</h2> <img id="point8" src="car-left.png"> <h2>filter: sepia(100%) blur(5px)</h2> <img id="point9" src="car-left.png"> </body> </html>

Using JavaScript

In the following example, we will demonstrate how to change the CSS filter property of an element using JavaScript.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> img{ width: 250px; height: 125px; } button{ display: block; } </style> </head> <body> <h1>CSS filter Property</h1> <img src="car-left.png"> <button onclick="myFunction()">Click Me</button> <script> var x = document.getElementsByTagName("img")[0]; function myFunction(){ x.style.filter = "blur(5px)"; x.style.WebkitFilter = "blur(5px)"; } </script> </body> </html>

Reminder

Hi Developers, we almost covered 98.7% of CSS Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in CSS.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Meet the Author