将 DIV 堆叠在一起?

问题描述 投票:0回答:11

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

<div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>

那么所有这些内部 DIV 都具有相同的 X 和 Y 位置吗?默认情况下,它们都在彼此下方,将 Y 位置增加到前一个 DIV 的高度。

我有一种感觉,某种漂浮物、显示器或其他技巧可能会起作用?

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

css html stack css-float
11个回答
226
投票

根据需要放置外部 div,然后使用绝对位置放置内部 div。它们都会堆积起来。

.inner {
  position: absolute;
}
<div class="outer">
   <div class="inner">1</div>
   <div class="inner">2</div>
   <div class="inner">3</div>
   <div class="inner">4</div>
</div>


65
投票

添加到戴夫的答案

div { position: relative; }
div div { position: absolute; top: 0; left: 0; }

43
投票

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

<div class="outer">
  <div class="top"> top </div>
  <div class="below"> below </div>
</div>

还有这个的CSS:

.outer {
  display: grid;
  grid-template: 1fr / 1fr;
  place-items: center;
}
.outer > * {
  grid-column: 1 / 1;
  grid-row: 1 / 1;
}
.outer .below {
  z-index: 1;
  background-color: aqua;
}
.outer .top {
  z-index: 2;
  background-color: brown;
}

z-index越低,级别越低。 z 指数最高的关卡位于前景。


19
投票

我更喜欢 CSS 网格以获得更好的页面布局(

absolute
div 可以被页面中的其他 div 覆盖。)

.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;
}
<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>

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

这是一个有效的链接


9
投票

如果您的意思是将一个放在另一个的顶部,一个放在顶部(X、Y 位置相同,但 Z 位置不同),请尝试使用

z-index
CSS 属性。这应该有效(未经测试)

<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>

这应该显示 4 在 3 的顶部,3 在 2 的顶部,依此类推。 z 索引越高,元素在 z 轴上的位置就越高。我希望这对你有帮助:)


6
投票

style="position:absolute"


4
投票

我将 div 的位置稍微偏移,以便您可以在工作时看到它。
HTML

<div class="outer">
  <div class="bot">BOT</div>
  <div class="top">TOP</div>
</div>

CSS

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

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

.bot {
  position: absolute;
  background-color: yellow;
}

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


3
投票

我有同样的要求,我在下面的小提琴中尝试过。

#container1 {
background-color:red;
position:absolute;
left:0;
top:0;
height:230px;
width:300px;
z-index:2;
}
#container2 {
background-color:blue;
position:absolute;
left:0;
top:0;
height:300px;
width:300px;
z-index:1;
}

#container {
position : relative;
height:350px;
width:350px;
background-color:yellow;
}

https://plnkr.co/edit/XnlneRFlvo1pB92UXCC6?p=preview


3
投票

提供

position:absolute
的答案现已过时。

在当前支持的浏览器中,使用 CSS 网格更容易实现相同的效果。这将确保元素仍然是页面布局的一部分(使用

position:absolute
产生的问题。

这是一篇博客文章,解释如何实现此目的:https://zelig880.com/how-to-stack-two-elements-on-top-of-each-other-without-using-position-absolute

这是一个带有实例的codepen:https://codepen.io/zelig880/pen/oNdZWNa

快速代码:

.container_row{
  display: grid;
}

.layer1, .layer2{
  grid-column: 1;
  grid-row: 1;
}

.layer1{
  color: blue;
  background: red;
  animation-direction: reverse;
}

.layer2{
  color: white;
  background: blue;
}
.layer1, .layer2 {
  animation-name: fade;
  animation-duration: 10s;
}

@keyframes fade {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
<div class="container_row">
    <div class="layer1">
        I am the layer behind
    </div>
    <div class="layer2">
        I am actually on top
    </div>
</div>
<div class="container_row">
   Yuppi! This line is positioned successfully! This would not have been the case with position:absolute
</div>


0
投票

我知道这篇文章有点旧,但我遇到了同样的问题,并尝试修复它几个小时。最后我找到了解决方案:

如果我们有 2 个盒子位于绝对位置

<div style='left: 100px; top: 100px; position: absolute; width: 200px; height: 200px;'></div>
<div style='left: 100px; top: 100px; position: absolute; width: 200px; height: 200px;'></div>

我们确实希望屏幕上会出现一个框。为此,我们必须将 margin-bottom 设置为等于 -height,因此这样做:

<div style='left: 100px; top: 100px; position: absolute; width: 200px; height: 200px; margin-bottom: -200px;'></div>
<div style='left: 100px; top: 100px; position: absolute; width: 200px; height: 200px; margin-bottom: -200px;'></div>

对我来说效果很好。


0
投票

如果您遇到与影响其他元素位置相关的问题,您可以这样做。

<div class="container">
  <div>Div 1</div>
  <div>Div 2 on top of div 1</div>
  <div>Div 3 on top of div 1 and 2</div>
</div>

<style type="text/css">
  .container {
    /* to align absolute children to relative parent */
    position: relative;
  }
  .container > div {
    /* no need for z-index, the lower div will be higher 
       use z-index if you want a specific div to be on top of others */
    position: absolute;
  }
</style>

David Antoon
© www.soinside.com 2019 - 2024. All rights reserved.