简体   繁体   English

如何从底部到顶部垂直堆叠div元素?

[英]How to vertically stack div elements from bottom to top?

I am trying to stack 2 div s on top of each other (that is, from bottom up). 我试图将2 div彼此堆叠(即从下至上)。 The bottom one has dynamic height, I want the top div to be stacked on it accordingly. 底部的具有动态高度,我希望顶部的div相应地堆叠在其上。

I want them to be from bottom: 0 and up. 我希望它们从bottom: 0和更高。 not from up to bottom 不是从上到下

<div> s are block level elements. <div>是块级元素。 They stack vertically by default. 默认情况下,它们垂直堆叠。

Using the Flexbox CSS properties is the only way to change the ordering of the elements in the way you desire (source order of elements is reversed from the desired display order, elements have dynamic sizes) if you're looking for a pure CSS solution. 如果您正在寻找纯CSS解决方案,则使用Flexbox CSS属性是按所需方式更改元素顺序的唯一方法(元素的源顺序与所需的显示顺序相反,元素具有动态大小)。

If one element has a fixed size (such as an image, not recommended for text elements), you could use absolute positioning: 如果一个元素具有固定大小(例如图像,不建议用于文本元素),则可以使用绝对定位:

http://jsfiddle.net/wHDWP/ http://jsfiddle.net/wHDWP/

.container {
    position: relative;
}

.top {
    border: 1px solid red;
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height:  1.5em;
}

.bottom {
    border: 1px solid blue;
    padding-bottom: 1.5em;
}

You could rearrange the elements using JS if you're desperate. 如果您不顾一切,可以使用JS重新排列元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM