CSS Filters are a powerful tool that authors can use to achieve varying visual effects (sort of like Photoshop filters for the browser). The CSS filter
property provides access to effects like blur or color shifting on an element’s rendering before the element is displayed. Filters are commonly used to adjust the rendering of an image, a background, or a border.
The syntax is:
.filter-me { filter: <filter-function> [<filter-function>]* | none }
Where is one of:
- blur()
- brightness()
- contrast()
- url()
- drop-shadow()
- grayscale()
- hue-rotate()
- invert()
- opacity()
- sepia()
- custom() – “coming soon”
Multiple functions can be used, space separated.
Example of a single filter being used:
.blur-me { filter: blur(20px); }
Example of multiple filters being used:
.do-more-things { filter: blur(20px) grayscale(20%); }
Filter Functions
To use the CSS filter property, you specify a value for one of the following functions listed above. If the value is invalid, the function returns “none.” Except where noted, the functions that take a value expressed with a percent sign (as in 34%) also accept the value expressed as decimal (as in 0.34).
Below we’ll use a variety of different filter functions on this image:
grayscale()
grayscale([ <number> | <percentage> ])
Converts the input image to grayscale. The value of “amount” defines the proportion of the conversion. A value of 100% is completely grayscale. A value of 0% leaves the input unchanged. Values between 0% and 100% are linear multipliers on the effect. If the “amount” parameter is missing, a value of 100% is used. Negative values are not allowed.
img { display: block; width: 90%; } img { -webkit-filter: grayscale(1); filter: grayscale(1); }
sepia()
sepia([ <number> | <percentage> ])
Converts the input image to sepia. The value of “amount” defines the proportion of the conversion. A value of 100% is completely sepia. A value of 0 leaves the input unchanged. Values between 0% and 100% are linear multipliers on the effect. If the “amount” parameter is missing, a value of 100% is used. Negative values are not allowed.
img { display: block; width: 90%; } img { -webkit-filter: sepia(1); filter: sepia(1); }
saturate()
saturate([ <number> | <percentage> ])
Saturates the input image. The value of “amount” defines the proportion of the conversion. A value of 0% is completely un-saturated. A value of 100% leaves the input unchanged. Other values are linear multipliers on the effect. Values of amount over 100% are allowed, providing super-saturated results. If the “amount” parameter is missing, a value of 100% is used. Negative values are not allowed.
img { display: block; width: 90%; } img { -webkit-filter: saturate(8); filter: saturate(8); }
hue-rotate()
hue-rotate(<angle>)
Applies a hue rotation on the input image. The value of “angle” defines the number of degrees around the color circle the input samples will be adjusted. A value of 0deg leaves the input unchanged. If the “angle” parameter is missing, a value of 0deg is used. Maximum value is 360deg.
img { display: block; width: 90%; } img { -webkit-filter: hue-rotate(90deg); filter: hue-rotate(90deg); }
invert()
invert([ <number> | <percentage> ])
Inverts the samples in the input image. The value of “amount” defines the proportion of the conversion. A value of 100% is completely inverted. A value of 0% leaves the input unchanged. Values between 0% and 100% are linear multipliers on the effect. If the “amount” parameter is missing, a value of 100% is used. Negative values are not allowed.
img { display: block; width: 90%; } img { -webkit-filter: invert(.8); filter: invert(.8); }
opacity()
opacity([ <number> | <percentage> ])
Applies transparency to the samples in the input image. The value of “amount” defines the proportion of the conversion. A value of 0% is completely transparent. A value of 100% leaves the input unchanged. Values between 0% and 100% are linear multipliers on the effect. This is equivalent to multiplying the input image samples by amount. If the “amount” parameter is missing, a value of 100% is used. This function is similar to the more established opacity property; the difference is that with filters, some browsers provide hardware acceleration for better performance. Negative values are not allowed.
img { display: block; width: 90%; } img { -webkit-filter: opacity(.2); filter: opacity(.2); }
brightness()
brightness([ <number> | <percentage> ])
Applies a linear multiplier to input image, making it appear more or less bright. A value of 0% will create an image that is completely black. A value of 100% leaves the input unchanged. Other values are linear multipliers on the effect. Values of an amount over 100% are allowed, providing brighter results. If the “amount” parameter is missing, a value of 100% is used.
img { display: block; width: 90%; } img { -webkit-filter: brightness(3); filter: brightness(3); }
contrast()
contrast([ <number> | <percentage> ])
Adjusts the contrast of the input. A value of 0% will create an image that is completely black. A value of 100% leaves the input unchanged. Values of amount over 100% are allowed, providing results with less contrast. If the “amount” parameter is missing, a value of 100% is used.
img { display: block; width: 90%; } img { -webkit-filter: contrast(4); filter: contrast(4); }
blur()
blur(<length>)
Applies a Gaussian blur to the input image. The value of ‘radius’ defines the value of the standard deviation to the Gaussian function, or how many pixels on the screen blend into each other, so a larger value will create more blur. If no parameter is provided, then a value 0 is used. The parameter is specified as a CSS length, but does not accept percentage values.
img { display: block; width: 90%; } img { -webkit-filter: blur(5px); filter: blur(5px); }
drop-shadow()
drop-shadow(<length>{2,3} <color>?)
Applies a drop shadow effect to the input image. A drop shadow is effectively a blurred, offset version of the input image’s alpha mask drawn in a particular color, composited below the image. The function accepts a parameter of type (defined in CSS3 Backgrounds), with the exception that the ‘inset’ keyword is not allowed.
This function is similar to the more established box-shadow property; the difference is that with filters, some browsers provide hardware acceleration for better performance.
img { display: block; width: 90%; } img { -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)); }
Drop-shadow also mimics the intended objects outline naturally unlike box-shadow
that treats only the box as its path.
<div class="tri drop-shadow">filter: drop-shadow</div> <div class="tri box-shadow">box-shadow</div> <div style="clear:both"></div> <div class="flecha drop-shadow"></div> <div class="flecha box-shadow"></div>
body{ background: #D5D5D5; font-family: museo-300; font-size: 1.5rem; text-align: center; } .tri, .flecha {display: inline-block;margin: 15px;} .tri { text-align: left; width: 250px; height: 100px; background: #fff; position: relative; padding: 15px; box-sizing: border-box; } .tri:after { content:''; height: 0; width: 0; border-width: 20px 20px 20px 0; border-style: solid; border-color: rgba(255,255,255,0) rgba(255,255,255,1) rgba(255,255,255,0) rgba(255,255,255,0); position: absolute; top: 15px; left: -20px; } .flecha { position: relative; margin: -20px 110px 0; width: 0; height: 0; border-top: 90px solid transparent; border-right: 90px solid #FFC000; -webkit-transform: rotate(10deg); -moz-transform: rotate(10deg); -ms-transform: rotate(10deg); -o-transform: rotate(10deg); } .flecha:after { content: ""; position: absolute; border: 0 solid transparent; border-top: 30px solid #FFC000; border-radius: 200px 0 0 0; top: -119px; left: -98px; width: 120px; height: 120px; -webkit-transform: rotate(45deg); -moz-transform: rotate(45deg); -ms-transform: rotate(45deg); -o-transform: rotate(45deg); } .drop-shadow { -webkit-filter: drop-shadow(0px 0px 10px rgba(0,0,0,.5)); -moz-filter: drop-shadow(0px 0px 10px rgba(0,0,0,.5)); -ms-filter: drop-shadow(0px 0px 10px rgba(0,0,0,.5)); -o-filter: drop-shadow(0px 0px 10px rgba(0,0,0,.5)); filter: drop-shadow(0px 0px 10px rgba(0,0,0,.5)); } .box-shadow {box-shadow: 0px 0px 10px rgba(0,0,0,.5)}
url()
url(<url>)
The url()
function takes the location of an XML file that specifies an SVG filter, and may include an anchor to a specific filter element.
#svg-filter { display: none; } img { display: block; width: 90%; } img { -webkit-filter: url(#svg-blur); }
Animating Filters
Since Filters are animatable it can open the doors for a whole bunch of fun.
<button>Click to Animate</button> <img src="image.jpg" alt="ukulele" />
button { position: absolute; top: 10px; right: 10px; z-index: 1; } img { display: block; width: 100%; } img.animated { -webkit-animation: filter-animation 5s infinite; } @-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); } }
function activateFilterAnimation() { $('img').toggleClass('animated'); } $('button').on('click', activateFilterAnimation);
Notes
You may combine any number of functions to manipulate the rendering, but
order still matters (i.e. sepia()
before grayscale()
otherwise using grayscale()
beforesepia()
will result in only grayscale as the output).
A computed value of other than “none” results in the creation of a stacking context the same way that CSS opacity does. The filter property has no effect on the geometry of the target element’s box model, even though filters can cause painting outside of an element’s border box. Any parts of the target element are affected by filter functions. This includes any content, background, borders, text decoration, outline and visible scrolling mechanism of the element to which the filter is applied, and those of its descendants. Filters can also be applied to background images along with inline content.
The IE proprietary filter stuff
Also used the filter
property, like:
.half-opacity { -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)"; filter: alpha(opacity=50); }
Browser Support
Chrome | Safari | Firefox | Opera | IE | Android | iOS |
---|---|---|---|---|---|---|
31+ (-webkit-) | 7+ (-webkit-) | nope | 18+ (-webkit-) | nope | 4.4+ (-webkit-) | 6+ (-webkit-) |
Leave a Reply