Z-index: How to make nested elements appear underneath their parent element

跟風遠走 提交于 2019-12-22 06:59:20

问题


This fiddle should demonstrate the issue:

http://jsfiddle.net/5sqxQ/2/

I want the sub menu to appear underneath the parent menu. I was then was looking to extend this with JavaScript to slide the menu from underneath on hover of the parent li element.

Not fussed about the JavaScript but can't figure out how to style the elements to achieve the desired layering.


回答1:


You don't.

Instead, make the a be the "Sign In" "button".

Something like this: http://jsfiddle.net/5sqxQ/15/




回答2:


It doesn't work because you are applying a z-index to the parent element which makes the child element stack relative to the other elements within the parent.

Once you assign an element a value for z-index (other than auto), that element establishes its own local stacking context. This means that all of the element's descendants have their own stacking order, relative to the ancestor element.

So if the parent has z-index: 9 and the child is z-index: 8, it's kind of like assigning a z-index of 9, 8

See the article here for a better explanation.

If you remove the z-index on the parent and set the sibling element to z-index: -1, that should work. I'm not sure about all browsers, but it works in Chrome anyway.

Here is the updated fiddle

Hope that helps.




回答3:


Try setting the parent and siblings containers position to relative. Its worked for me before.




回答4:


Look at the rules below for how elements are stacked and you can easily come up with your own solution.

ex. Like thirtydot said, give "Sign In" .users > li > a a position, relative or absolute, and z-index: 1

The Stacking order and stacking context rules below are from this link

Stacking Order within a Stacking Context

The order of elements:

  1. The stacking context’s root element (the <html> element is the only stacking context by default, but any element can be a root element for a stacking context, see rules below)
    • You cannot put a child element behind a root stacking context element
  2. Positioned elements (and their children) with negative z-index values (higher values are stacked in front of lower values; elements with the same value are stacked according to appearance in the HTML)
  3. Non-positioned elements (ordered by appearance in the HTML)
  4. Positioned elements (and their children) with a z-index value of auto (ordered by appearance in the HTML)
  5. Positioned elements (and their children) with positive z-index values (higher values are stacked in front of lower values; elements with the same value are stacked according to appearance in the HTML)

When a Stacking Context is Formed

  • When an element is the root element of a document (the <html> element)
  • When an element has a position value other than static and a z-index value other than auto
  • When an element has an opacity value less than 1
  • Several newer CSS properties also create stacking contexts. These include: transforms, filters, css-regions, paged media, and possibly others. See https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context
  • As a general rule, it seems that if a CSS property requires rendering in an offscreen context, it must create a new stacking context.


来源:https://stackoverflow.com/questions/6493865/z-index-how-to-make-nested-elements-appear-underneath-their-parent-element

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!