What sets apart elevation from z-index in material-ui?

As I delved into the material-ui documentation, I came across an interesting snippet:

Various components in Material-UI make use of z-index, a crucial CSS property that aids in organizing content along a third axis. Material-UI employs a predefined z-index scale to effectively layer drawers, modals, snackbars, tooltips, and more.

I have a dilemma about creating a component where it pops on top of another one upon button click and vanishes once a task is completed.

My query revolves around whether I should utilize the elevation property by enclosing the popper component within a paper component or opt for z-index instead? How does the usage of elevation differ from zIndex in material-ui considering their reliance on zIndex for multi-dimensional styling?

Answer №1

In the commentary section, ehutchllew pointed out that elevation does not affect z-index and merely provides a raised appearance to the Paper element through shadowing. The z-index property determines the stacking order of positioned elements when they overlap.

Below is a sample code snippet illustrating both concepts:

import React from "react";
import ReactDOM from "react-dom";
import Paper from "@material-ui/core/Paper";
import CssBaseline from "@material-ui/core/CssBaseline";
import { withStyles } from "@material-ui/core/styles";
const styles = {
  root: {
    width: 100,
    height: 100,
    margin: 10,
    padding: 10
  }
};
function App({ classes }) {
  return (
    <>
      <CssBaseline />
      <Paper elevation={0} classes={classes}>
        Elevation 0
      </Paper>
      <Paper classes={classes}>Default Elevation (2)</Paper>
      <Paper elevation={4} classes={classes}>
        Elevation 4
      </Paper>
      <Paper elevation={8} classes={classes}>
        Elevation 8
      </Paper>
      <Paper elevation={16} classes={classes}>
        Elevation 16
      </Paper>
      <div style={{ marginTop: 30 }}>
        <div
          style={{
            height: 20,
            backgroundColor: "lightblue",
            position: "relative",
            top: 0,
            zIndex: 2
          }}
        >
          zIndex - I have a middle zIndex value
        </div>
        <div
          style={{
            height: 20,
            backgroundColor: "yellow",
            position: "relative",
            top: -10,
            zIndex: 3
          }}
        >
          zIndex - I have the highest
        </div>
        <div
          style={{
            height: 50,
            backgroundColor: "lightgreen",
            position: "relative",
            top: -50,
            zIndex: 1
          }}
        >
          zIndex - I have the lowest
        </div>
      </div>
    </>
  );
}
const StyledApp = withStyles(styles)(App);
const rootElement = document.getElementById("root");
ReactDOM.render(<StyledApp />, rootElement);

https://codesandbox.io/s/jpolv9wmkw

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

Ways to update a nested object by utilizing the setState method

I have a situation where I need to modify the key/value pairs of an object in my state. Specifically, I need to add a key/value pair to the object with an ID of 13. Please refer to the attached photo for reference. Although I know how to use setState, I ...

What is the best way to establish a state within a component?

How can I update the state of a component in ReactJS to show notifications again? import React, { useState } from 'react' const NotificationError = (props) => { const [notify, setNotify] = useState(false); console.log("notify.state: ...

SampleMessageDialog implemented in material design for Windows Presentation Foundation (WPF)

Hi everyone, I'm currently exploring how to call the SampleMessageDialog within my app. Here's the code snippet I have so far for a button on my form that is intended to trigger the message box: Private Async Sub BrowseButton_Copy_Click(sender ...

Setting up a Next.js configuration to automatically route all visitors to a designated path on my website

I am working on a Next.js app that consists of various pages such as /, /pre-registration, /contest, /profile, and /auth. For deployment, I want to restrict user access only to the /pre-registration page while redirecting them to it if they try to access ...

React Hook Form - The onSubmit function is unresponsive

I'm currently working on a small project using NextJS 14 and implementing react-hook-form for easier form submission and input handling. Unfortunately, I've been struggling with an issue for the past few days despite trying various troubleshootin ...

Utilizing jQuery to apply a class to a targeted element when clicked

I'm currently working on animating a menu item where it expands to the left when hovered over, and contracts back when the mouse moves out. This part is functioning as expected. Additionally, I am trying to apply a specific color to the button by add ...

Tips on utilizing the useState hook for storing numerous key objects?

Currently, I am working with a candlestick chart for a cryptocurrency that displays data over different timeframes such as 1 minute and 30 minutes. Below is the code snippet that sets the initial state to show a 1-hour chart when a user first visits: const ...

Using conditional CSS values in Angular 8

I am currently working on a parent component and child component setup, where I am utilizing the child component's selector to display it within the parent component. Here is my child component's HTML: <div class="main-container" [n ...

Displaying localized error messages in React

Currently, I am developing a FrontEnd application using React and implementing L10n for localization. The process works smoothly for displaying static text like this: <input {...input} type="password" placeholder={strings.passwordPlaceholder} ...

Ensuring equal spacing between elements that are defined as a percentage, utilizing padding and margin properties

I've been searching online for a while now, but I haven't been able to find a solution that fits my needs. What I'm trying to do is space out 4 divs equally, each with a width of 25% minus the margin. The challenge is ensuring that the last ...

Using Material UI, I have styled a typography element and a button to appear on the

Struggling to position a button and a <Typography> element side by side: Here's what I currently have: <Grid item lg={12} md={12} sm={12} xs={12} className={classes.register}> <Typography noWrap className={classes.typoText} varian ...

Adding an overlay to a material UI table row: Step by step guide

My code is rendering a row in the following format: `<TableRow key={row.name} > <TableCell>{row.empId}</TableCell> <TableCell>{row.userId}</TableCell> <TableCell>{row.name}</TableCell> <TableCell>{r ...

Updating Button Text with PHP on WooCommerce

Hi there, I'm trying to find a way to translate a button in my WooCommerce store without using LocoTranslate or any other tools. Is there a PHP function that can help me change the button text without needing an ID? If you want to take a look at the ...

The concepts of width and min-width in CSS allow for controlling

I have a table with the following styles applied: width: auto; min-width: 98.5%; When viewing in Chrome, the table inside a div appears to only be about 50% of the width of the containing div. However, in IE9, the table occupies the full width. Checking ...

The logo positioned on the left with menu links perfectly centered

Looking for Help with Navigation Layout Greetings. I am encountering some challenges while trying to code the navigation layout depicted in the image (linked above). My goal is to have the logo, which is an image, aligned on the left side and the menu lin ...

What is the best way to avoid passing a value to a component in nextjs?

I'm trying to make this component static and reusable across different pages without passing a parameter when calling it. Is there a way to achieve this while following the official documentation? component: import { GetStaticProps, InferGetServerSid ...

Vertical stability bar

I need help creating a vertically fixed navigation bar for my website. Currently, I am using a method that has been discussed in various posts: HTML: <html> <head> src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">< ...

Setting state dynamically in Typescript with ReactJS

Within my state, I have defined this interface: interface State { id: string; name: string; description: string; dimensionID: string; file: File | null; operator: string; isFormValid: boolean; filename: string; }; To handle changes, I&apo ...

What is the most efficient way to substitute text within an HTML document?

Is there a way to switch between languages on a website to replace text on multiple pages with a simple button click? What is the most efficient method for achieving this? This code snippet only changes text in the first div. Is there a way to implement a ...

What could be causing the Material AngularJS (CSS) on my website to not function properly, unlike the demo that was

I am completely new to AngularJS and I want to incorporate the material design version of AngularJS for developing a form. I followed the beginner's guide and attempted to create something, but it didn't turn out like the one on the website. So, ...