Z-Index

Z-index is a CSS property that decides which elements appear in front when two or more overlap on screen. A higher number brings an element forward, but only against the elements it shares a stacking context with.

How stacking contexts work

A stacking context is a group of elements that get layered together, as one unit. The page itself is the first one. A new one starts whenever an element is positioned and given a z-index, and also when it has an opacity below 1, a transform, or a filter. Sticky and fixed elements always start one.

Inside a stacking context, the numbers only rank the children against each other. So a tooltip inside a card can be set to 9999, but if the card sits at 1, the tooltip can never rise above a header at 10.

The property also needs something to work on. It only affects positioned elements (relative, absolute, fixed or sticky) and items inside a flex or grid layout. On a plain static element, the number does nothing.

How our own header and menus stack

The header on this page, its menus, and the birds flying behind it all follow one small scale of layers.

  1. src/styles/site.css
    .site-sky { position: fixed; inset: 0; z-index: 30; width: 100%; height: 100%; pointer-events: none;}
    The birds sit over the page

    The birds in our background are drawn on one canvas, fixed over the page at 30. It doesn't take clicks, so the links and buttons under it still work.

  2. src/styles/site.css
    .site-header { position: sticky; top: 0; z-index: 50; background: var(--bg); border-bottom: 1px solid var(--line);}
    The header sits above the birds

    The sticky header sits at 50, which keeps it above the birds and the BirdAI button at 40. Being sticky with a z-index also makes the header a stacking context of its own.

  3. src/styles/site.css
    .small-panel { position: absolute; top: calc(100% + (var(--header-bar) - 48px) / 2); left: -24px;…
    The menus open from inside the header

    Our dropdown menus don't have a z-index at all. They sit inside the header, so they open over the page at the header's 50. Only the cookie notice and the skip link sit higher, at 100.

Common z-index problems

Most of them turn up in the same places. A dropdown menu opens behind the next section, a sticky header slides under a carousel, or a pop-up appears beneath the cookie banner.

The usual cause is a parent element that has started its own stacking context, often through a transform or an animation. That's why raising the number to 99999 rarely helps. The fix is to find the parent that's trapping the element, then move the element or change the parent.

It helps to agree a small scale for the whole site and write it down, such as 10 for sticky parts, 50 for the header, and 100 for anything that has to sit over everything else. Leaving gaps between the numbers means a new layer can slot in later without renumbering the rest.

More web design glossary terms

These web design and development glossary terms cover how websites are built, hosted, secured and used on every screen. Each definition shows the idea in a diagram with a practical example, so briefs, quotes and conversations with your web team are easier to follow.