【发布时间】:2016-06-20 03:38:59
【问题描述】:
我正在尝试了解 z-index 背后的规则以及它如何与溢出属性交互。
我有这个 html:
<body>
<div class="cell">
Here is some text to keep things interesting
<div class="boxy"></div>
</div>
</body>
还有这个css:
.boxy {
position: absolute;
z-index: 9999;
top:70px;
width: 50px;
height: 50px;
background: #0FF;
}
.cell {
border: 2px solid #F00;
position: relative;
/* comment these two lines out and the box appears */
/* or change them both to 'visible' */
/* changing only one of them to 'visible' does not work */
overflow-y: auto;
overflow-x: auto;
}
我原以为青色框会出现,即使它超出了div.cell 的大小,因为它的 z-index 和位置已设置。
但是,使青色框出现的唯一方法是注释掉溢出的-x 和-y 行。
我的问题是:如何使青色框出现在屏幕上,同时将溢出保持为隐藏或自动?但更重要的是,我希望了解为什么会发生这种情况。这里应用了哪些 css 和布局规则?
See my Plunkr. 这个例子当然是我实际使用的 HTML/CSS 的简化版本。
编辑
下面的答案似乎有些混乱,因为我解释得不够好。如果将两条溢出行注释掉,可以看到出现了青色框。它出现在.cell 的边界之外。为什么会这样?如何使青色框出现,同时仍然隐藏溢出和 z-index?
【问题讨论】:
-
删除位置
-
哪个职位?为什么会这样?这里有什么规则?