将div重叠并定位固定的问题

时间:2016-06-15 18:00:25

标签: html css css3 position

我正在查看Cnet的主页并试图复制他们的重叠下滑图层。 Click to see their homepage

enter image description here

这究竟是怎么做到的?我知道这与中间层定位固定,然后z-index和层之间的高margin-top有关,我无法弄明白。

有什么想法吗?



#blue, #green, #red {
  height: 500px;
  width: 100%;
}
#blue {
  background: blue;
  z-index: 1;
}
#green {
  background: green;
  position: fixed;
  margin-top: 200px;
}
#red {
  background: red;
  z-index: 1;
  margin-top: 500px;
}

<div id="blue"></div>
<div id="green"></div>
<div id="red"></div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

你的意思是这样吗?

position设置为非static而非z-indexhtmlk, body { margin: 0; } #blue, #green, #red { height: 200px; width: 100%; } #blue { position: relative; background: blue; z-index: 2; } #green { background: green; position: fixed; margin-top: 50px; top: 0; left: 0; z-index: 1; } #red { position: relative; background: red; z-index: 2; margin-top: 200px; }才能正常使用。

&#13;
&#13;
<div id="blue"></div>
<div id="green"></div>
<div id="red"></div>
&#13;
{{1}}
&#13;
&#13;
&#13;

相关问题