Make background fade out/in
Asked Answered
S

3

27

Is there any way I can use CSS3 to fade in and out a solid white background of a <div>? the content should remain visible so just the background should fade.

Any ideas using css3 transitions/transforms?

thank you for your help.

Sweeps answered 1/10, 2011 at 20:36 Comment(0)
J
44

Sure, you can use the transition property directly on the background-color:

div {
   background-color: white;    

   /* transition the background-color over 1s with a linear animation */
   transition: background-color 1s linear;
}

/* the :hover that causes the background-color to change */
div:hover {
   background-color: transparent;      
}

Here's an example of a red background fading out on :hover.

Janiculum answered 1/10, 2011 at 20:41 Comment(6)
Didn't know you could actually animate the background color. I think that's a lot better than my solution :)Hoarsen
@Hoarsen Transitions are pretty excellent but I'm looking forward to when we can drop all the browser specific prefixes.Janiculum
you could use LESS / SASS and use the mixins there. At least makes it bearable if you're using lots of CSS3 properties.Hoarsen
@Hoarsen I haven't tried LESS/SASS yet, but yeah, the prefixes definitely make a good argument for them.Janiculum
thanks for the jsfiddle! here's a forked sample which fades out a background color after load jsfiddle.net/divzero/DdhwxChapfallen
@WilliamDenniss Thank you! You just saved me a couple of hours of fooling around!Nottinghamshire
F
3

You may find this useful for examples of image and background color fades: - http://nettuts.s3.amazonaws.com/581_cssTransitions/demos.html

However using CSS 3 to do the transition will limit the effect to browsers that don't support it.

Feininger answered 1/10, 2011 at 20:40 Comment(0)
H
0

You could have two divs in one container div. The first div contains the white background, the second one gets the content.

Then, use a CSS3 transform to animate the opacity of the first div to zero.

Hoarsen answered 1/10, 2011 at 20:42 Comment(1)
Thanks for the suggestion. I eventually set the first color inline on the div and the second color (ie. white) external as background-color: white !important; as well as the transition property.Muckraker

© 2022 - 2024 — McMap. All rights reserved.