CSS Fade
Web developers have been using fade ins and fade outs (basic opacity animation) on their website for years, but the effect needed to be accomplished using JavaScript because CSS didn't have the capability to complete animations. The tides have turned: CSS fading is now possible thanks to CSS transitions. Let's take a look basic CSS fading animations!
CSS FadeIn and FadeOut
The principle behind fading is animating opacity, so the transition will address just that property:
/* fades in upon hover */ .fadeIn { opacity: 0; } .fadeIn:hover { opacity: 1; }
The example above illustrates a CSS fade in, while the example below illustrates a fade out:
/* fades in upon hover */ .fadeIn { opacity: 1; } .fadeIn:hover { opacity: 0; }
The examples above illustrate CSS fades during hover states, but what if you want to fade an element in or out without relying on use interactions. In this case, CSS animations are the better call:
/* basic fadein */ @keyframes fadeIn { 0% { opacity: 0 } 100% { opacity: 1 } } .fadeIn { animation-name: fadeIn; opacity: 1; }
Add the fadeIn class to the element you'd like to fade in (via JavaScript) and the job is done! A fade out would simply be the inverse of the code above.
CSS fade animations are child's play these days. CSS is has advanced quickly and there's no need to use JavaScript these days. Use CSS transitions and fading is easy!