Site icon Treehouse Blog

CSS Hover Effects for Photos

Screenshot of hover effects added to a portfolio website.

One of the most popular courses on Treehouse is How to Make a Website, which walks through the process of building a complete portfolio site from scratch. However, even after completing the course, the learning is only just beginning. Thousands of students have customized the example project and made it their own, for everything from displaying family photos to showcasing their favorite animals.

The course teaches how to build a basic site, but there’s plenty of enhancements that could be added. As an example, here’s how to add CSS hover effects to the photos in the image gallery.

Before: A Simple Website

Here’s what the image gallery looks like before adding the hover effects.

View Demo on CodePen

The page is responsive and features an image gallery with five portfolio pieces. The gallery itself is composed of an unordered list, and each list item contains a link with a nested image inside. By itself, this is pretty decent, but it might be fun to add a nice hover effect. Keep in mind that the hover state generally doesn’t work on mobile devices, but it can’t hurt to add it for desktop users, as long as it’s just an enhancement and not an essential site function.

After: Adding Hover Effects

Here’s the image gallery looks like with the hover effects added. Check out the site in a new window and try hovering over the images.

View Demo on CodePen

This effect is created by adding some simple CSS. If you already have a pre-existing website, you modify the selectors to make this code work with your own custom markup. Here’s the code, followed by a quick breakdown of how it works:


#gallery li {
  perspective: 250px;
}

#gallery a img {
  transition: 100ms;
  transform: translateZ(0px);
  box-shadow: 0px 0px 0px rgba(0,0,0,0);
}

#gallery a img:hover {
  transform: translateZ(25px) rotate(3deg);
  box-shadow: 0px 0px 10px rgba(0,0,0,0.8);
}

Share Your Own Customizations

If you’ve taken the How to Make a Website course and made your own customizations, I’d love to hear about them in the comments!

Exit mobile version