CSS Fade

By  on  

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!

Recent Features

  • By
    Responsive and Infinitely Scalable JS Animations

    Back in late 2012 it was not easy to find open source projects using requestAnimationFrame() - this is the hook that allows Javascript code to synchronize with a web browser's native paint loop. Animations using this method can run at 60 fps and deliver fantastic...

  • By
    9 Mind-Blowing WebGL Demos

    As much as developers now loathe Flash, we're still playing a bit of catch up to natively duplicate the animation capabilities that Adobe's old technology provided us.  Of course we have canvas, an awesome technology, one which I highlighted 9 mind-blowing demos.  Another technology available...

Incredible Demos

Discussion

    Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!