日本語

Viewing z-index Stacking Order in 3D with Chrome DevTools

​​Chrome DevTools​Chrome,CSS,z-index

The Layers panel in Chrome DevTools shows how the elements on a page are stacked, in a 3D diagram seen from an angle. If a dropdown menu or modal has z-index: 9999 but still appears under other elements, you can see which element is in front of which. Chrome has no tool like the 3D View in Edge. In Chrome, you view the 3D diagram in the Layers panel.

However, the Layers panel only shows elements that the browser has painted as separate layers. Elements that only have a z-index stayed in the same single layer as the rest of the page. So when I temporarily added will-change: transform to the elements I wanted to examine, putting them into layers, their stacking order became visible in the 3D diagram.

Note

The test environment was Ubuntu 24.04, Node.js 22, and Chrome for Testing 158.0.8127.36 (the version bundled with Playwright 1.63.0). Since the test environment had no screen, I ran Chrome in a normal window on a virtual display (Xvfb) and displayed DevTools at the bottom of the screen. There was no GPU, so I started Chrome with a launch option that draws WebGL in software. I sent DevTools operations from a script, and for the drag that rotates the 3D diagram, I sent real mouse input with xdotool. The DevTools UI was in English. I did not test Microsoft Edge's 3D View, Mac or Windows, computers with a GPU, publicly available websites, or elements such as position: fixed and popovers.

Chrome has no "3D View"; use the Layers panel

3D View is a tool in the DevTools of Microsoft Edge. According to Microsoft's 3D View documentation (in English), 3D View has a Z-index tab that shows the hierarchy of stacking contexts in 3D, so you can investigate z-index stacking problems.

In Chrome 158, the test environment, typing "3d" into the command menu showed no matching results. Among the 62 panels and tabs registered in DevTools, there was nothing equivalent to 3D View. In Chrome, the Layers panel is where you view 3D diagrams. The Chrome Layers panel documentation (in English) says it shows how layers are stacked and arranged in a diagram.

A page where a menu with z-index: 9999 is hidden

The test page had a menu inside a card, and a notice bar directly below the card. The menu extended out of the card and over the notice. The CSS below includes only the parts related to stacking.

css
.card   { position: relative; z-index: 1; }
.menu   { position: absolute; z-index: 9999; top: 48px; /* an element inside the card */ }
.banner { position: relative; z-index: 2; }

The menu's z-index was 9999, but when displayed, the lower half of the menu was hidden by the notice. I used document.elementFromPoint() to check the topmost element at one point in the lower part of the menu. It returned the notice (#banner).

Opening the Layers panel

Following the official documentation, I focused DevTools and pressed Ctrl + Shift + P (Command + Shift + P on Mac) to open the command menu. I typed layers, and "Show Layers" (with Panel shown on the right) appeared at the top of the suggestions. I pressed Enter. According to the official documentation, you can also open it from the menu (⋮) in the upper right of DevTools, via More tools > Layers.

In the test environment, three seconds after opening the panel, it still said "No layers detected yet" and the layer list was empty. When I focused DevTools and pressed F5 to reload the page, #document (800 × 497) appeared in the list.

The list showed only a parent item with just a number (#6 (0 × 0)) and #document. The card, the menu, and the notice were not in the list. Adding a z-index alone did not make them separate layers. In this state, rotating the 3D diagram just shows the page as a single flat board.

Adding will-change: transform to split elements into layers

I added will-change: transform to the three elements I wanted to examine, using the Console.

js
document.querySelectorAll('.card, .menu, .banner').forEach(e => e.style.willChange = 'transform')

Going back to the Layers panel, the list had not changed after three seconds. Then I moved the mouse over the 3D diagram, and about a second later, the three elements were added to the list.

text
#6 (0 × 0)
#document (800 × 497)
div#card (768 × 84)
div#menu (220 × 130)
div#banner (768 × 114)

When I clicked an item in the list, the Details section below showed the reason it was split into a layer (Compositing reasons). All three said "Has a will-change: transform compositing hint.", which confirms that will-change: transform caused the split.

According to the MDN explanation of stacking contexts, an element with will-change: transform becomes a new stacking context. If you apply it to an element that was not already a stacking context, check whether the page's stacking order is the same before and after applying it, and then look at the diagram. The three elements in the test page all had position and z-index, so they were already stacking contexts. After applying the property, the topmost element at the lower part of the menu was still #banner.

Rotating the 3D diagram to see the stacking order

I checked "Paints" at the top of the panel. According to the official documentation, when Paints is on, most of the page content is painted onto the diagram. Next, I clicked the "Rotate mode (V)" button (the second from the left) and dragged on the diagram to tilt it.

Screenshot of the Layers panel. On the left is the layer list: under the parent #6, the items appear in this order from top to bottom: #document, div#card, div#menu, div#banner. On the right, dragging the diagram tilts the page, and the pale blue card, the blue menu, and the pink notice appear as boards floating slightly apart. The pink notice board overlaps the lower half of the blue menu board.
The 3D diagram before the fix, rotated by dragging in Rotate mode

When tilted, the card, menu, and notice appear as boards floating slightly above the whole-page board. The notice board was in front of the menu board and overlapped its lower half. This is the same overlap that hid the menu on the page.

In the list on the left, the order from top to bottom is div#card, div#menu, div#banner. In the 3D diagram, div#banner, the last item in the list, was the board closest to the front. As shown later, when I changed the card's z-index, the order in the list also changed, and so did the board that appeared in front.

Raising the card's z-index changes the order in the list and the diagram

The menu's z-index: 9999 has no effect because the menu is inside the card's stacking context. According to the MDN explanation, a child's z-index only has meaning within its parent's stacking context, and from the outside, the parent context is stacked as a single unit. The menu was placed below the notice, which has z-index: 2, together with the card, which has z-index: 1.

So I changed the card's z-index to 3 using the Console.

js
document.getElementById('card').style.zIndex = '3'

The order in the Layers panel changed as follows. This time, the list had already changed three seconds after I returned to the panel.

text
#6 (0 × 0)
#document (800 × 497)
div#banner (768 × 114)
div#card (768 × 84)
div#menu (220 × 130)

When I tilted the diagram the same way, the menu was the board closest to the front.

Screenshot of the Layers panel. On the left, the list shows, under the parent #6, these items from top to bottom: #document, div#banner, div#card, div#menu. In the tilted diagram on the right, the blue menu board overlaps in front of the pink notice board.
The diagram tilted in the same way after setting the card’s z-index to 3

On the page, the menu now appeared above the notice, and its lower half was visible. The topmost element at the lower part of the menu also changed to #menu.

Two page screenshots stacked vertically. The top one, before the fix, shows the lower half of the blue menu hidden behind the pink notice bar. The bottom one, after setting the card's z-index to 3, shows the menu overlapping the notice bar, with the whole menu visible.
The page before the fix and after setting the card’s z-index to 3
Point

If raising the z-index does not bring an element to the front, compare the z-index of the outer stacking context rather than the element itself. In the Layers panel diagram, if the outer elements are also split into layers, you can see which group is in front.

Removing will-change after the investigation

I reset will-change to empty using the Console.

js
document.querySelectorAll('.card, .menu, .banner').forEach(e => e.style.willChange = '')

Three seconds after returning to the Layers panel, the list still had not changed. When I moved the mouse over the 3D diagram, about a second later, the list went back to only #6 (0 × 0) and #document (800 × 497).

I applied will-change: transform temporarily in DevTools only to see the stacking order. The actual fix is in the original CSS that creates the stacking context, such as the z-index of the card.

​Posted by 86