Adjusting the transparency of a button results in it being displayed above other elements within its parent container

As I work on developing a configuration page using React, I encountered an interesting issue. The page contains a table with configurable elements and a "save" button that starts off disabled and transparent. However, once a data field is modified, the button becomes enabled and fully opaque.

The problem arises when the disabled button overlaps parent elements like the sticky table header and nav bar, especially noticeable during scrolling. The issue goes away once the buttons are enabled, as they no longer cover the parent elements.

I am currently utilizing the react-bootstrap Button component to render the button, but switching to a regular HTML button yields the same results. It seems that both disabling the button and adjusting the opacity independently trigger this behavior.

<td className="saveButton">  
  <Button
    variant="primary"
    id={this.props.id}
    disabled={!this.state.modified} //Only enable saving after data has been modified
    style={this.state.modified ? {opacity: 1} : {opacity: 0.3}}
    onClick={this.updateListItem}
  >Save</Button>
</td>

The expected behavior is for the buttons to scroll beneath the sticky table header and navbar along with the rest of the table row. However, the disabled buttons end up rendering over these parent elements instead:

(The enabled button functions correctly by scrolling beneath the table header)

Any thoughts on what might be causing this issue?

Answer №1

I encountered the same issue you described while working with a basic table layout featuring a sticky header. By uncommenting the z-index in the th, the problem is resolved. Check out the fix here: https://codepen.io/sallf/pen/BaaBwmR

You've only provided a glimpse of the button within the table, so I can't determine your specific table setup, but this solution seems promising.

th {
  position: sticky;
  top: 0;
  background: white;
  // Removing z-index will cause td to appear over th
  z-index: 1;
}

https://i.sstatic.net/URiqg.png

https://i.sstatic.net/Pqy7C.png

The root cause of this behavior remains unclear to me. It appears to be a browser bug, but I'm open to further insights from others.

Update

This anomaly arises because an element with an opacity less than 1 creates a new stacking context. Element positioning within a stacking context is based on stacking order.

As per the color module:

If an element with opacity less than 1 is not positioned, then it is painted on the same layer, within its parent stacking context, as positioned elements with stack level 0.

Essentially, a static element with opacity < 1 shares the same stack level as a positioned element with default z-index: auto (when within the same parent stacking context).

In cases of equal stack levels, the stacking order follows the rendering tree (html element order). Here, since the td comes after th in the html structure, td will be displayed in front of th.

Refer to What No One Told You About Z-Index for an example.

Related query.

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

Styling dropdown menus with CSS

I have encountered an issue with the dropdownlist control on my asp.net page. I set the width to 150px, but in Mozilla browser, the list items appear expanded while in IE, they are restricted to the width of the dropdown list. How can I achieve a consisten ...

Loading indicator displayed at the top of a div using JavaScript/jQuery

My current challenge involves implementing a progress bar, similar to the pace.js progress bar. The issue arises when the browser is refreshed, as the pace.js progress bar loads on top of the body instead of within a specified div. It is important that the ...

Is there a way to stop text from being selected when double clicking?

#btnLeft{ width:40px; padding:2px 5px 2px 5px; background-color: #20a0a0; border: thin solid #099; border-radius:7px; text-align:center; font-family:"Futura Md BT"; font-size:large; color:#FFF; } This particular code sn ...

challenge with filling and overflowing in Microsoft Edge

Here is a snippet of generic CSS (SCSS) style for flex: .flex { display: flex; &.v { flex-direction: column; } &.h { flex-direction: row; } > * { flex: 0 0 auto; } > .fill { flex: 1 1 auto; } &am ...

The attribute *** is not found in the 'ExoticComponent<any>' type

I'm attempting to implement code splitting in my React/Next.js project, but I'm encountering some challenges. I've been following the guidelines outlined here: https://reactjs.org/docs/code-splitting.html The import statement looks like th ...

Error: When attempting to test my react-redux connected component with redux-mock-store, I encounter a TypeError stating that properties of undefined cannot be read, specifically 'getState'

I have been working on testing my function connected to a React-Redux component using `redux-mock-store`. However, I keep encountering the error message `TypeError: Cannot read properties of undefined (reading 'getState')`. Even after trying both ...

Error encountered while deploying NodeJS application on Heroku platform

I encountered some errors while trying to deploy my project to herokuapp: 2018-07-27T08:27:30.827410+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/robots.txt" host=ihealth99.herokuapp.com request_id=54687736-9467-4484-93d6 ...

My React project is unable to import the foundation SCSS file into the App.scss file

After installing zurb-foundation using npm, I attempted to import the .scss file into my App.scss file. Utilizing the "live sass server" for compiling the .scss code into .css code, a .css file was generated but did not display any code despite successfull ...

Implement a shadow effect on a customized image using Tailwind CSS

Can someone help me with this code snippet I have written: <script src="https://cdn.tailwindcss.com"></script> <div class="m-6 space-x-3"> <div v-for="m in media" class="w-[200px] h-[200px] inline-block"> <img class=" ...

Stop the Bootstrap 5 accordion from expanding when the spacebar is pressed in the header text input

Within my React app using react-bootstrap, I have implemented an accordion component that expands or collapses based on the user's interaction with a text input located in the AccordianHeader component. Interestingly, whenever the spacebar is released ...

The CSS style is missing from the print preview

I've designed a style specifically for printing like this: #content .inner h2 { background: Red; border-bottom: solid 1px #dfdfdf; color: #000000; font-size: 20px; padding-left: 10px; } However, the red color doesn't show up in the print previe ...

Tips for managing a back button press in a single page application

Currently, I am in the process of developing a single-page application (SPA) using NextJS. The entire website consists of just one page, with no next/link or Router functionality being utilized. Everything that occurs, such as button clicks and navigating ...

Exploring Flexbox Grid on Safari and Chrome

I have encountered an issue while trying to implement a grid using flexbox and sass. It seems to be functioning smoothly on Chrome, but I am facing some problems with Safari. The red boxes are "overflowed" and do not break to a new line as expected. An im ...

String variable representing the name of a React element

As I was reviewing the source code of a React UI library, I stumbled upon an interesting code pattern that I will simplify here: function Test() { let Button = "button"; // ... return <Button>Click me</Button>; } I'm curious about ...

MAC Safari is not registering input fields

I'm experiencing a small issue with a form that has two input fields for indicating time (in the format HH:mm) that are very close together, like this: https://i.sstatic.net/u2GzN.jpg Here is the HTML code snippet: <label> <span>Hou ...

Ensuring the alignment of a personalized list bullet with the accompanying text

Looking to make the bullet point next to an li element larger? The common approach is to eliminate the standard point, add a custom one using the :before selector, and adjust the font size of the added point. While this technique somewhat achieves the goa ...

The buttons within the bootstrap navbar are not properly collapsing or resizing when the screen size is decreased, causing them to overlap and maintain their

As a newcomer to bootstrap, I recently created a navbar with several buttons. However, when I reduce the screen size to 1000 pixels, the button text gets overwritten and the navbar extends off the screen. I don't want the navbar to collapse at this si ...

Is there a method within the blueprintjs tabs component to showcase the tabs at the bottom of the tab panel?

Currently, I am working with Version 4 of Blueprintjs and experimenting with its tabs component. I've been trying to find a way to display the tabs at the bottom of the panel, similar to how they are often seen in spreadsheets. While there is an optio ...

NextJS - You can only call hooks within the body of a function component, otherwise it will result in an invalid hook call

Encountering an error while attempting to build with NextJS, even though styled-components are not being used. Deleting node_modules and other attempts have been unsuccessful. Added aliases and resolves for react in next.config.js file and package.json wit ...

Steps for adjusting an image in a Bootstrap Carousel

Currently tackling a simple bootstrap Carousel for a school project. I've adjusted the max-height of the images to 400px but it seems like only the top part of the images is visible. I attempted to adjust the margins on the image, and even thought abo ...