简体   繁体   English

没有z-index的堆叠?

[英]Stacking without z-index?

I searched a lot for how stacking of HTML elements occurs without z-index, however could not find anything concrete. 我搜索了很多关于如何在没有z-index的情况下堆叠HTML元素,但是找不到具体的东西。 I did find that stacking without z-index occurs in the order of the appearance of the HTML. 我确实发现没有z-index的堆叠按照HTML外观的顺序发生。

https://developer.mozilla.org/en-US/docs/CSS/Understanding_z-index/Stacking_without_z-index https://developer.mozilla.org/en-US/docs/CSS/Understanding_z-index/Stacking_without_z-index

Is this specification valid for IE? 此规范对IE有效吗? I have two divs enclosed in another div. 我有两个div包含在另一个div中。 Both the divs have absolute position: 两个div都有绝对的位置:

<div>
    <div id"div1" style="position:absolute"></div>
    <div id"div2" style="position:absolute"></div> 
</div>

for both the divs, ie div1 and div2, the values of left and width are same. 对于两个div,即div1和div2, leftwidth的值是相同的。 From my understanding about the behavior of CSS here, div2 should always overlap div1. 根据我对CSS行为的理解,div2应该总是与div1重叠。 What is the possibility that 'div1' will overlap 'div2'? 'div1'与'div2'重叠的可能性是多少?

In your example, according to the document you've referenced , both div elements are positioned but without z-index, therefore having equal precedence (note: they do have a higher stacking order than unpositioned elements). 在您的示例中,根据您引用文档 ,两个div元素都已定位但没有z-index,因此具有相同的优先级(注意:它们具有比未定位元素更高的堆叠顺序)。 This means they are stacked in the order they appear in the markup, with the last declared element having the highest stacking index. 这意味着它们按照它们在标记中出现的顺序堆叠,最后声明的元素具有最高的堆叠索引。

Therefore, according to that definition, there is no chance that div1 will appear above div2 . 因此,根据这一定义,有没有机会div1上方会出现div2

See the W3C CSS2 specification on stacking context for more information. 有关更多信息,请参阅堆栈上下文中W3C CSS2规范 Note point 8 in particular: 特别注意第8点

All positioned descendants with 'z-index: auto' or 'z-index: 0', in tree order. 所有定位的后代都带有'z-index:auto'或'z-index:0',按树顺序排列。

Basically the order defined in the source is the order of stacking (not specifying z-index equates to z-index: auto ). 基本上,源中定义的顺序是堆叠顺序(不指定z-index等于z-index: auto )。

If you set the left and top properties of both the DIVs same then the second DIV will always overlap the first one. 如果将两个DIV的左侧和顶部属性设置为相同,则第二个DIV将始终与第一个DIV重叠。

Thats what I know. 多数民众赞成我所知道的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM