Hover Effect for 3D Images

I recently came across an interesting 3D Hover Image Effect that I wanted to implement - https://codepen.io/kw7oe/pen/mPeepv. After going through various tutorials and guides, I decided to try styling a component with Materials UI and apply CSS in a different way. Unlike the example link where the image is declared as a background in CSS, I opted to use the MUI Image tag instead. Unfortunately, it's not functioning as expected and I'm uncertain about the cause.

Just for context, I developed this in React TS.

import React from 'react';
import { Box, Typography } from '@mui/material';
import Image from 'mui-image';

// Rest of the JavaScript code here...

export { Hover };

Answer №1

To enhance the styling consistency in MUI, it is recommended to replace all instances of the style attribute with the sx prop as advocated by MUI guidelines. The use of the sx prop is preferred over directly using the style attribute: https://mui.com/system/the-sx-prop/

Additionally, maintaining the background url inside the <a/> tag is crucial for applying the 3D Hover Effect transformations effectively.

return (
  <Box sx={styles.container}>
    <Box sx={styles.row}>
      <Typography sx={styles.h1}>Image Hover Effect</Typography>
      <Typography sx={styles.h2}>3D Hover Effect</Typography>
    </Box>

    <Box sx={styles.row}>
      <Box sx={styles.tdimension}>
        <a href="#"></a>
      </Box>
      <Divider sx={{ padding: "1px" }} />
    </Box>
  </Box>
);

To maintain clarity and prevent loss of sub-styling, it is advisable to nest everything related to tdimension within a separate styling object inside your styles object. This approach ensures that all subtleties in styling are preserved when utilizing styles.tdimension.

...
  tdimension: {
    width: "300px",
    height: "300px",
    margin: "20px auto 40px auto",
    perspective: "1000px",
    a: {
      background:
        'url("https://mir-s3-cdn-cf.behance.net/project_modules/disp/e8346826957515.56361c2106f3f.png")',
      display: "block",
      width: "100%",
      height: "100%",
      backgroundSize: "cover",
      transformStyle: "preserve-3d",
      transform: "rotateX(70deg)",
      transition: "all 0.8s",
    },
    "&:hover a": {
      transform: "rotateX(20deg)",
    },
    "a:after": {
      content: '""',
      position: "absolute",
      left: 0,
      bottom: 0,
      width: "100%",
      height: "40px",
      background: "linear-gradient(rgba(0, 0, 0, 0.2), rgba(0, 0, 0, 0.1))",
      transform: "rotateX(90deg)",
      transformOrigin: "bottom",
    },
  },

By following these recommendations, your code structure will closely resemble the one on Codepen.

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

How is it possible for a TypeScript function to return something when its return type is void?

I find the book Learning JavaScript to be confusing. It delivers conflicting information regarding the use of void as a return type in functions. const foo = (s: string): void => { return 1; // ERROR } At first, it states that if a function has a re ...

Is there a way to optimize value caching using a customized version of useMemo that remains constant?

I have a complex computation function that could benefit from the useMemo hook. The issue is, I am certain that the arguments for this function will always remain constant. As far as I know, useMemo requires you to list all arguments in an array within its ...

Inline CSS not appearing correctly upon initial page load

Is your page rendering incorrectly on the first load but then correct after a refresh? It seems to be related to the "display:inline-block" style. Despite clearing cache before loading, the issue persists and you're unable to find a solution. First L ...

Interference caused by Material-UI icon affecting button functionality

Whenever I attempt to delete a post by clicking in the area surrounding the icon, everything works smoothly and the post is successfully removed. However, if I click directly on the (<DeleteOutlineOutlinedIcon />) icon, the deletion does not occur. D ...

How to Perfectly Center a Div using CSS in Next.js (slightly off-center)

Despite scouring numerous StackOverflow questions, I haven't been able to find a solution that fits my specific issue. It seems like there might be a CSS attribute preventing the div from centering properly. Could utilizing flexbox help resolve this? ...

Align the div to the left with text or a paragraph displayed on the right side

Check out my js fiddle below: https://jsfiddle.net/9yj63s3f/ Here is the CSS code snippet: .viddiv{ float:left; width:50%; } .vidholder{ position:relative;height:0;padding-bottom:50%; width:50%; float:left; } iframe{ position:abso ...

How to Use Jquery to Deactivate a Div with a Background Color

Currently, I have a div that contains several child divs. My goal is to disable the parent div and all of its children, showing them in gray color. I initially tried using the CSS property opacity, but unfortunately, the background color didn't change ...

What is the best way to center align cards using MUI?

I am facing an issue with my grid display using MUI where the cards are stuck to the left side of the page. Even after trying various properties like justify='center', mx: 'auto', and alignItems='center', the cards refuse to a ...

Assigning object properties from a request to every item in an observable array of objects using RxJS

Despite my efforts to search various resources like mergeMap and switchMap, I am unable to solve the problem I'm facing as I am still new to RxJs. While I would like to provide more details about my attempts in this post, I fear it may complicate my q ...

Prevent the footer from overlapping with the text when printing several pages

Is there a way to prevent text from overlapping the footer when printing a page containing both? I have tried using CSS and HTML to adjust the positioning of the footer, but it still overlaps with long blocks of text. Any suggestions on how to fix this i ...

The specified column `EventChart.åå` is not found within the existing database

I've been developing a dashboard application using Prisma, Next.js, and supabase. Recently, I encountered an issue when making a GET request. Prisma throws an error mentioning a column EventChart.åå, with a strange alphabet "åå" that I haven&apos ...

A step-by-step guide on creating a menubar location using Material UI

Recently, I delved into the world of Material UI to create a basic website structure and familiarize myself with the template code. However, I encountered an issue where certain items in the menu bar were not aligning correctly, unlike the example I was f ...

"Update your Chart.js to version 3.7.1 to eliminate the vertical scale displaying values on the left

Is there a way to disable the scale with additional marks from 0 to 45000 as shown in the screenshot? I've attempted various solutions, including updating chartjs to the latest version, but I'm specifically interested in addressing this issue in ...

`How to resolve `next dev` ESM import issues?`

Upon running next dev on my project, I encountered a series of errors. These errors stem from an internal library that serves as a dependency for other internal libraries: Module not found: ESM packages (@company/style-lib) need to be imported. Use 'i ...

Problem with prompt updating when the input value is modified in the text field

My application depends on the 'city' value to make an API call and fetch data. To achieve this, I have set up a form that utilizes a setState value for my city and a useEffect value in my main App.js file so that each time a new city is submitted ...

How can I adjust the spacing system in Material UI according to the screen width?

Recently, I've come across a component that looks like this: <Box px={3}> <Content /> </Box> While rendering this code on mobile, everything seems to work fine. However, the paddingX remains fixed at 24px (calculated using an 8 b ...

Is there a way to specifically import only variables and mixins from Sass stylesheets?

Using the Zurb Foundation 4 (S)CSS framework has presented a challenge with massively duplicated styles. Each file that I import 'foundation' into brings along all of Foundation's styles, resulting in redundant declarations for body, .row, . ...

Applying Vue Quill CSS to the initial Quill Editor component exclusively and tips for personalizing the toolbar

I have encountered an odd CSS issue while using the vue-quill package in my project. Each comment has its own quill editor for replying, and I temporarily set the isOpen prop to true for debugging purposes. This results in a quill editor being displayed un ...

Conceal a div once the user scrolls to a certain position using vanilla JavaScript

As a beginner in the field of web development, I am currently working on creating a blog website. Code Function - One of the challenges I'm facing is implementing a floating pagination bar that needs to be hidden when a visitor scrolls near the foote ...

Issue with displaying validation messages in React hooks when using a basic React validator

Currently, I am encountering an issue with form validation in React. The validation itself is functioning correctly, however, the validation message is not being displayed on the user interface. In the console, the `shownmessage` attribute indicates true w ...