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
    CSS Animations Between Media Queries

    CSS animations are right up there with sliced bread. CSS animations are efficient because they can be hardware accelerated, they require no JavaScript overhead, and they are composed of very little CSS code. Quite often we add CSS transforms to elements via CSS during...

  • By
    5 Ways that CSS and JavaScript Interact That You May Not Know About

    CSS and JavaScript:  the lines seemingly get blurred by each browser release.  They have always done a very different job but in the end they are both front-end technologies so they need do need to work closely.  We have our .js files and our .css, but...

Incredible Demos

  • By
    MooTools Documentation Search Favelet

    I'm going to share something with you that will blow your mind: I don't have the MooTools documentation memorized. I just don't. I visit the MooTools docs frequently to figure out the order of parameters of More classes and how best to use...

  • By
    MooTools: Set Style Per Media

    I'd bet one of the most used MooTools methods is the setStyle() method, which allows you to set CSS style declarations for an element. One of the limitations of MooTools' setStyle() method is that it sets the specific style for all medias.

Discussion

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