Wednesday, 6 February 2013

Filter in CSS


CSS FILTER:
                The CSS filter property provides for effects like blurring 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.
·         Initial value:          none
·         Applies to:  graphics and container elements
·         Inherited :   no
·         Media:                  visual
·         Computed valueas specified
·         Animatable: yes
Syntax
filter: <filter-function> [<filter-function>]* | none
Examples
Examples of using the predefined functions are shown below. See each function for a specific example.
.mydiv { filter: grayscale(50%) }
/* gray all images by 50% and blur by 10px */
img {
  filter: grayscale(0.5) blur(10px);
}
Examples of using the URL function with an SVG resource are shown below.
.target { filter: url(#c1); }

.mydiv { filter: url(commonfilters.xml#large-blur) }
Functions
To use the CSS filter property, you specify a value for one of the following functions. 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).
blur(radius)
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.
filter: blur(5px)
<svg height="0" xmlns="http://www.w3.org/2000/svg">
  <filter id="svgBlur" x="-5%" y="-5%" width="110%" height="110%">
    <feGaussianBlur in="SourceGraphic" stdDeviation="5"/>
  </filter>
</svg>

brightness(amount)
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.
filter: brightness(0.5)


contrast(amount)
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. 
filter: contrast(200%)

drop-shadow(<shadow>)
Applies a drop shadow effect to the input image. The function accepts a parameter of type <shadow> (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. 
<offset-x> <offset-y> (required)          
This are two <length> values to set the shadow offset. <offset-x> specifies the horizontal distance. <offset-y> specifies the vertical distance. If both values are 0, the shadow is placed behind the element (and may generate a blur effect if <blur-radius> and/or <spread-radius> is set).
<blur-radius> (optional)
This is a third <length> value. The larger this value, the bigger the blur, so the shadow becomes bigger and lighter.
<spread-radius> (optional)
This is a fourth <length> value. Positive values will cause the shadow to expand and grow bigger, negative values will cause the shadow to shrink. 
<color> (optional)
          It provides the color.

grayscale(amount)
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. 
filter: grayscale(100%)




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.
filter: hue-rotate(90deg)

invert(amount)
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. 
filter: invert(100%)
opacity(amount)
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. If the ‘amount’ parameter is missing, a value of 100% is used. This function is similar to the more established opacity property.
filter: opacity(50%)

saturate(amount)
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.  If the ‘amount’ parameter is missing, a value of 100% is used. 
filter: saturate(3%)

sepia(amount)
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.
filter: sepia(100%)


Combining functions
You may combine any number of functions to manipulate the rendering. The following example enhances the contrast and brightness of the image.
filter: contrast(175%) brightness(3%)