css - 堆栈上下文 : absolute positioning and floats

标签 css css-float z-index

This MDN article解释堆叠上下文和 float 。这是他们提供的示例:

div {
  font: 12px Arial;
}
span.bold {
  font-weight: bold;
}
#absdiv1 {
  opacity: 0.7;
  position: absolute;
  width: 150px;
  height: 200px;
  top: 10px;
  right: 140px;
  border: 1px dashed #990000;
  background-color: #ffdddd;
  text-align: center;
}
#normdiv {
  /* opacity: 0.7; */
  height: 100px;
  border: 1px dashed #999966;
  background-color: #ffffcc;
  margin: 0px 10px 0px 10px;
  text-align: left;
}
#flodiv1 {
  opacity: 0.7;
  margin: 0px 10px 0px 20px;
  float: left;
  width: 150px;
  height: 200px;
  border: 1px dashed #009900;
  background-color: #ccffcc;
  text-align: center;
}
#flodiv2 {
  opacity: 0.7;
  margin: 0px 20px 0px 10px;
  float: right;
  width: 150px;
  height: 200px;
  border: 1px dashed #009900;
  background-color: #ccffcc;
  text-align: center;
}
#absdiv2 {
  opacity: 0.7;
  position: absolute;
  width: 150px;
  height: 100px;
  top: 130px;
  left: 100px;
  border: 1px dashed #990000;
  background-color: #ffdddd;
  text-align: center;
}
<br />
<br />

<div id="absdiv1">
  <br /><span class="bold">DIV #1</span>
  <br />position: absolute;
</div>

<div id="flodiv1">
  <br /><span class="bold">DIV #2</span>
  <br />float: left;
</div>

<div id="flodiv2">
  <br /><span class="bold">DIV #3</span>
  <br />float: right;
</div>

<br />

<div id="normdiv">
  <br /><span class="bold">DIV #4</span>
  <br />no positioning
</div>

<div id="absdiv2">
  <br /><span class="bold">DIV #5</span>
  <br />position: absolute;
</div>

在示例中,div#1 应属于类别 5(后代定位元素),div#3 应属于类别 3( float block )。所以 div#1 应该在 div#3 之上。为什么在例子下面?

最佳答案

堆叠顺序和不透明度

堆叠顺序受 opacity 属性的影响。这在 article you linked to 中有说明。 :

Note: In the example below, all the blocks except the non-positioned one, are translucent show the stacking order. If the opacity of the non-positioned block (DIV #4) is reduced, then something strange happens: the background and border of that block pops up above the floating blocks, but still under positioned blocks. I was not able to understand whether this is a bug or a peculiar interpretation of the specifications. (Applying opacity could implicitly create a stacking context.)

验证这个理论

让我们减少标记以仅包含有问题的两个 div。这是我们看到的场景;绝对定位的 div1div3 下面:

Screenshot 1

div {
  padding: 20px;
}
#div1 {
  opacity: 0.7;
  position: absolute;
  width: 150px;
  height: 200px;
  top: 100px;
  left: 50px;
  border: 1px dashed #990000;
  background-color: #ffdddd;
  text-align: center;
}
#div3 {
  opacity: 0.7;
  margin: 0px 20px 0px 10px;
  float: left;
  width: 150px;
  height: 200px;
  border: 1px dashed #009900;
  background-color: #ccffcc;
  text-align: center;
}
<div id="div1">
  DIV #1
  <br>position: absolute;
</div>



<div id="div3">
  DIV #3
  <br>float: right;
</div>

现在,让我们删除 opacity 属性(特别是 div3 上的属性)。堆叠顺序现在如您所愿:

Screenshot 2

div {
  padding: 20px;
}
#div1 {
  /*opacity: 0.7;*/
  position: absolute;
  width: 150px;
  height: 200px;
  top: 100px;
  left: 50px;
  border: 1px dashed #990000;
  background-color: #ffdddd;
  text-align: center;
}
#div3 {
  /*opacity: 0.7;*/
  margin: 0px 20px 0px 10px;
  float: left;
  width: 150px;
  height: 200px;
  border: 1px dashed #009900;
  background-color: #ccffcc;
  text-align: center;
}
<div id="div1">
  DIV #1
  <br>position: absolute;
</div>



<div id="div3">
  DIV #3
  <br>float: right;
</div>

阅读更多

Here is an in-depth write-up关于 opacity 属性如何影响堆叠顺序。

关于css - 堆栈上下文 : absolute positioning and floats,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33160724/

相关文章:

html - 分配正的可用空间时扩展元素的高度(无 JS)

css - 2 Column Div 在父级之外左右浮动子 div

javascript - 自动将 float div 的宽度固定为图像大小

html - 使元素与 float 元素的边框相邻

css - 将 Z-Index 应用于 DropDownTree 不会影响它

android - 如何在不调用 bringToFront() 的情况下将 View 置于最前面?

internet-explorer-8 - Z-index 在 IE8 中损坏?

html - meteor css 动画与之前/之后

css - 媒体查询不适用于大型移动设备(平板电脑)

html - float 两个 50% 宽度的 div,它们之间有 10px 的边距