change image on hover | pure css effect.

In the world of web development, adding interactivity to your website can greatly enhance user experience. One way to achieve this is by implementing image hover effects. In this blog post, we’ll delve into a simple yet effective technique to change images on hover using HTML and CSS.

Understanding the Code

Let’s break down the provided code snippet to understand how it works.

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Image Hover Effect</title>
<link rel="stylesheet" href="styles.css">

<h2>Change Image on Hover</h2>
<div class="image-container">
  <img src="image1.jpg"  >
  <img src="image2.jpg"  >

In the HTML part, we have a basic structure of an HTML document. Inside the body, we have a div element with the class image-container containing two img elements with different image sources.

CSS Styling

.image-container {
  max-width: 300px;  
  max-height: 200px;  
  position: relative;
.image-container img{
  width: 100%;
  position: absolute;
  transition: opacity 0.9s;

.image-container > img:nth-child(1) {
 z-index: 1;
.image-container > img:nth-child(1):hover{
 z-index: 0;

.image-container > img:nth-child(2) {
   z-index: 0;
   opacity: 0;
.image-container > img:nth-child(2):hover {
z-index: 1;
 opacity: 1;

In the CSS section, we define styles for our HTML elements. Let’s break it down:

.image-container: Sets the maximum width and height for the container and positions it relatively.
.image-container img: Styles the images within the container to take up 100% of the width and be positioned absolutely, enabling layering.
.image-container > img:nth-child(1): Targets the first image and sets its z-index to 1, bringing it to the front.
.image-container > img:nth-child(1):hover: When hovering over the first image, it changes the z-index to 0, pushing it behind the second image.
.image-container > img:nth-child(2): Targets the second image and sets its initial opacity to 0, making it invisible.
.image-container > img:nth-child(2):hover: When hovering over the second image, it changes the z-index to 1 and opacity to 1, making it visible.

How It Works

The magic happens in the CSS hover states. When you hover over the first image, it goes behind the second image due to the change in z-index. Similarly, when hovering over the second image, it becomes visible by changing its opacity and z-index.


Implementing image hover effects adds a touch of interactivity to your website, making it more engaging for users. By understanding the simple HTML and CSS techniques demonstrated in this post, you can easily incorporate image hover effects into your own projects. Experiment with different styles and transitions to create unique effects that enhance your website’s visual appeal.

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments

Let's Start Your Social Media Success Story Today!

At Ourgrid, we’re committed to helping you achieve your social media marketing goals. Whether you’re looking to boost brand awareness, drive engagement, or increase conversions, our team of experts is here to guide you.

Frequently Asked Questions

Have Questions About Our Social Media Marketing Services? We Have Answers!
What services does your web design agency offer?
  • Our agency specializes in a wide range of services including website design, development, UX/UI design, e-commerce solutions, mobile responsiveness optimization, SEO, and digital marketing strategies tailored to enhance online presence and engagement.
How do you approach the design process for a new website?

Our design process begins with comprehensive research and understanding of your business goals and target audience. We then proceed to wireframing, prototyping, and iterative design, ensuring constant communication and feedback loops with our clients to achieve the desired outcome effectively and efficiently.

What sets your agency apart from others in the industry?

Our agency stands out due to our commitment to delivering high-quality, customized solutions that are not only visually appealing but also functional and user-friendly. We prioritize collaboration with our clients throughout the entire project lifecycle, ensuring their vision is realized while also incorporating industry best practices and the latest design trends.

Can you provide examples of successful projects your agency has completed?

Certainly! We have a portfolio showcasing a diverse range of projects across various industries, each demonstrating our expertise in creating unique and impactful web experiences. From small businesses to large enterprises, our projects consistently deliver results that exceed expectations and drive measurable success.

How do you ensure websites are optimized for search engines (SEO)?

SEO optimization is an integral part of our design process. We employ best practices in website structuring, content creation, keyword research, and metadata optimization to ensure maximum visibility and ranking on search engine results pages. Additionally, we provide ongoing SEO maintenance and analytics tracking to continuously improve performance.

What is your approach to responsive design?

We prioritize responsive design to ensure that websites are accessible and perform well across all devices and screen sizes. Our approach involves utilizing flexible grids and layouts, fluid images, and CSS media queries to adapt the design seamlessly to different viewing environments, enhancing user experience and engagement.

How do you handle website maintenance and updates post-launch?

We offer comprehensive maintenance packages tailored to meet the specific needs of our clients. This includes regular backups, security updates, content updates, performance monitoring, and ongoing support to address any issues or enhancements required post-launch, ensuring the website remains secure, up-to-date, and optimized for continued success.

What is your process for measuring the success of a website after launch?

We utilize a combination of analytics tools and key performance indicators (KPIs) to assess the effectiveness and impact of the website post-launch. This includes tracking metrics such as traffic, engagement, conversion rates, and user behavior to identify areas for improvement and optimization, allowing us to continuously refine and enhance the website’s performance over time.

Empower Your Brand Captivating Audiences through marketing.

Copyright: © 2024 WordPress. All Rights Reserved.

Would love your thoughts, please comment.x
Verified by MonsterInsights