The z-index returned by getComputedStyle is not what you would typically

When attempting to retrieve the computed style of an element with no defined position, the result is auto.

I find it surprising that the parent element has z-index: 100;

Should getComputedStyle return 100 for A1 or is auto the correct value (even though the z-index for A1's parent is greater than B)

jsFiddle

CSS

html, body {
    margin: 0;
    padding: 0;
    border: 0;
}
#A, #B {
    position: absolute;
}
#A {
    top: 35px;
    width: 200px;
    bottom: 35px;
    background-color: #999999;
    z-index:100;
}
#A1 {
    height: 50px;
    width: 200px;
    margin-left: 200px;
    background-color: #CC0066;
}
#B {
    top: 35px;
    left: 200px;
    right: 0;
    bottom: 35px;
    background-color: #99CC00;
}

HTML

<div id="container">
    <div id="A">
        <div id="A1">I am A1, on top of B.
            <br />My parent has z-index 100</div>
    </div>
    <div id="B">
        <br />
        <br />
        <br />I am B and have no z-index. I
        <br />f I had `z-index:200;` A1 would not be visible</div>
</div>

Javascript

var elementA1 =  document.getElementById('A1');
var a1 = window.getComputedStyle(elementA1).getPropertyValue("z-index");
console.log(a1); // logs 'auto'

Answer №1

To ensure the correct z-index value for #A1, it is necessary to specify position: relative;.

Additionally, if only #A element has a z-index, then #A1 should also have z-index:inherit;

I made some modifications to your code in this updated fiddle: http://jsfiddle.net/6DJpY/1/

For further information, please refer to:

Answer №2

auto is the correct choice in this scenario. This element does not create a new stacking context and is at level 0 within the current stacking context, as explained in z-index:

auto

The stack level of the generated box in the current stacking context is 0. The box will only establish a new stacking context if it is the root element.

This indicates that in relation to A, A1 is on the same level. However, in relation to container, A1 is at level 100:

#container    (level   0 in context 0)
    #A        (level 100 in context 0; new context 1)
        #A1   (level   0 in context 1; relative to #container: 100 via context 1)
    #B        (level   0 in context 0)

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

Is there a way to stop objects in a particular list from being dragged into another nested ul using jquery sortable?

I am faced with a challenge regarding nested lists, specifically when it comes to preventing parent-parent list items from dropping into an inner <div> within an <li> <div> that already contains its own <ul> <li> list. Additio ...

Exploring the integration of Django Rest Framework with Angular

I am just starting out with Django Rest Framework (DRF) and AngularJs. I am trying to figure out the best way to integrate these two technologies. Combining DRF and AngularJs in one project (Most of the tutorials recommend this) Using DRF as the backend ...

Switching to http2 with create-react-app: step-by-step guide

Can someone provide guidance on implementing http2 in the 'create-react-app' development environment? I've searched through the README and did a quick Google search but couldn't find any information. Your assistance is much appreciated. ...

Is the code failing to refresh the browser when the file is modified?

Does anyone know how to reload the browser when a file changes? var changing = require('changing'); var watcher = changing( { interval: '0s' }); watcher.add("/home/diegonode/Desktop/ExpressCart-master/routes/2.mk"); watcher.on(& ...

Switching node.js URL parameters from ID to keywords for a blog: A step-by-step guide

As I work on building my blog with node.js, one of my priorities is to enhance its search engine optimization. Currently, when a user accesses the show route for a specific post, the URL looks like this: /blogs/:id, where :id represents a lengthy string o ...

Design element: Text overlaying background with a touch of transparency

Is there a way to create a background image with an opacity level of 0.5, while maintaining the text on top at full opacity (1) for better readability? Currently, the background image is not displaying as desired, and the opacity settings are affecting the ...

The XMLHttpRequest fetches data before the page is completely loaded

I'm currently working on automating the search functionality of using XMLHttpRequest. However, I am facing an issue where the response is being received before the results finish loading. If you append a search term like "Layer", "Geoprocessor", "Le ...

Leveraging ng-repeat solely for the purpose of parsing without generating an excessive number of HTML elements

When using ng-repeat in a div to iterate through an array and display data, I encountered an issue. Within the first ng-repeat, I am using another ng-repeat to parse through a different array and show specific data. However, during the second loop, two div ...

.then() failing to wait for completion of all promises

Currently, I am loading multiple gltf models using an async function. After that, I go through the results using .then() and add the models to my scene. However, everything works perfectly fine until I decide to remove one of the models from the source. Sp ...

What is the best way to create a gap between two Bootstrap buttons?

I'm attempting to create two buttons in the same line in this code, but it's not working as I want. Take a look below: <div class="w3-show-inline-block"> <div class="w3-bar"> <button class="btn btn-s ...

Error with WooCommerce checkout causing input values to disappear upon clicking or submitting

I am facing an issue where I need to set #billing-postcode to a specific value using a JS script. When I input jQuery('#billing-postcode').val('2222') on the checkout page, the input displays the value 2222 with the Postcode label abov ...

What is the best way to smoothly insert an item into a nested object within the state of a React component

Trying to create a complex array using react hook: const [strategy, setStrategy] = useState([leg]); Here's how the `leg` object is defined: const leg = { entry: { conditions: [], actions: [""], }, exit: { conditions: [], ...

What is the process for creating a sublist within an md-list-item using Angular Material?

I used Angular Material to create a md-list on the side-nav. I am looking to add a sub list under the md-list-item. Below is my code snippet: <md-sidenav id="sideNav" md-component-id="left" class="md-sidenav-left md-whiteframe-z1 md-locked-open " layo ...

Tips for effectively utilizing Vuex to manage items within a Vuetify data table

I am currently sending an API request to retrieve an array of objects that has the following structure: returnedItems [ { name: 'Adam', age: '30', interest: 'Sports' }, ...

Transition Effects in Bootstrap Navbar Hover Menu

I recently came across this codepen showcasing a CSS transition for achieving a sliding effect on hover, which can be found here. However, I am having trouble implementing it in my Bootstrap navigation bar. Could it be incompatible with the menu in Boots ...

javascript popup window with redirection functionality

Hello everyone, I need assistance with the code snippet below: echo ("<SCRIPT LANGUAGE='JavaScript'> window.alert('Successfully Updated') </SCRIPT>"); I am trying to redirect the page to my edit.php after c ...

Build desktop-style applications using a unique user interface library and integrated development environment designed specifically for HTML, JavaScript

I am looking to create a desktop application using HTML, JavaScript, and AJAX for the purpose of making it available on multiple platforms. I was wondering if there is a UI library and web IDE similar to WebOS Enyo that I can use to build my HTML/AJAX app? ...

Animation of CSS with an image depicting a leaf swaying gently in the breeze

This project was a personal challenge for me, and I am quite satisfied with the approach I have developed. However, I am open to exploring alternative methods if there are any. The website I am working on features a logo that includes an image of a leaf. ...

Utilize JavaScript API for generating and retrieving XSD schema and XML documents

Are there any stable JavaScript APIs that can be used to generate and read XSD schemas and XML documents? My specific requirement is: Allow one user to define/generate an XSD schema through a UI. Later, allow another user to provide appropriate data ...

Why isn't my lightbox able to read this specific property?

A gallery was created using both lightgallery and cycle2. Cycle is a carousel plugin while lightgallery is a lightbox gallery. Clicking on an image in the carousel opens it in the lightbox. Everything worked perfectly until a category in the carousel was ...