简体   繁体   English

如何在不更改当前 HTML 结构的情况下使用 css 将底部元素对齐(不覆盖)顶部元素

[英]How do I align bottom element above (not overlay) top element using css without changing the current HTML structure

I want to align the bottom element in an HTML file on top of the parent element, I do not want to overlay it, I literally want the bottom element to be on top of the top element:我想在父元素顶部对齐 HTML 文件中的底部元素,我不想覆盖它,我真的希望底部元素位于顶部元素的顶部:

Example: HTML:示例:HTML:

<div class="top-container"></div>
<div class="bottom-container"></div>

CSS: CSS:

.top-container {
    width: 200px;
    height: 200px;
    background-color: green;
}

.bottom-container {
    width: 200px;
    height: 200px;
    background-color: red;
}

Current Output:当前 Output:

在此处输入图像描述

Expected output: I want the red box to be above the green box.预期 output:我希望红框在绿框上方。

If you can wrap the two elements in a div, if they are not already wrapped, then in the container element, display: flex; flex-direction:column-reverse;如果可以将两个元素包裹在一个div中,如果还没有包裹,那么在容器元素中, display: flex; flex-direction:column-reverse; display: flex; flex-direction:column-reverse; ... if there's really no container element, your best bet is position: relative on the bottom element and bottom: 400px . ...如果真的没有容器元素,你最好的选择是position: relative on the bottom element and bottom: 400px They won't switch spots, but the red one will be ontop of the green one.他们不会交换位置,但红色的会在绿色的上面。

暂无
暂无

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

相关问题 如何在容器底部元素的正上方对齐光滑的 slider? - How do I align slick slider directly above the bottom element in my container? 如何使用 vanilla javascript 判断元素的顶部或底部何时滚动到页面顶部 - How do I tell when the top or bottom of an element scrolls to the top of the page using vanilla javascript 如何使用 CSS 使元素粘在页面的底部和顶部 - How to make an element sticky to both bottom and top of page using CSS 使用CSS从头到尾排列元素 - arrange element bottom to top using CSS 我如何使用JQuery从底部到顶部(最嵌套的元素首先)对表单进行迭代,而无需事先知道最深元素的ID? - How do I use JQuery to iterate a form from the bottom to the top, most nested element first without knowing the id of the deepest element beforehand? 如何使用JavaScript滚动到叠加层中的元素顶部? - How to scroll to top of element in an overlay using JavaScript? 如何使用JS将元素对齐到窗口底部 - How to align an element to the bottom of the window using JS 我怎样才能让它显示这个元素的底部? (HTML, CSS, JS) - How can i make it show the bottom of this element? (HTML, CSS, JS) 如何计算当前显示分辨​​率的最高值与HTML DOM中td元素的最低位置之间的大小/间距? - How to calculate the size / the space between the top of current display resolution and the bottom position of a td element in HTML DOM? 如何使用简单的JavaScript / CSS将元素放在DOM中任何给定元素的上方? - How do I put an element above any given element in the DOM with simple JavaScript/CSS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM