Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for CSS child behind parent with z-index
Sabbir Sobhani
Sabbir Sobhani

Posted on • Edited on

     

CSS child behind parent with z-index

z-index only works on positioned elements (position: absolute, position: relative, position: fixed, or position: sticky) and flex items (elements that are direct children of display:flex elements).w3school

Child behind Parent

parent: Not set anyz-index(it's important), set positionrelative (by default though it's relative).

child: Give any positional value(absolute, sticky or fixed), andz-index any negative integer value.

Now, the child is behind the parent element. ✅

Scenario

  1. If you have a situation so to make the parent elementfixed,sticky orabsolute then what?

Simple, since child element will not work withz-index: -1; or behind parent, if parent is other thanrelative. Just wrap the parent element with any tag and make itfixed,sticky orabsolute.

<divclass="wrapper fixed-or-sticky-or-absolute"> 👈wrapper tag<divclass="parent relative"><divclass="child any-position-will-work z-index(-1)"></div></div></div>
Enter fullscreen modeExit fullscreen mode

Moreover, you can now give anyz-index value to thewrapper element tag and it will not affect the child and parent.

Done.

Buy Me a Coffee 🙏:
If this article helped you a bit and you'd like to support my work, feel free to buy me a coffee:https://buymeacoffee.com/sobhani ☕️ Thanks for keeping me motivated!

Follow me on Twittersabbirsobhani

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
architchandra profile image
Archit Chandra
I'm an independent front-end developer from Delhi, India.
  • Location
    Delhi, India
  • Education
    Netaji Subhas Institute of Technology, Delhi
  • Work
    Founder and C3PO, Greythink Labs
  • Joined

I was facing a problem where this exact fix helped me. Why is it important to omit the z-index from the parent? Can you also add a reference to the documentation where this is mentioned? Thanks.

CollapseExpand
 
sabbirsobhani profile image
Sabbir Sobhani
Focusing 🎯
  • Education
    Bachelor in Computer Science and Engineering
  • Pronouns
    He/Him
  • Work
    Software Engineer
  • Joined

Good to know it helps!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Focusing 🎯
  • Education
    Bachelor in Computer Science and Engineering
  • Pronouns
    He/Him
  • Work
    Software Engineer
  • Joined

More fromSabbir Sobhani

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp