Add a hovering effect to images by applying the absolute positioning property

On my React website, I am utilizing Tailwind CSS and I am attempting to showcase an image that has interactive elements. I want certain parts of the image to increase in size slightly when hovered over. My approach to achieving this was to save each part of the image individually while maintaining a consistent frame size, like this:

[![Part 1][1]][1] [![Part 2][2]][2] [![Part 3][3]][3] [![Part 4][4]][4] [![Part 5][5]][5]

When combined, these parts form the complete image that can be transformed individually:

[![Complete Image][6]][6]

I was successful in recreating the image using this method, but I am facing several issues:

  1. Regardless of which part of the image I hover over, the same part always increases in size, while the others remain static.
  2. The image overlaps with the footer.

Here is my current code:

import React from "react";
import One from "../assets/1.png";
import Two from "../assets/2.png";
import Three from "../assets/3.png";
import Four from "../assets/4.png";
import Five from "../assets/5.png";

export const Image = () => {
  return (
    <div className="relative">
      <img
        className="absolute hover:scale-105 duration-300"
        src={Four}
        alt="/"
      />
      <img
        className="absolute hover:scale-105 duration-300"
        src={Five}
        alt="/"
      />
      <img
        className="absolute hover:scale-105 duration-300"
        src={Three}
        alt="/"
      />
      <img
        className=" absolute hover:scale-105 duration-300"
        src={One}
        alt="/"
      />
      <img
        className="absolute hover:scale-105 duration-300"
        src={Two}
        alt="/"
      />
    </div>
  );
};

export default Image;

Footer:

  
const Footer = () => {
  return (
    <div className="max-w-[1240px] mx-auto py-16 px-4 grid lg:grid-cols-3 gap-8 text-gray-300">
    [...]
   
    </div>
  );
};
export default Footer;

How can I resolve these issues? Additionally, is there a more efficient way to achieve the desired effect?

  1. https://i.sstatic.net/YYsDp.png
  2. https://i.sstatic.net/O9lu9.png
  3. https://i.sstatic.net/inRdM.png
  4. https://i.sstatic.net/IREpR.png
  5. https://i.sstatic.net/FuZyA.png
  6. https://i.sstatic.net/KFbKA.png

Answer №1

import React, { useState } from "react";
import One from "../assets/1.png";
import Two from "../assets/2.png";
import Three from "../assets/3.png";
import Four from "../assets/4.png";
import Five from "../assets/5.png";

export const Image = () => {
  const [isHovered, setIsHovered] = useState(false);

  const handleHover = () => {
    setIsHovered(true);
  };

  const handleMouseLeave = () => {
    setIsHovered(false);
  };

  return (
    <div
      className={`relative ${isHovered ? "hover:scale-105 duration-300" : ""}`}
      onMouseEnter={handleHover}
      onMouseLeave={handleMouseLeave}
    >
      <img className="absolute" src={Four} alt="/" />
      <img className="absolute" src={Five} alt="/" />
      <img className="absolute" src={Three} alt="/" />
      <img className="absolute" src={One} alt="/" />
      <img className="absolute" src={Two} alt="/" />
    </div>
  );
};

export default Image;

the :hover pseudo-class in CSS is not supported on parent or ancestor elements. It can only be applied to the element being hovered or its child elements.

By using this code, you can manually control the hover effect.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing the super method within a sails.js controller

Is there a way to call the default parent method of a redefined standard method in a controller created in sails.js? module.exports = { create: function(req, res) { //test some parameters if (condition) { //call regul ...

Learn how to automatically populate input fields based on the values entered in previous fields. These dynamic fields are generated based on the user

In the following code snippet, fields are dynamically created using jQuery... For more visual explanation, refer to this image: The goal is to calculate the grand total based on previous inputs and display the result when an onclick() event occurs. < ...

What's the most efficient way to iterate through this Array and display its contents in HTML?

I'm struggling to sort a simple array and I think the issue might be related to the time format. I'm not sure how to reference it or how I can properly sort the time in this array format for future sorting. //function defined to input values ...

Decrease the construction duration within a sprawling Angular 8 project

It takes nearly 10-15 minutes to compile the project in production mode, resulting in a dist folder size of 32MB Here are the production options I am currently using:- "production": { "optimization": true, "outputHashing": "all", ...

The Bootstrap navbar-fixed-top is failing to function properly within the confines of my "content" div

My goal is to utilize the bootstrap navbar-fixed-top within my id="content" div. However, when I apply the navbar-fixed-top class like this: <nav class="navbar navbar-default navbar-fixed-top navbar-shadow"> The navbar ends up on top of everything ...

Whenever I execute the 'ng serve' command, I encounter an issue with ineffective mark-compacts close to the heap limit, resulting in an allocation failure and a JavaScript

I'm currently using Angular 9 and Node.js 12. When I input ng serve, I encounter the following problem: C:\Users\homz\my-app>ng serve 93% after chunk asset optimization SourceMapDevToolPlugin vendor.js generate SourceMap <--- ...

Guide to integrating animations into a React JS array sorting program

I am currently working on a project that utilizes react js to visually sort an array. I have successfully implemented the sorting algorithm, and the website now displays the correctly sorted array. However, I would like to incorporate an animation to show ...

Keep the link underlined until a different button is pressed

I need help with a webpage that displays a list of partners organized by categories. When clicking on sidebar categories, the link remains underlined. However, if you click anywhere else on the page, the link becomes inactive. CSS: button:hover { t ...

Attempting to implement usedispatch hook in combination with userefs, however, encountering issues with functionality

I've been exploring the Redux useDispatch hook lately. I created a simple app for taking notes in a todo list format. However, I am facing an issue with useDispatch as it's not working for me and I keep encountering this error: Module build faile ...

When incorporating Vue as an npm package, a Vue 3 app may inadvertently clear the mounted element upon initialization

I have a straightforward Vue 3 application that is working perfectly fine when I include Vue as a script, as shown in the code snippet below. However, I need to integrate it with webpack by including it as an npm package. When I do this, the app loads but ...

When I try to insert new data into a MongoDB collection during an update operation, the existing data is being overwritten instead

While updating data, I have a requirement to also add new data. Specifically, I need to append data to an existing object in a for loop that stores data in a database. However, when attempting this, the data is being updated instead of added to the current ...

Unable to successfully interact with Paypal button using selenium automation

I'm facing an issue with Selenium where I am unable to click on the PayPal button. https://i.sstatic.net/Xx62H.png https://i.sstatic.net/lZg88.png Despite trying various methods recommended in the Selenium documentation, the button remains unrespon ...

Protractor unable to locate elements using by.repeater

What is the best method for targeting this repeater with Protractor? <a ng-repeat="item in filteredItems = (items | filter:'abc')">{{item}}</a> ...

What is the best way to transform a UTC/GMT date and time into CST in Javascript? (CST specifically, not based on

Dealing with a tricky situation where backend data is always stored in UTC time while our front-end data is in CST. Unfortunately, I don't have access to the system handling this 'black box' conversion. Trying to replicate this setup in our ...

Send the 'key' parameter to the React component

Attempting to pass a parameter named "key" to a react component is resulting in it not functioning properly. However, when I use "keyy" instead of "key," it works as intended. It appears that using "key" as a parameter name may be causing issues due to it ...

What is the reason that em takes precedence over p for font-family?

Take a look at this CSS snippet: * { font-family: Gill Sans, Verdana, Arial, Helvetica, Sans-Serif} p { font-family: Courier; } Now let's consider this HTML markup: <p>Hi there. So <em>very</em> pleased to make your acquaintance. ...

removing a specific cookie from the browser's cookies using jQuery

This block of code displays a URL along with a delete image for removing a cookie. The add and display functions are working fine, but I'm stuck on how to implement the delete function. function backLinks(){ var pathname = window.location; va ...

Is it feasible to transmit an image file to PHP using AJAX?

I'm attempting to utilize AJAX to send an image file along with other text information. However, I am encountering difficulties with the file part and am struggling to comprehend how to resolve it. Below is the JavaScript code snippet: //The user ...

Creating a custom route in Node.js using Express for posting content and adding it to a specific user's page

I am currently working on a node.js express project where I am developing a health app for a trainer to manage his clients. The main functionality of the app includes allowing the trainer to access individual client profiles and view their exercise list by ...

Using React to securely display PDF documents retrieved from an authenticated backend server

I am facing a challenge with my React Frontend and Django Backend setup. The issue arises when trying to incorporate a view for the PDF document fetched from the backend in the frontend. I attempted to use iframe and object HTML Tags, but encountered authe ...