The z-index of two elements seems to be behaving differently than anticipated

I am encountering an issue with the code provided below, which is a simplified version of a larger problem.

Can you explain why, if:

.div_A {z-index: 10;} < .div_C {z-index: 20;}

the button inside div_C appears behind div_A?

I need to understand the specific reason so I can apply the solution accurately to my main issue.

If possible, please provide the corrected code (with minimal changes) as it would be greatly appreciated.

.button {
display: inline-block;
color: #fff !important;
background-color: #be1e2d;
text-decoration: none;
padding: 10px 23px;
}
.div_A {
display: inline-block;
position: absolute;
top: -100px;
z-index: 10;
}
.div_B {
position: relative;
display: inline-block;
width: 400px;
height: 400px;
background-color: #aaf;
opacity: 0.9;
}
.div_C {
text-align: center;
z-index: 20;
}
<div>
<div class="div_A">
<div class="div_B"></div>
</div>
<div class="div_C">
        <a href="#" class="button" style="">TRY TO CLICK ME!</a>
</div>
</div>

Thank you!

Answer №1

To ensure that the z-index property takes effect as desired, elements must have a specific position value other than static. I included position: relative; in the snippet for DIV_C below.

.button {
display: inline-block;
color: #fff !important;
background-color: #be1e2d;
text-decoration: none;
padding: 10px 23px;
}
.div_A {
display: inline-block;
position: absolute;
top: -100px;
z-index: 10;
}
.div_B {
position: relative;
display: inline-block;
width: 400px;
height: 400px;
background-color: #aaf;
opacity: 0.9;
}
.div_C {
text-align: center;
z-index: 20;
position: relative;
}
<div>
<div class="div_A">
<div class="div_B"></div>
</div>
<div class="div_C">
        <a href="#" class="button" style="">TRY TO CLICK ME!</a>
</div>
</div>

Answer №2

Don't forget to include position: absolute; in the .div_C class!

.button {
display: inline-block;
color: #fff !important;
background-color: #be1e2d;
text-decoration: none;
padding: 10px 23px;
}
.div_A {
display: inline-block;
position: absolute;
top: -100px;
z-index: 10;
}
.div_B {
position: relative;
display: inline-block;
width: 400px;
height: 400px;
background-color: #aaf;
opacity: 0.9;
}
.div_C {
text-align: center;
z-index: 20;
position: absolute;
}
<div>
<div class="div_A">
<div class="div_B"></div>
</div>
<div class="div_C">
        <a href="#" class="button" style="">TRY TO CLICK ME!</a>
</div>
</div>

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

Having difficulty centering an image in HTML?

I've been struggling to get the image (logo) centered on the top bar! I've tried using align-self: center;, display:block;, but nothing seems to work. It feels like I'm missing something here! Click here for the codesandbox link to view the ...

Experimenting with @media queries to dynamically resize images for various devices such as phones, tablets, and desktop

I'm struggling with making the background images on my website responsive. I have already added media queries to my page, but they seem ineffective and I am unsure of what else to try. Below are the HTML and CSS codes for the three images. Any suggest ...

Tips for applying/styling "All Items Center" and "Space Between Items" in CSS and ReactJS

justify-content: space-evenly; seems to be working correctly, but the desired output is not achieved I am aiming for something like this: https://i.stack.imgur.com/iQoWK.png However, this is what I currently have: https://i.stack.imgur.com/1PxGR.png ...

Utilizing MaterialUI and CSS to craft this stunning box

Looking to create a new MaterialUI component similar to this one: original box Struggling with implementing it using the card component, but so far it looks like: poorly designed box Here's my custom styling using makeStyles: const useStyles = ma ...

How about designing a button with a dual-layered border for a unique touch

Looking for a specific button: My attempted CSS code that's not working: button { font-family: 'Ubuntu', sans-serif; font-size: 1em; font-weight: bold; color: white; border: 3px double #f26700; background: #f26700; ...

Ways to expand the width of mat-dialog-actions component in Angular 8

Is there a way to make the cancel and save buttons in the dialog window take up the entire available space? If anyone has any suggestions on how to achieve this, please let me know! https://i.sstatic.net/rfQrN.png ...

Styling a PrimeFaces panelGrid within a data table using CSS

I am struggling to add a background color to a panelgrid within a datatable when a hover event occurs. Despite writing the necessary code and CSS, nothing seems to work. Any suggestions on how to address this issue? <p:dataTable id="movementsTable" var ...

How can I display different divs based on a selected option?

I'm encountering difficulties while attempting to develop the code for this specific task. My goal is to display or hide divs based on the selection made in a select option, with a total of 4 options available. There are 2 select elements <select ...

Tips for binding data to numerous dynamic controls

Implementing reactive forms in my Angular project allowed me to create a simple form for adding employee work hours and breaks. The challenge I encountered was the inability to bind data from break controls. In the .ts file export class AddAppointmentForm ...

Attempting to place a different span beneath the current span

Is it possible to add another span below the first span and keep it on the same line as the circle? Check out this link for reference The following is the HTML code snippet: <div class="dialog-box"> <div class="dialog-box-circle"></d ...

Calculate the number of parent nodes and their respective child nodes

I am curious about how I can determine the number of children nested within parent-child relationships. For example: const parent = document.querySelectorAll('.parent'); parent.forEach(el => { const ul = el.querySelector('.child3-chi ...

Utilizing Vue JS to set an active state in conjunction with a for loop

Currently in Vue, I have a collection of strings that I am displaying using the v-for directive within a "list-group" component from Bootstrap. My goal is to apply an "active" state when an item is clicked, but I am struggling to identify a specific item w ...

Transparent dropdown elements using Bootstrap

I'm trying to incorporate a transparent bootstrap dropdown into my form. Specifically, I want the button itself to be transparent, not the dropdown list that appears when the button is clicked. Here's an example of what I currently have: https: ...

Is there a way to stop my <pre> code from being displayed on the page?

Currently, I am utilizing the google code prettifier found at http://code.google.com/p/google-code-prettify/ This tool functions similarly to stack overflow by enhancing and highlighting syntax when a block of code is input. However, I have encountered a ...

Retrieve CSS height using Jquery, based on the declared value rather than the computed value

In a previous version of jQuery, the default behavior was different. When I use .css("height") and .height(), it gives me the computed height instead of what I actually want. I only need the height value if it is explicitly declared in the CSS for that ele ...

Issue with the close button on ngb-alert not functioning properly

As I develop my website, I have incorporated an ngb-alert component to display an alert message to users upon login. While the alert itself is functioning properly, I have encountered an issue with the close button being misaligned, and I am struggling to ...

Failure of Outlook 2007, 2010, and 2013 to Recognize Conditional CSS

I am currently working on a design for a responsive email. The layout is functioning well in all email clients tested using Litmus, except for Outlook 07/10/13 due to its challenging word rendering engine versions. To ensure correct display across differen ...

Enhancing Image Quality in Internet Explorer 10

Here is my current situation: I am working on a web page with a responsive design. The layout of the page consists of two vertical halves, and I intend to display an image (specifically a PDF page converted to PNG or JPG) on the right side. The challenge ...

The React Vite application encountered an issue: There is no loader configured for ".html" files at ../server/node_modules/@mapbox/node-pre-gyp/lib/util/nw-pre-gyp/index.html

**Encountered errors in a React Vite web app** ** ✘ [ERROR] No loader is configured for ".html" files: ../server/node_modules/@mapbox/node-pre-gyp/lib/util/nw-pre-gyp/index.html ../server/node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js:86 ...

Ways to align text on the left within a centered format

It seems like there is something missing here. I am struggling to align text to the left in a centered div. Below is an example of my final work: <div class="grid-row"> <img src="http://www.fununlimitedsports.com/wp-content/images/istock ...