Issue with displaying elements at intended layering order when parent element has fixed positioning

My web app features both upper and lower elements (div) with a position: fixed. Each element has child popups also with position: fixed.

Even though I want to position the popup above its parent element, using z-index doesn't work due to inheritance from the parents.

I've searched on Stack Overflow for a solution to this issue but haven't found one that addresses it directly.

You can view the HTML/CSS code at: https://jsfiddle.net/pLsdspon/

<html>
<head></head>
<body style="height: 100%;">

  <div style="
        background-color: red;
    position: fixed;
    display: block;
    width: 100%;
    height: 50%;
    top: 0%;
    z-index: 2; ">
Top
    <div style="
    background-color: pink;
    position: fixed;
    display: block;
    width: 80px;
    height: 200px;
    top: 40%;
    left: 100px;
    z-index: 20; ">
      PopUp-Top - Need to be on top!
    </div>
  </div>

  <div style="
    background-color: green;
    position: fixed;
    display: block;
    width: 100%;
    height: 50%;
    top: 50%;
    z-index: 2; ">
Bottom
    <div style="
    background-color: lightgreen;
    position: fixed;
    display: block;
    width: 80px;
    height: 200px;
    top: 40%;
    left: 200px;
    z-index: 20; ">
      PopUp-Bottom - Need to be on top!
    </div>
  </div>

</body>
</html>

Answer №1

The issue with the z-index not behaving as expected is due to having nested divs, rather than the position being fixed. Instead of nesting the popup div inside another, why not place them outside (ontop) of the others? https://jsfiddle.net/7efx38um/1/

<html>
<head></head>
<body style="height: 100%;">

<div style="
background-color: red;
position: fixed;
display: block;
width: 100%;
height: 50%;
top: 0%;
z-index: 1;
">
Top
</div>
<div style="
background-color: pink;
position: fixed;
display: block;
width: 80px;
height: 200px;
top: 40%;
left: 100px;
z-index: 20;
">
PopUp-Top - Need to be on top!
</div>

<div style="
background-color: green;
position: fixed;
display: block;
width: 100%;
height: 50%;
top: 50%;
z-index: 1;
">
Bottom
</div>
<div style="
background-color: lightgreen;
position: fixed;
display: block;
width: 80px;
height: 200px;
top: 40%;
left: 200px;
z-index: 20;
">
PopUp-Bottom - Need to be on top!
</div>

</body>
</html>

You can also consider placing both the banner and popup divs inside separate dummy containers, so they won't share styles and z-index values ;)

'

Answer №2

It appears that the task at hand is proving to be quite challenging.

#Krypton's proposed solution tackles the issue by adjusting the html structure; unfortunately, this approach is not feasible in my specific scenario.

The sequence of elements, known as the Stacking Order, operates based on the following principles:
1. Elements without z-index or position follow the order in which they appear in the html markup.
2. Positioned elements (relative, absolute, and fixed) take precedence over non-positioned elements (static).
3. The use of z-index impacts positioned elements and establishes a Stacking Context.

In a Stacking Context, elements with a shared parent form this context under the following conditions:
1. The root document element (<html>).
2. Positioning with z-index.
3. Elements with opacity less than 1 (a lesser-known fact among web developers).
Elements within a Stacking Context maintain their relative stacking order, preventing an element inside one context from appearing above another if the latter has a higher stacking order, even with a high z-index value.

Updates: Recent CSS properties creating Stacking Context include: transform, filter, css-region, and pages_media.

Order within a Stacking Context:
1. Root element
2. Positioned element with negative z-index.
3. Non-positioned elements following the html markup order.
4. Positioned elements.
5. Positioned elements with z-index sorted by their values.

  • Returning to the example, both the red and green divs establish a Stacking Context due to being positioned (fixed) with z-index.
  • Given their identical z-index values (2), the red div will be situated beneath the green div per the html order.
  • Considering the pink and lightgreen elements nested within red and green respectively, the stacking order dictates all elements within red to be lower than those within green.

To resolve this matter, introducing a new element to create a Stacking Context with a higher order than the red and green divs, allowing for the placement of popups within that element, is recommended.

For more insights, refer to "What No One Told You About Z-Index" by Philip Walton:

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

Utilizing JSON for HTML conversion in Codeigniter

public function getCrew(){ $search = $this->input->post('name'); if($this->input->post('ajax') && (!empty($search))){ $result = $this->model->getNames($search); foreach($result as $r){ ...

I'm having trouble with aligning my input checkbox within the form

My form includes multiple checkboxes, but I am having trouble aligning them properly. <!-- Multiple Checkboxes (inline) --> <div class="form-row"> <label class="chkdonar" for="donarchkform-0">Yes, I want to donate to AMIAB</label> ...

Changing the color of a tooltip for a specific element using Bootstrap 4

Is there a way to customize the tooltip background-color for icons with the classes "alert-danger" to red and "alert-success" to green using the following CSS commands: .alert-danger + .tooltip > .tooltip-inner { background-color: red !important; } ...

Operating PHP program from html on Beaglebone Black Rev C Debian

I've created a JavaScript program that utilizes node.js to manage GPIO from an IO.html page to the beaglebone. I simply initiate the JavaScript with "node myscript.js". Now, I'm looking to incorporate a PHP file that will store data from the IO. ...

Steps for updating images and text on a live webpage after launching the web application

I added an image to a jsp file. The image, named kid.jpg, is downloaded and stored in the /resources/images folder as shown in the code snippet below. <li style="background-image: url('resources/images/kid.jpg');"> Now that my web app is ...

Show the file name in the email signature instead of displaying the image

I'm facing a unique challenge with images in my HTML email signature. Sometimes the images are replaced by their file names on certain email hosts. Interestingly, I can't replicate this error now as the images are displaying correctly again! He ...

Creating CSS Effects for Text Hover and Transition

Looking for help to implement the hover effect 4 on the following site: . I was able to create a test on JSFiddle, check it out here: https://jsfiddle.net/34aaqh70/ Struggling to make it auto-responsive, any tips would be appreciated. If there's a ...

Is there a way to create an image gallery layout similar to Pinterest using CSS?

I am currently developing a dynamic PHP gallery that will feature thumbnails with the same width but varying heights. These thumbnails will be arranged from left to right, so I would prefer not to use a traditional five-column layout. I suspect that achiev ...

What is the best way to incorporate the icon directly from the .svg sprite into CSS/SCSS?

An irate customer is demanding that I use all icons inside an SVG file according to 2023 standards. This includes icons for background images and normal icons that can be easily changed. I currently utilize an SVG sprite in the following format: <?xml ...

Custom-designed background featuring unique styles

I have implemented the following code to create a continuous running banner: <style> #myimage { position: fixed; left: 0%; width: 100%; bottom: 0%; background:url("http://static.giga.de/wp-content/uploads/2014/08/tastatur-bild ...

Adjusting the display property using the CSS plus symbol operator

I am currently in the process of designing a dropdown menu using CSS. The focus is on utilizing the following code snippet: #submenu_div { display: none; } #li_id:hover + #submenu_div { display: block; } UPDATE: Here is the corrected HTML structure ...

Tips for automating file uploads in HTML

I am having trouble filling the <input type="file"> element programmatically when trying to upload a file using a form. Can someone please provide me with guidance on how to accomplish this task? The method does not matter, I just want to achieve m ...

Trouble Viewing Images on Website

I am currently dealing with a critical issue in my project. I am using the MVC model and have my CSS stylesheet located in the view folder. In this stylesheet, I have included the following code for the body element: body{ margin-left:250px; backg ...

jQuery selector unable to locate the specified class in the table row

After receiving an array of strings as a response from an SQL query, I am using JavaScript to dynamically add this data as rows to an HTML table: loadUsers: function() { . . . function displayUsersOnTable(response) { for(var i = ...

What is the best way to ensure the second navbar stays at the top when scrolling, after the first one?

Looking for a solution to create a navbar with two distinct sections for contact info and the menu. The goal is to have the contact info disappear when scrolling down, while the menu remains fixed at the top of the page. When scrolling back up, the menu sh ...

Display information from a MySQL database in a tabular format using PHP

I am currently learning PHP and attempting to retrieve data from a database to display it in an HTML table. However, I am facing an issue where the total number of records returned is 13 but only 12 records are being displayed in the table (it seems to be ...

Passing data from the <ion-content> element to the <ion-footer> element within a single HTML file using Ionic 2

In my Ionic 2 html page, I have a setup where ion-slide elements are generated using *ngFor. I am seeking a way to transfer the data from ngFor to the footer section within the same page. <ion-content> <ion-slides> <ion-slide *n ...

Tips for making nested sliding divs within a parent sliding div

Is it possible to slide a float type div inside another div like this example, but with a white box containing "apple" text sliding inside the black div it's in? I have attempted to recreate the effect using this example. Here is my current JavaScript ...

Issue with Jquery addClass method in Firefox

I am encountering an issue with the jQuery addClass() function in Firefox where the class is not being added to the current element. Interestingly, this code works perfectly in Chrome and Safari (IE has not been tested). Here is the CSS snippet : $(doc ...

Having trouble with PHP form not functioning correctly?

Is there a way to display the var_dump with the 'selected country' information? I am having trouble due to an issue with the $res (array) $id. <?php if(isset ($_POST['submit'])) { $id = $_POST['cata']; $api = new ...