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
    Being a Dev Dad

    I get asked loads of questions every day but I'm always surprised that they're rarely questions about code or even tech -- many of the questions I get are more about non-dev stuff like what my office is like, what software I use, and oftentimes...

  • By
    7 Essential JavaScript Functions

    I remember the early days of JavaScript where you needed a simple function for just about everything because the browser vendors implemented features differently, and not just edge features, basic features, like addEventListener and attachEvent.  Times have changed but there are still a few functions each developer should...

Incredible Demos

  • By
    Introducing MooTools Templated

    One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating: new Element Madness The first way to create UI-driven...

  • By
    Duplicate the jQuery Homepage Tooltips

    The jQuery homepage has a pretty suave tooltip-like effect as seen below: The amount of jQuery required to duplicate this effect is next to nothing;  in fact, there's more CSS than there is jQuery code!  Let's explore how we can duplicate jQuery's tooltip effect. The HTML The overall...

Discussion

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