2011-12-13 42 views
18

看到这个评论from jquery-ui为什么z-index忽略位置:static?

// Ignore z-index if position is set to a value where z-index is ignored by the browser 
// This makes behavior of this function consistent across browsers 
// WebKit always returns auto if the element is positioned 

我看到jQuery的zIndex()返回0,如果该元素是position: static

z-index是否支持position:static? (它为我在Chrome中,还没有测试跨浏览器)

+0

可能重复http://stackoverflow.com/questions/7814282/why-is-my-z-index-being-ignored – Dve

+2

@Dve - 不是真的。这个问题的答案是“它不适用于静态位置的元素。”我问**为什么**。 – ripper234

回答

1

z-index也是不可忽视的Flex的项目(柔性容器的直接子,元件相display: flexdisplay: inline-flex)。从w3c flexbox spec

Flex items漆完全一样,内嵌块CSS21,除了order修饰的文档顺序代替原文档顺序使用,并z-index值以外auto创建堆叠环境即使positionstatic

所以假设我们有该布局具有重叠(.flex-item-two使用例如负边缘重叠.flex-item-one):

.flex { 
 
    display: flex; 
 
    align-items: center; 
 
} 
 

 
.flex-item-one { 
 
    width: 100px; 
 
    height: 100px; 
 
    background-color: red; 
 
    margin-right: -50px; 
 
} 
 

 
.flex-item-two { 
 
    width: 200px; 
 
    height: 200px; 
 
    background-color: green; 
 
}
<div class="flex"> 
 
    <div class="flex-item flex-item-one">One</div> 
 
    <div class="flex-item flex-item-two">Two</div> 
 
</div>

如果flex-item-one指数是大于.flex-item-two的,.flex-item-one然后重叠.flex-item-two

.flex { 
 
    display: flex; 
 
    align-items: center; 
 
} 
 

 
.flex-item-one { 
 
    width: 100px; 
 
    height: 100px; 
 
    background-color: red; 
 
    margin-right: -50px; 
 
    z-index: 1; 
 
} 
 

 
.flex-item-two { 
 
    width: 200px; 
 
    height: 200px; 
 
    background-color: green; 
 
}
<div class="flex"> 
 
    <div class="flex-item flex-item-one">One</div> 
 
    <div class="flex-item flex-item-two">Two</div> 
 
</div>