Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

Jennifer Hinkle
Jennifer Hinkle
8,365 Points

CSS transitions with img elements

Hello,

I'm using images as links and want to apply CSS transitions to them (giving the images the effect of changing color when hovered over).

html

<html>
<body>
    <a href="#"><img src="img.jpg"></a>
</body>
</html>

CSS

a {
    transition-duration: .4s;
}
a:hover {
    background: url("img2.jpg");
}

I know the above CSS doesn't work for this, but if it was text color it would work... any idea how to make it work with images as well?

Thanks for any advice! Jenn

1 Answer

Hi Jen, You could try adding the 2 images to your index.html, use absolute positioning to lay them on top of one another. Then fade them in out by changing opacity, using css transitions. For example:

<div class="container">
        <a href="#" class="top"><img src="img.jpg"></a>
    <a href="#" class="bottom"><img src="img2.jpg"></a>
</div>
.container {
    position: relative;
    margin: 0;
}

img {
    position: absolute;
    left: 0;
    opacity: 1;
    -webkit-transition: opacity 4s ease;
}


.bottom img:hover {
    opacity: 0;
}

Hope it helps! :)

EDIT: removed unecessary code