Troubleshooting Issue with Absolute Positioning and Hover Effect on Material UI Button

I have a div containing a hidden button and an inner div filled with graphs and text. Occasionally, I need to blur the inner div and make the button float on top in the center of the blurred section, similar to the layout seen on subscription prompts found on sites like Medium or news websites (simplified for this example). Currently, I am achieving this by using absolute positioning for the button. However, I'm encountering issues with hover functionality on the button - it doesn't change background color or cursor when hovered over. My project involves material UI and react. Below is a code snippet:

const useStyles = makeStyles((theme) => ({
    blur: {
        filter: "blur(7px)",
    },
    relativePos: {
        position: "relative",
    },
    absolutePos: {
        position: "absolute",
        top: "50%",
        left: "50%",
    },
    floatingBtn: {
        "&:hover": {
            cursor: "pointer",
            backgroundColor: "red",
        },
    },
});

// some other stuff

<div className={classes.relativePos}>
    <Button
        variant="contained"
        color="primary"
        className={`${classes.absolutePos} ${classes.floatingBtn}`}
    >
    Button Text
    </Button>
    <div className={classes.blur}>
        {/* Blurred Inner Div Stuff */}
    </div>
</div>

I am open to suggestions: either improving the current implementation for proper functionality OR considering a different, more modern approach without relying on absolute positioning.

Answer №1

Here are two possible solutions to the issue:

  1. Apply zIndex on the button with a value greater than that of the blurred inner div
  2. Position the button beneath the blurred inner div

I would recommend opting for the second approach as it eliminates the need to determine the zIndex of other elements.

const useStyles = makeStyles((theme) => ({
  blur: {
    filter: "blur(7px)"
  },
  relativePos: {
    position: "relative"
  },
  absolutePos: {
    position: "absolute",
    top: "50%",
    left: "50%"
    // zIndex: 1000  <- Add The zIndex here if you want 1st approach
  },
  floatingBtn: {
    "&:hover": {
      cursor: "pointer",
      backgroundColor: "red"
    }
  },
  // This is temp button to just toggle the absolute button
  tempButton: { margin: "30px 0" }
}));
export default function App() {
  const classes = useStyles();
  const [showButton, setShowButton] = useState(false);

  return (
    <div className={classes.relativePos}>

      {/* Your graphs area */}
      <div className={classes.blur}>This is graphs area</div>

      {/* Your absolute button with hover effect */}
      {/* you can add it at the bottom and then no need to use zIndex */}
      {showButton && (
        <button className={`${classes.absolutePos} ${classes.floatingBtn}`}>
          Button
        </button>
      )}

      {/* Temp button to show/hide the absolute button, you should have ur own logic */}
      <button
        className={classes.tempButton}
        onClick={() => setShowButton(!showButton)}
      >
        Click me to show/Hide the button
      </button>
    </div>
  );
}

working example: codesandbox

BTW If you remove filter: "blur(7px)" from the blur class then the hover should work without changing anything in your code. I have no idea why (-_-)

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

Trouble in testing: ComponentDidMount is mysteriously bypassed by Jest/Enzyme when it's supposed to be triggered

In my code, there is a method called componentDidMount that triggers the fetchUser() function. I am currently working on testing the componentDidMount method. Here is the Component Code: static propTypes = { match: PropTypes.shape({ isExact: Pr ...

Filter products by pressing the "Enter" key while using the search input in

I have a list of products and I want to only display products that have a description or name containing the typed word when enter is clicked on the search input. Here is what I tried in my Search component: const Search = (props) => { return ( &l ...

Working with PHP and CodeIgniter to highlight active nav-link in Bootstrap for a subdirectory

Just starting out with Bootstrap and running into a little issue. Spent some time on it but can't seem to figure it out. Perhaps it's something simple that I'm overlooking. Essentially, my goal is to keep the nav-link active when navigating ...

Utilizing Material UI's React framework for maintaining uniform grid column heights

Take a look at the image provided for context. I am facing an issue with two div elements, where one should occupy 20% of the total browser height and the other should take up 80%. Currently, I am utilizing Material UI Grid for positioning, which looks lik ...

The Bootstrap navigation bar isn't displaying properly on mobile devices

Help! My Bootstrap navbar is not working properly on mobile view. The menu icon shows up but when clicked, no items are displayed. Here is the HTML code for my navbar: <header class="header_area"> <div class="main-menu"> ...

Unpredicted organization when combining react, mapbox-gl, custom layer, and three.js

I am currently working on utilizing mapbox gl to showcase gltf buildings through a custom layer. Additionally, I am displaying the default 3d-buildings provided by mapbox in this manner: export default { id: '3d-buildings', source: 'c ...

Elements that are hidden or not displayed are still able to receive mouse over events

I am in the process of learning how to handle events within svgs and I have encountered a strange issue. Currently, I am working on an infovis project where I create an interface to display various column-graphs. This part is functioning well so far. Ho ...

Achieving full coverage of cell content within a cell area by utilizing align-items in CSS Grid

Is it possible to achieve two conflicting goals using CSS Grid? In my design, I want the content in each cell to be vertically centered using align-items: center; At the same time, I need the entire area of the cell to be filled with color. However, ...

Customize the color of the React Material UI progress bar across all variants

Is there a way to customize the colors of the Material UI linear progress bar for all variants? Below are two solutions I tried. import React from "react"; import { makeStyles } from "@material-ui/core/styles"; import LinearProgress fro ...

Ways to create interactive images

<div className="col-lg-3 col-md-3"> <li> <img src='https://i.imgur.com/fe0T4nw.png' onClick="https://arizonaatwork.com" /> </li> </div> I am currently working on a project where I need to link an image ...

Initiate a react change event using Appium

I'm currently working on automating a hybrid app that has a login screen implemented as a react web view, and unfortunately, I don't have control over it. The challenge I'm facing is that the Sign-in button remains disabled until something i ...

Nextjs - resolving the issue of shopping carts displaying incorrect total amounts

I am currently facing an issue with the total cart amount while building a shopping cart. The problem arises when I visit the cart page as it only displays the amount of the last item added to the cart. state = { cart: { items: [], total: 0 }, }; ad ...

Using React.Js along with Framer Motion, the animations will trigger solely upon the initial loading of

I'm currently developing a React project that involves animating components as they scroll into view using Framer Motion. However, I'm facing an issue where the animation triggers every time I scroll past a component, even if it has already been ...

I'd like to know if there's a method to test my website on the iOS Instagram in-app browser

One issue I encountered with my React app is on a certain page where input fields and buttons are present. While the website functions properly on mobile browsers, users using iOS in the Instagram in-app browser face a problem - after clicking on an input ...

Grid system not being followed by table in Bootstrap layout

I'm struggling to figure out why the table I have doesn't follow Bootstrap's grid system. It took me a good 2 hours to pinpoint and fix the issue. Any help would be greatly appreciated. I could share the code, but it might be easier for you ...

Passing a JSON object between pages in Next.js using props during programmatic navigation

In my Next.js project, I have two pages. On the first page, the user fills out a form to create a post. The information is stored in a large JSON object, which needs to be passed to the second page for the user to preview the post before finalizing it. Wi ...

In what ways can the Material-UI theme be customized to modify the font size of labels for TextFields, Menu dropdowns, and Autocomplete components within a React application?

Currently, I am in the midst of a React project and using Material-UI to style my components. My goal is to modify the theme by adjusting the font size of certain labels such as Text Field labels, Menu dropdown labels, and Autocomplete labels. Despite my ...

How can you utilize jQuery to eliminate specific select field values from the DOM prior to submission?

I am currently working on a Squarespace form that utilizes jQuery to show/hide specific fields in order to create a customized form using the show(); and hide(); functions. $(".option2 select").hide(); The issue I am facing is that simply hiding the fiel ...

Tips for expanding the maximum width of a container using bootstrap4

Having trouble increasing the maximum width of my container. How can I adjust the width in the source code? I've attempted changing the width size in the bootstrap-grid.css file. The Element Inspector lets me change the max-width. Any tips on modify ...

Tips for managing various modifications in input fields with just one function

I need to update the state object when data is entered into a form with 2 fields and submitted. Instead of creating a separate function for each input field, I want to handle the input changes in a more efficient way. Sample Code: import React, {Compone ...