z-index

The z-index is a CSS property that controls the stacking order of overlapping elements: a higher z-index sits in front of a lower one.
Design
Created on
23.08.2026

Summarize this

The z-index is a CSS property that controls the stacking order of elements that overlap on a page. An element with a higher z-index appears in front of one with a lower value. It answers a simple question: when two things sit on top of each other, which one is visible?

How stacking works

z-index only applies to positioned elements (those with a position other than static) and to flex or grid children. Values can be positive or negative, and the browser paints higher values on top.

  • .modal { position: fixed; z-index: 1000; }
  • Without a z-index, elements paint in the order they appear in the HTML.

The painting order

The browser paints in layers: first backgrounds, then non-positioned blocks, then floats, then positioned elements ordered by z-index. When two elements share the same z-index, the one later in the HTML wins.

Stacking contexts

A stacking context is a group of layers that stack together as a unit. Several properties silently create one, which is the usual source of confusion.

TriggerExample
Positioned element with z-indexposition: relative; z-index: 1
Opacity below 1opacity: 0.9
Transformtransform: translateZ(0)
Filterfilter: blur(2px)
will-changewill-change: transform

Why z-index fails

Most problems come from a missing position or from a parent that created a stacking context and trapped the child inside it. A z-index of 9999 cannot escape its parent's context, so it still sits behind a sibling of that parent with a higher value. Inside a context, child z-index values are only compared with each other.

Best practices

  • Only add z-index where elements actually overlap.
  • Use a small, documented scale (for example 10, 100, 1000) instead of arbitrary large numbers.
  • Keep track of which components create a stacking context (transforms, opacity, filters), so surprises are easy to diagnose.

z-index at BeBranded

At BeBranded we keep stacking predictable with a clear layering scale, so menus, modals and sticky bars never fight each other. It is part of the tidy, maintainable front-end we deliver through our design work.

FAQ

The z-index is a CSS property that sets the stacking order of overlapping elements. A higher z-index appears in front of a lower one.
A stacking context is a group of layers that stack as a unit. Properties like a positioned element with a z-index, an opacity below 1, or a transform create one. Child z-index values only compete within the same context.
Usually because the element has no position (it must be non-static), or because a parent created a stacking context that traps the element. Check the parent's opacity, transform and z-index.
Yes. A negative z-index places an element behind others in the same stacking context, which is useful for background layers.
For most elements, yes: z-index applies to positioned elements (position other than static). Flex and grid children are exceptions where z-index also works.
The practical maximum is very large (2147483647), but using huge values is a bad habit. A small, consistent scale is easier to maintain.

Ready to boost your conversions?

Our team is here to understand your needs & work with you to create your next projects.
Get news, infos and resources.
Actionable tips delivered straight to your inbox.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.