image adjustment with css filter effects

6
2/10/2015 Image Adjustment with CSS Filter Effects data:text/html;charset=utf8,%3Ch1%20style%3D%22margin%3A%200px%3B%20padding%3A%200px%3B%20border%3A%200px%3B%20fontweight… 1/6 Image Adjustment With CSS Filter Effects By Thoriq Firdaus. Filed in Coding, Web Design Adjusting image Brightness and Contrast, or turning image into Grayscale or Sephia is a common feature you may find in image editing application , like Photoshop . But, it is now possible to add the same effects to web images using CSS. This capability comes from the Filter Effects which actually is still in the Working Draft stage. However, the Webkit browser seems to be a step a head in implementing this feature. So, let’s give it a try and we will use this image from Mehdi Kh to demonstrating the effects. The Effects Applying the effects is very easy. Take a look at the snippet below, for turning images into grayscale; 01. img { 02. ‐webkit‐filter: grayscale(100%); 03. }

Upload: vedran-vukelic

Post on 21-Nov-2015

5 views

Category:

Documents


0 download

DESCRIPTION

Image Adjustment With CSS Filter Effects

TRANSCRIPT

  • 2/10/2015 ImageAdjustmentwithCSSFilterEffects

    data:text/htmlcharset=utf8,%3Ch1%20style%3D%22margin%3A%200px%3B%20padding%3A%200px%3B%20border%3A%200px%3B%20fontweight 1/6

    ImageAdjustmentWithCSSFilterEffectsByThoriqFirdaus.FiledinCoding,WebDesign

    AdjustingimageBrightnessandContrast,orturningimageintoGrayscaleorSephiaisacommon

    featureyoumayfindinimageeditingapplication,likePhotoshop.But,itisnowpossibletoaddthe

    sameeffectstowebimagesusingCSS.

    ThiscapabilitycomesfromtheFilterEffectswhichactuallyisstillintheWorkingDraftstage.However,

    theWebkitbrowserseemstobeastepaheadinimplementingthisfeature.

    So,letsgiveitatryandwewillusethisimagefromMehdiKhtodemonstratingtheeffects.

    TheEffectsApplyingtheeffectsisveryeasy.Takealookatthesnippetbelow,forturningimagesintograyscale

    01. img{

    02. webkitfilter:grayscale(100%);

    03. }

  • 2/10/2015 ImageAdjustmentwithCSSFilterEffects

    data:text/htmlcharset=utf8,%3Ch1%20style%3D%22margin%3A%200px%3B%20padding%3A%200px%3B%20border%3A%200px%3B%20fontweight 2/6

    andthisisforsepiaalaInstagram.

    01. img{

    02. webkitfilter:sepia(100%);

    03. }

    Boththesepiaandthegrayscalevaluesarestatedinpercentagewhere100%isthemaximumand

    applying0%willkeeptheimageunchange,butwhenthevalueisnotexplicitlyspecifiedthe100%will

    betakenasthedefault.

    Brighteningtheimageisalsopossible,andwecandoitbyusingthebrightnessfunction,asfollows

    01. img{

    02. webkitfilter:brightness(50%);

    03. }

  • 2/10/2015 ImageAdjustmentwithCSSFilterEffects

    data:text/htmlcharset=utf8,%3Ch1%20style%3D%22margin%3A%200px%3B%20padding%3A%200px%3B%20border%3A%200px%3B%20fontweight 3/6

    Thebrightnesseffectworkslikethecontrastandsepiaeffectabovewherethevalueof0%willkeepthe

    imageasitisandapplying100%willtotallybrightentheimage,whichwouldpossiblyshowonlya

    blankwhitepageinsteadofanimage.

    Thebrightnesseffectalsoallowsfornegativevalues,inwhichitwilldarkentheimage.

    01. img{

    02. webkitfilter:brightness(50%);

    03. }

    ..andwecanadjusttheimagecontrastthisway.

    01. img{

    02. webkitfilter:contrast(200%);

    03. }

  • 2/10/2015 ImageAdjustmentwithCSSFilterEffects

    data:text/htmlcharset=utf8,%3Ch1%20style%3D%22margin%3A%200px%3B%20padding%3A%200px%3B%20border%3A%200px%3B%20fontweight 4/6

    Thereisalittledifferenceonhowthevalueworksaswell,asyoucanseeintheabove,weset

    thecontrast()by200%,thisisbecausethevalueof100%isthestartingpointwheretheimagewill

    remainunchanged.Whenthevalueisbelow100%,letssay50%,theimagewillbecomeless

    contrasted.

    Furthermore,wecanalsocombinetheeffectinonedeclarationblock,suchasintheexamplebelow.We

    turntheimageintograyscaleandincreasethecontrastby50%atthesametime.

    01. img{

    02. webkitfilter:grayscale(100%)contrast(150%);

    03. }

    BycombiningthefilterwithCSS3transitionwecanmakeagracefulhovereffect.

    01. img:hover{

    02. webkitfilter:grayscale(0%);

    03. }

    04. img:hover{

    05. webkitfilter:sepia(0%);

  • 2/10/2015 ImageAdjustmentwithCSSFilterEffects

    data:text/htmlcharset=utf8,%3Ch1%20style%3D%22margin%3A%200px%3B%20padding%3A%200px%3B%20border%3A%200px%3B%20fontweight 5/6

    06. }

    07. img:hover{

    08. webkitfilter:brightness(0%);

    09. }

    10. img:hover{

    11. webkitfilter:contrast(100%);

    12. }

    Lastly,thereisonemoreeffectwecantrytheGaussianBlur,whichwillblurthetargetedelement.

    viewplain copytoclipboard print ?

    01. img:hover{

    02. webkitfilter:blur(5px);

    03. }

    LikeinPhotoshop,theblurvalueisstatedinpixelradiusandifthevalueisnotexplicitlystated,the

  • 2/10/2015 ImageAdjustmentwithCSSFilterEffects

    data:text/htmlcharset=utf8,%3Ch1%20style%3D%22margin%3A%200px%3B%20padding%3A%200px%3B%20border%3A%200px%3B%20fontweight 6/6

    FinalThoughtThereareactuallyalotmoreeffectsinthespec.suchashue-rotate,invertandsaturate,butI

    thinktheyarelessappliedinrealWebcases.Thus,thediscussionwaslimitedtojustfoureffects.

    And,despitethediscussionbeingappliedtoimagesinthistutorial,thepropertycanactuallyalsobe

    appliedtoanyelementintheDOM.

    Finally,youcanviewthelivedemofromtheselinksbelow.PleasetakenotethattheFilteriscurrently

    onlysupportedinChrome19andabove.

    DEMO DOWNLOADSOURCE