Why is my z-index not working?

Damaris Göbel
1 min readMay 13, 2021

--

The z-index property define the stack order of an element.

An element with greater stack order is always in front of an element with a lower stack order.

Not setting a position: fixed, relative, absolute or sticky for an element.

z-index requires an element’s positioning to be either fixed, relative, sticky or absolute.

Positioning Recap:

HTML elements are positioned static by default. In the following codepen, you will find all the positioning possibilities in CSS.

Stacking context

What you already know is that an element with a higher z-index gets placed above elements with lower z-indexes. If a parent element is positioned below another parent element, the child elements can’t go higher than their parent element.

When elements are on the same level, one element will still be in front of the other one, because they have a default order in terms of the z-axis. This is called the stacking order

Stacking Context example:

Further information here: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context

z-index and Flexbox

You can apply z-index to flex item elements even if they are not position: absolute, relative, sticky or fixed.

Remember:

  • z-index only works on positioned elements (position: absolute, position: relative, position: fixed, or position: sticky)
  • child elements can’t go higher than their parent element
  • You can apply z-index to flex items, even if they have position: static.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Damaris Göbel
Damaris Göbel

Written by Damaris Göbel

I like to do crazy things with CSS & JavaScript. My brain occasionally runs out of memory so I need to write down my thoughts.

No responses yet

Write a response