Smooth Image Zoom Transition in CSS

Smooth Image Zoom Transition in CSS
Code Snippet:Image hover effect
Author: The Coding Bug
Published: January 19, 2024
Last Updated: January 22, 2024
Downloads: 1,320
License: MIT
Edit Code online: View on CodePen
Read More

This code enables smooth image zoom transition in CSS. It works by using the CSS transform property to smoothly zoom in on images when they are hovered over. It’s particularly useful for portfolio websites, product showcases, image galleries, and any other web pages where you want to add an engaging hover effect to images.

How to Create Smooth Image Zoom Transition In CSS

1. First, let’s set up the HTML structure for your images. You can place the following code inside the <body> tag of your HTML document. Replace the URL of the img tags with your own images on which you want to apply a smooth zoom transition.

<div class="display-flex">
 <div>
 <h2>Zoom in</h2>
<div class="zoom-in">
<img width="300" src="https://images.pexels.com/photos/1261728/pexels-photo-1261728.jpeg?auto=compress&cs=tinysrgb&w=600"/>
</div>
  </div>
  <div>
  <h2>Zoom Out</h2>
<div class="zoom-out">
<img width="300" src="https://images.pexels.com/photos/1261728/pexels-photo-1261728.jpeg?auto=compress&cs=tinysrgb&w=600"/>
</div>
    </div>
</div>

2. Next, let’s add the necessary CSS styles to create the zoom effect on hover. You can include the following CSS code in your <style> tag or an external CSS file.

@import url('https://fonts.googleapis.com/css2?family=Caprasimo&display=swap');
*{
  box-sizing:border-box;
  margin:0;
  padding:0;
}
body{
  height:100vh;
  align-items:center;
  justify-content:center;
  background:black !important;
  font-family: 'Caprasimo', cursive;
  color:white;
}
h2{
  text-align: center;
}
.display-flex{
  display:flex;
}
.zoom-in,
.zoom-out {
  overflow: hidden;
  padding: 0;
  width: 300px;
  margin: 10px;
}
.zoom-in img,
.zoom-out img {
  transition-duration: 2s;
  margin: 0 auto;
  display: block;
}
.zoom-in img:hover {
  transform: scale(1.1);
}
.zoom-out img {
  transform: scale(1.1);
}
.zoom-out img:hover {
  transform: scale(1);
}

You can customize this code by changing the image URLs, adjusting the image dimensions, or modifying the CSS styles to match your website’s design.

That’s it! You’ve successfully added smooth image zoom transitions to your website using CSS. Feel free to use this technique to make your web content more engaging. If you have any questions or suggestions, don’t hesitate to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X