将 DIV 堆叠在一起?
是否可以堆叠多个 DIV,例如:
<div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
以便所有这些内部 DIV 具有相同的 X 和 Y 位置?默认情况下,它们都位于彼此下方,将 Y 位置增加了上一个 DIV 的高度。
我有一种感觉,某种浮动、展示或其他技巧可能会起作用?
编辑:父 DIV 具有相对位置,因此,使用绝对位置似乎不起作用。
Is it possible to stack up multiple DIVs like:
<div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
So that all those inner DIVs have the same X and Y position? By default they all go below each other increasing the Y position by the height of the last previous DIV.
I have a feeling some sort of float or display or other trick could bite?
EDIT: The parent DIV has position relative, so, using position absolute does not seem to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
根据需要放置外部 div,然后使用绝对位置放置内部 div。他们都会堆积起来。
Position the outer div however you want, then position the inner divs using absolute. They'll all stack up.
要添加到 Dave 的回答:
To add to Dave's answer:
您现在可以使用 CSS 网格来解决此问题。
css 是这样的:
z-index 越低,级别越低。 z-index 最高的关卡位于前景。
You can now use CSS Grid to fix this.
And the css for this:
The lower the z-index, the lower the level. The level with the highest z-index is in the foreground.
我更喜欢 CSS 网格以获得更好的页面布局(
absolute
div 可以被页面中的其他 div 覆盖。)如果您使用 CSS 隐藏紫色 div,您会看到蓝色 div 位于顶部。
这是一个有效的链接
I'd prefer CSS grid for a better page layout (
absolute
divs can be overridden by other divs in the page.)If you hide the purple div with CSS, you'll see the blue div is at the top.
Here's a working link
如果您的意思是将一个放在另一个的顶部,一个放在顶部(X、Y 位置相同,但 Z 位置不同),请尝试使用
z-index
CSS 属性。这应该可以工作(未经测试)这应该显示 4 在 3 的顶部,3 在 2 的顶部,依此类推。 z 索引越高,元素在 z 轴上的位置就越高。我希望这对你有帮助:)
If you mean by literally putting one on the top of the other, one on the top (Same X, Y positions, but different Z position), try using the
z-index
CSS attribute. This should work (untested)This should show 4 on the top of 3, 3 on the top of 2, and so on. The higher the z-index is, the higher the element is positioned on the z-axis. I hope this helped you :)
风格=“位置:绝对”
style="position:absolute"
我将 div 放置得稍微偏移,以便您可以在工作时看到它。
HTML
CSS
https://codepen.io/anon/pen/EXxKzP
I positioned the divs slightly offset, so that you can see it at work.
HTML
CSS
https://codepen.io/anon/pen/EXxKzP
提供
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
快速代码:
The answer that provides
position:absolute
is now outdated.In currently supported browsers, it is much easier to achieve the same using CSS grid. This will ensure that the elements are still part of the page layout (issue produced with
position:absolute
.This is a blog post explaining how to achieve this: https://zelig880.com/how-to-stack-two-elements-on-top-of-each-other-without-using-position-absolute
And this is a codepen with a live example:https://codepen.io/zelig880/pen/oNdZWNa
Quick code:
我有同样的要求,我在下面的小提琴中尝试过。
https://plnkr.co/edit/XnlneRFlvo1pB92UXCC6?p=preview
I had the same requirement which I have tried in below fiddle.
https://plnkr.co/edit/XnlneRFlvo1pB92UXCC6?p=preview
我知道这篇文章有点旧,但我遇到了同样的问题,并尝试修复它几个小时。最后我找到了解决方案:
如果我们有 2 个处于绝对位置的盒子,
我们确实希望屏幕上只有一个盒子。为此,我们必须将 margin-bottom 设置为等于 -height,因此这样做:
对我来说效果很好。
I know that this post is a little old but I had the same problem and tried to fix it several hours. Finally I found the solution:
if we have 2 boxes positioned absolue
we do expect that there will be one box on the screen. To do that we must set margin-bottom equal to -height, so doing like this:
works fine for me.
如果您遇到与影响其他元素位置相关的问题,您可以这样做。
If you have issues related to affecting other elements' positions you can do it like this.