Responsive CSS Background Image Fade
11/15/12
I figured out a way to fade out background images with only CSS today and I thought I’d share. I guess I should clarify to begin with and say that this doesn’t technically fade out the image. It simply hides the image gradually with a transparent to solid gradient. Because of this, elements below the container with the background image won’t show through. But, if you have solid colors behind it, then the overall effect is very similar and very useful.
This little tutorial will use CSS multiple backgrounds, CSS gradients, and CSS transitions, and RGBA colors. Yes, lots and lots of awesome CSS.
The Demo
First, let’s see the DEMO. The demo will only work in webkit browsers because I’m feeling lazy at the moment and don’t want to deal with adding all the browser prefixes. It will work in all modern browsers though if you swap out -webkit in the code for the other prefixes.
The Code
HTML:
<;div class="one">; This div has a background image that gets faded out on hover. <;/div>; <;div class="two">; This div has a background image that slides sideways and gets faded out on hover. <;/div>; <;div class="three">; This div has a background image that slides sideways, down, and gets faded out on hover. <;/div>;
CSS:
div {
background: -webkit-linear-gradient(right, rgba(255,255,255,0) 0%, rgba(255,255,255,0) 25%, rgba(255,255,255,1) 75%, rgba(255,255,255,1) 100%), url('overpass.jpg');
background-size: 400% 100%, 150% auto;
background-position: 100% 0, right top;
width: 400px;
height: 200px;
-webkit-transition: background-position .3s;
}
div.one:hover {
background-position: 0% 0, right top;
}
div.two:hover {
background-position: 0% 0, 0% 0%;
}
div.three:hover {
background-position: 0% 0, 0% 100%;
}
The Explanation
So what’s going on here? What I’ve done is set two background images. One is the photo you see before the fade. The second is a linear gradient that uses the alpha channel in the rgba values to go from a transparent white to a solid white. This sits on top of the photo so that it can mask the image. I then animate the gradient on hover to slide over the image which hides or shows it.

The real magic happens with the background size property. I am dividing the gradient into three section (the middle gradient section accounts for two parts in the list and diagram). 1.Completely Transparent (25% of gradient) 2.Transparent to Solid (25% of gradient) 3.Transparent to Solid (25% of gradient) 4.Completely Solid White (25% of gradient). I then set the width of the gradient with the background-size property to 400% because I am using 4 parts, each of which need to cover 100% of the image. This forces the transparent and solid portions of the gradient to completely cover 100% of the div area, leaving the middle transition area to fade between the two ends. The beauty of using percentages is that the width of the div container can be changed on the fly and the fade effect will always work.
I’m sure I gave a less than clear explanation so if you have any questions, ask away! Enjoy.
P.S. Before I even finished writing this, I’ve already made a few changes to the demo. The core concept is still the same even though there might be some slight stylistic differences.