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

CSS

How to create a slide in image on div hover

On a project I got a bunch of divs which contain some text and a button. On user hover I'd like an image to slide in and remove text and buttons from within the divs, what would be the best way of achieving this?

1 Answer

Something like this maybe?

<html>
<head>
<style>

.item-wrapper {
    position: relative;
    width: 200px;
    height: 100px;
    margin: 20px auto;
    border: 1px solid black;
    border-radius: 10px;
}

.item-wrapper img {
    position: absolute;
    top: 0;
    transform: translateX(-2000px);
    border-radius: 10px;
    transition: transform .5s;
}

.item-wrapper:hover img {
    transform: translateX(0);
}

p {
    text-align: center;
}

button {
    width: 170px;
    margin: 10px;
}

</style>
</head>
<body>
    <div class="item-wrapper">
        <div class="text-and-button">
            <p>Some text</p>
            <button>Button</button>
        </div>
        <img src="http://lorempixel.com/200/100/" alt="">
    </div>
    <div class="item-wrapper">
        <div class="text-and-button">
            <p>Some text</p>
            <button>Button</button>
        </div>
        <img src="http://lorempixel.com/200/100/" alt="">
    </div>    
    <div class="item-wrapper">
        <div class="text-and-button">
            <p>Some text</p>
            <button>Button</button>
        </div>
        <img src="http://lorempixel.com/200/100/" alt="">
    </div>
</body>
</html>

Yes pretty much :D