DEV Community

Cover image for CSS Slide out animation for Image Gallery
Alteca
Alteca

Posted on • Edited on

CSS Slide out animation for Image Gallery

You likely have an image gallery or a blog post thumbnail gallery on your website, if so, this article will definitely help you bring your gallery to life and make it more interactive. To discover how to do this, read on.

It's actually very simple and you can do it with about 20 lines of code in total, but the result is awesome. Sliding animated images are much more appealing than static images

First we need to make the gallery itself in HTML

<div id="gallery">

<!-- this is where your images go -->
<div>
Enter fullscreen mode Exit fullscreen mode

Note that this code will not show anything because the src attribute needs to be added.
Additionally add the alt attribute so that if the image doesn't load you will still have some text in its place. Now so that your gallery isn't all stacked vertically let's make it flex with CSS.
Firstly create CSS file and copy or write this code inside it

#gallery{
display: flex;
}
#gallery img {
margin: 3rem 1.5rem;
border-radius: 1rem;
max-width: 40rem;

}
Enter fullscreen mode Exit fullscreen mode

Now we have a flexed image gallery, but it sadly doesn't do anything. I added a border-radius property because I don't like completely square images, but if that's okay with you, you can delete it.
In the next step we will make the images slide upwards when you hover over them with css transition, so hang on.
Firstly add this code to the #gallery img selector:

transition: all 400ms ease-in-out;
Enter fullscreen mode Exit fullscreen mode

Now we will need to make the actual transition using this code:

#gallery img:hover{
transform: translateY(-1.2)
}
Enter fullscreen mode Exit fullscreen mode

Look at your image gallery, it looks awesome, and even has a cool animation. You can be proud of yourself now.

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay