Knockouttextisatechniquewherewordsareclippedoutofanelementandrevealthebackground.Inotherwords,youonlyseethebackgroundbecausethelettersareknockingoutholes.It’sappealingbecauseitopensuptypographicstylesthatwedon’tgetoutoftraditionalCSSproperties,likecolor.
Eventhough,inmostcases,eitherblackorwhiteisusedintheseblendmodestogetacleardistinctionbetweenthetextandthebackground,Ipreferusingadarkerorlightercolorinsteadtokeepthebackimageslightlyvisible,likethis:
Usingadarker(orlighter)coloralsocreatesanice“theme”goingonwiththeimageshownthroughthetext.
Themultiplyblendmodekeepsdarkercolorsdarkandthelightercolorsletthroughwhatever’sbehindthem:ablackportiononthetoplayerwillbefullyopaqueandwhitewillbefullytransparent.
Intheaboveexample,thewhitetextbecamecompletelysee-throughwhilethedarkercolorarounditletstheimagebehindbeseenonlyalittle,asthedarkershadesremainunaffected.
Thescreenblendmodereversestheroles:darkercolorscreatetranslucencewhilelightershadesremainlightandblockwhat’sbehind.
Thedarkenandlightenblendmodesaresimilartomultiplyandscreen,respectively,exceptthatdetailsarelostontheportionsofthebackimagethatcanbeseen.Ratherthanmixingtheshades,themodeschoosethedarkerorthelightershadeofthetwolayersthatareshown.
Seebelowthedifferenceinhowthefourmodeshaveblendedthecolors:
Usingablendmodeisthemostconvenientoptiontogetaknockouttexteffect,becauseitallowsustoapplyadditionalstylesthatothertechniquesmightnotallow.
Let’stakeacloserlookatthestyleswecanusetoenhancetheknockouteffect.
ShadowBlurAddingawhite/blackoralight/darktextshadowtothetextcreatesablurredeffect.Forexample,let’ssayIaddatext-shadowthat’swithalargeblurradiusvalue:
.text{text-shadow:009pxwhite;...}Nowtheedgesarelesscrispandgiveasortofcloudyeffect:
AnimationWecanevenmakethingsmovearoundalittle.Forexample,let’stakebuildonthetext-shadowideawelookedataboveandputsomemovementonittomakeitappearthatthetextisglowing:
TransitionTransitionisanotherpropertywecanapplytoourknockouttextandthatopensupevenmoreinterestingpossibilities,likeusingtext-indentonpseudo-classeslike:hover.
Here’showwecanusetransitiononpseudo-classestointroduceanewelementintotheknockouttext:
Thetransparenttextshowstheimagebehinditthat’salreadycuttotheshapeofthetext.Althoughthisisatrueknockouttextapproach—itliterallyremovesthetext’ssurroundingbackgroundonscreen—havingnobackgroundlefttobleedintoormoveoverleaveslittlespaceforothereffectslikeblur,ormovingtext,totakeplace.That’swheremix-blend-modehasanadvantage.
CSSMaskThefirsttechniquewelookedatemploysmasking,aconceptwhereshapesarecreatedonaforegroundlayerandusecolortodeterminehowmuchoftheshapeshowsthebackground.Theblackpartsoftheforegroundhide(or“mask”)andwhitepartsrevealthebackground,orvice-versa.Anygrayvaluebetweenblackandwhiteistreatedasvaryingdegreesofpartialtransparency.
CSSmaskworksthesameway:youstraightupdeclareanimagetobethemaskappliedoveranotherimageand,dependingonthetypeofthemask,wegetaportioncutout.AsofwritingthispostCSSmaskisfullysupportedinFirefoxonly.
Sincewearelookingspecificallyintoknockouttext,themaskneedstobemadefromtext.ThisisagreatuseforSVG
SVG’s
Blurring,Animation,andTransitionCSSmasksopenupthesameblurandanimationeffectswewereabletousewithmix-blend-mode.
Thatsameglowingtextfromweusedbeforeapplieshereaswell,thistimeapplieddirectlytothe
text{font:bolder12vw'AlfaSlabOne';animation:glow3sinfinite;}@keyframesglow{0%{text-shadow:0010pxwhite;}15%{text-shadow:2px2px10pxrgba(255,255,255,1),-2px-2px10pxrgba(255,255,255,1);}30%{text-shadow:2px2px4pxrgba(255,255,255,.7),-2px-2px4pxrgba(255,255,255,.7);}50%{text-shadow:20px20px50pxrgba(255,255,255,.5),-20px-20px50pxrgba(255,255,255,.5);}}However,unlikemix-blend-mode,notallthesamepropertiescanbeanimated.Forexample,text-indentwon’tworkhereandneitherwilltransform.It’struethatCSStransformscanbeappliedtoSVGelementsbutbecauseourmaskisactuallybeingusedasamaskinitstruestform,browsersmightnotapplythosetransformations.
WecanalwaysinjectatransformSVGattributeusingJavaScript,deliveringthosetransformationstotheelementsinsidethemask:
Intermsofresult,bothblendmodesandmaskgivesimilaroutput.Betweenbackground-clipandmix-blend-modevalues,it’llbethechoiceofthedesignthat’llleadtochoosingoneoveranother.Whatyoucanachievewithbackground-clipcanalsobedonebyblending,providedyou’reusingeitheronlyblackorwhitebackgroundmatchingthepage’sbody.
TLDR;knockouttextmethoduno:applyoneofthefourknockout-friendlyblendmodestothetoplayerofatext-imagestackandusedark/light(orblack/white)colorcombinationonthetextanditsenclosure.Methoddos:setbackground-cliptotextintheelementcarryingbothabackgroundimageandatransparenttext.Methodtres:useCSSmaskingonasolidforegroundwithanimagebehindanddictatethecutoutusinganSVGtextmask.