CSS Filters can create visual effects. It is like the Photoshop filters for the browser.
The CSS filter property can have effects like blur or color shifting on an element's rendering.
Filters are commonly used to adjust the rendering of an image, a background, or a border.
filter: <filter-function> [<filter-function>]* | none
filter-function can be is one of:
The following code shows how to use blur function.
.blur-me { filter: blur(20px); }
filter |
31.0 (-webkit-) | No | No | 7.0 (-webkit-) | 18+ (-webkit-) |
grayscale()
converts the image to grayscale.
grayscale()
has the following syntax.
grayscale([ number | percentage ])
<!DOCTYPE html>
<html>
<style>
<!--from w ww .ja va2s . c o m-->
img { display: block; }
img.myFilter {
-webkit-filter: grayscale(1);
filter: grayscale(1);
}
</style>
</head>
<body>
<img src="http://www.java2s.com/style/demo/border.png">
<br/>
<img class='myFilter' src="http://www.java2s.com/style/demo/border.png">
</body>
</html>
sepia()
converts the input image to sepia.
sepia()
has the following syntax.
sepia([ number | percentage ])
<!DOCTYPE html>
<html>
<style>
<!--from w w w . j a v a2 s . c o m-->
img { display: block; }
img.myFilter {
-webkit-filter: sepia(1);
filter: sepia(1);
}
</style>
</head>
<body>
<img src="http://www.java2s.com/style/demo/border.png">
<br/>
<img class='myFilter' src="http://www.java2s.com/style/demo/border.png">
</body></html>
saturate()
saturates the input image.
saturate()
has the following syntax.
saturate([ number | percentage ])
<!DOCTYPE html>
<html>
<style>
<!-- ww w .j a va 2 s.c om-->
img { display: block; }
img.myFilter {
-webkit-filter: saturate(1.5);
filter: saturate(1.5);
}
</style>
</head>
<body>
<img src="http://www.java2s.com/style/demo/border.png">
<br/>
<img class='myFilter' src="http://www.java2s.com/style/demo/border.png">
</body></html>
hue-rotate()
applies a hue rotation on the input image.
hue-rotate()
has the following
syntax.
hue-rotate(angle)
<!DOCTYPE html>
<html>
<style>
<!-- w w w. j av a2 s . c o m-->
img { display: block; }
img.myFilter {
-webkit-filter: hue-rotate(50deg);
filter: hue-rotate(50deg);
}
</style>
</head>
<body>
<img src="http://www.java2s.com/style/demo/border.png">
<br/>
<img class='myFilter' src="http://www.java2s.com/style/demo/border.png">
</body></html>
invert()
inverts the samples in the input image.
invert()
has the following syntax.
invert([ number | percentage ])
<!DOCTYPE html>
<html>
<style>
<!-- w w w .j av a 2s . com-->
img { display: block; }
img.myFilter {
-webkit-filter: invert(0.5);
filter: invert(0.5);
}
</style>
</head>
<body>
<img src="http://www.java2s.com/style/demo/border.png">
<br/>
<img class='myFilter' src="http://www.java2s.com/style/demo/border.png">
</body></html>
opacity()
applies transparency to the samples in the input image.
This function is similar to opacity
property.
With filters, some browsers provide hardware acceleration for better performance.
opacity()
has the following syntax.
opacity([ number | percentage ])
<!DOCTYPE html>
<html>
<style>
<!-- w ww . j av a 2 s . c om-->
img { display: block; }
img.myFilter {
-webkit-filter: opacity(0.5);
filter: opacity(0.5);
}
</style>
</head>
<body>
<img src="http://www.java2s.com/style/demo/border.png">
<br/>
<img class='myFilter' src="http://www.java2s.com/style/demo/border.png">
</body></html>
brightness()
makes image appear more or less bright.
brightness()
has the following syntax.
brightness([ number | percentage ])
<!DOCTYPE html>
<html>
<style>
<!-- w ww .jav a 2 s. c o m-->
img { display: block; }
img.myFilter {
-webkit-filter: brightness(0.5);
filter: brightness(0.5);
}
</style>
</head>
<body>
<img src="http://www.java2s.com/style/demo/border.png">
<br/>
<img class='myFilter' src="http://www.java2s.com/style/demo/border.png">
</body></html>
contrast()
adjusts the contrast of the input image.
contrast()
has the following syntax.
contrast([ number | percentage ])
<!DOCTYPE html>
<html>
<style>
<!-- w ww . ja v a2 s . c o m-->
img { display: block; }
img.myFilter {
-webkit-filter: contrast(0.5);
filter: contrast(0.5);
}
</style>
</head>
<body>
<img src="http://www.java2s.com/style/demo/border.png">
<br/>
<img class='myFilter' src="http://www.java2s.com/style/demo/border.png">
</body></html>
blur()
applies a Gaussian blur to the input image.
blur()
has the following syntax.
blur(length)
<!DOCTYPE html>
<html>
<style>
<!--from w w w . j a v a2s . c om-->
img { display: block; }
img.myFilter {
-webkit-filter: blur(5px);
filter: blur(5px);
}
</style>
</head>
<body>
<img src="http://www.java2s.com/style/demo/border.png">
<br/>
<img class='myFilter' src="http://www.java2s.com/style/demo/border.png">
</body>
</html>
This function is similar to the box-shadow
property.
With filters, some browsers provide hardware acceleration for better performance.
drop-shadow()
applies a drop shadow effect to the input image.
drop-shadow()
has the following syntax.
drop-shadow(length{2,3} color)
The function accepts a parameter of type (defined in CSS3 Backgrounds), with the exception that the 'inset' keyword is not allowed.
<!DOCTYPE html>
<html>
<style>
<!-- w w w .jav a2 s . c om-->
img { display: block; }
img.myFilter {
-webkit-filter: drop-shadow(16px 16px 10px rgba(0,0,0,0.9));
filter: drop-shadow(16px 16px 10px rgba(0,0,0,0.9));
}
</style>
</head>
<body>
<img src="http://www.java2s.com/style/demo/border.png">
<br/>
<img class='myFilter' src="http://www.java2s.com/style/demo/border.png">
</body></html>
url()
function takes the location of
an XML file that specifies an SVG filter,
and may include an anchor to a specific filter element.
url()
has the following syntax.
url(urlValue)
<!DOCTYPE html>
<html class=''>
<style>
#svg-filter { display: none; }
img { display: block; }
<!--from w w w . ja v a 2 s . c o m-->
img.myFilter {
-webkit-filter: url(#svg-blur);
}
</style>
</head>
<body>
<img src="http://www.java2s.com/style/demo/border.png">
<br/>
<img class='myFilter' src="http://www.java2s.com/style/demo/border.png">
<!-- SVG Blur Filter -->
<!-- 'stdDeviation' is the blur amount applied -->
<svg id="svg-filter">
<filter id="svg-blur">
<feGaussianBlur in="SourceGraphic" stdDeviation="4"></feGaussianBlur>
</filter>
</svg>
</body></html>
<!DOCTYPE html>
<html><head>
<style>
img { display: block;
-webkit-animation: filter-animation 5s infinite;
}<!-- ww w .j av a 2 s . c o m-->
@-webkit-keyframes filter-animation {
0% {
-webkit-filter: sepia(0) saturate(2);
}
50% {
-webkit-filter: sepia(1) saturate(8);
}
100% {
-webkit-filter: sepia(0) saturate(2);
}
}
</style>
</head>
<body>
<img src="http://www.java2s.com/style/demo/border.png">
</body>
</html>