将DIV堆叠在一起?

Tow*_*wer 104 html css stack css-float

是否可以堆叠多个DIV,如:

<div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
Run Code Online (Sandbox Code Playgroud)

那么所有那些内部DIV都有相同的X和Y位置?默认情况下,它们都低于彼此,将Y位置增加上一个前一个DIV的高度.

我有一种浮动或显示或其他技巧可以咬人的感觉?

编辑:父DIV具有位置相对,因此,使用绝对位置似乎不起作用.

Mat*_*att 150

根据需要定位外部div,然后使用absolute定位内部div.他们都会堆积起来.

.inner {
  position: absolute;
}
Run Code Online (Sandbox Code Playgroud)

  • 这似乎不起作用。也许我应该提到我有这种情况: &lt;div style="position:absolute..."&gt; &lt;div style="position:relative..."&gt; &lt;div&gt;stack this&lt;/div&gt; &lt;div&gt;stack这个&lt;/div&gt; &lt;div&gt;堆叠这个&lt;/div&gt; &lt;div&gt;堆叠这个&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; (3认同)
  • 您想绝对定位其中具有“堆叠此”功能的 div。它确实有效 - 我在发布我的原件之前尝试过它。如果您没有将类选择器放在 div 上,请调整 Eric 答案中的 div 选择方法以选择堆栈 div。 (2认同)

Eri*_*lin 47

添加到戴夫的答案:

div { position: relative; }
div div { position: absolute; top: 0; left: 0; }
Run Code Online (Sandbox Code Playgroud)


小智 19

您现在可以使用 CSS Grid 来解决此问题。

<div class="outer">
  <div class="top"> </div>
  <div class="below"> </div>
</div>
Run Code Online (Sandbox Code Playgroud)

以及用于此的 css:

.outer {
  display: grid;
  grid-template: 1fr / 1fr;
  place-items: center;
}
.outer > * {
  grid-column: 1 / 1;
  grid-row: 1 / 1;
}
.outer .below {
  z-index: 2;
}
.outer .top {
  z-index: 1;
}
Run Code Online (Sandbox Code Playgroud)

  • 听起来很复杂,没有解释 (3认同)

Jim*_*Lin 7

如果你的意思是将一个放在另一个的顶部,一个在顶部(相同的X,Y位置,但不同的Z位置),尝试使用z-indexCSS属性.这应该工作(未经测试)

<div>
    <div style='z-index: 1'>1</div>
    <div style='z-index: 2'>2</div>
    <div style='z-index: 3'>3</div>
    <div style='z-index: 4'>4</div>
</div>
Run Code Online (Sandbox Code Playgroud)

这应该在3的顶部显示4,在2的顶部显示3,依此类推.z指数越高,元素在z轴上的位置越高.我希望这能帮助你:)


Rid*_*set 7

所有的答案似乎都很旧:) 我更喜欢 CSS 网格以获得更好的页面布局(absolutediv 可以被页面中的其他 div 覆盖。)

<div class="container">
  <div class="inner" style="background-color: white;"></div>
  <div class="inner" style="background-color: red;"></div>
  <div class="inner" style="background-color: green;"></div>
  <div class="inner" style="background-color: blue;"></div>
  <div class="inner" style="background-color: purple;"></div>
</div>

<style>
.container {
  width: 300px;
  height: 300px;
  margin: 0 auto;
  background-color: yellow;
  /* important part */
  display: grid;
  place-items: center;
  grid-template-areas:
                  "inner-div";
}

.inner {
  height: 100px;
  width: 100px;
  /* important part */
  grid-area: inner-div;
}
</style>
Run Code Online (Sandbox Code Playgroud)

如果您使用 CSS 隐藏紫色 div,您将看到蓝色 div 位于顶部。

这是一个工作链接

  • 这是最好的答案,因为 `position:absolute;` 会导致所有边距和其他此类属性崩溃。 (5认同)
  • 添加到 @ashuvssut 的评论中,“position:absolute”通常还需要对容器的宽度和高度进行硬编码,因为子级不再对其大小做出贡献,否则会导致容器折叠到 0×0(这可能是不可取的)。 (2认同)

Dav*_*sky 5

style="position:absolute"


nyc*_*nik 5

我将div略微偏移,以便您可以在工作中看到它.
HTML

<div class="outer">
  <div class="bot">BOT</div>
  <div class="top">TOP</div>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS

.outer {
  position: relative;
  margin-top: 20px;
}

.top {
  position: absolute;
  margin-top: -10px;
  background-color: green;
}

.bot {
  position: absolute;
  background-color: yellow;
}
Run Code Online (Sandbox Code Playgroud)

https://codepen.io/anon/pen/EXxKzP