Can't get z-index to work for div inside of another div

前端 未结 2 980
猫巷女王i
猫巷女王i 2020-12-08 22:26

I\'m trying to get a drop down menu to work. It consists of an LI with a DIV that appears when the LI is hovered. I want the parent to be drawn in front of the child so that

相关标签:
2条回答
  • 2020-12-08 22:49

    I can give you a common example about Bootstrap Modals. Consider that your #test_1 is my .modal-body and your #test_2 is my custom <select> box, OK? #test_2 is inside #test_1. Simple as that.

    I just have had to make sure #test_1 overflow computed property is not "auto" or "scroll" or "hidden", otherwise, the modal would cut my <select> box listing in the middle. My current .modal-body overflow is initial

    0 讨论(0)
  • 2020-12-08 23:04

    A positioned container always contains its children. You can't have the content of a container below the container itself. See this test file that I made (about 7 years ago) showing the situation.

    Note in particular that the dark blue div is z-index:-100, but doesn't appear below its z-index:3 parent container, nor below a different z-index:2 container that is its "uncle" (sibling of its parent).

    The z-index of an element is only valid with respect to other elements using the same positioned container. Once you set position:relative on test_1 you are causing its z-index to be in a different world than the z-index of test_2. The closest you can do is set #test_2 { z-index:-1 } to cause it to appear below the content of #test_1 (but not below its background).

    0 讨论(0)
提交回复
热议问题