【问题标题】:Push an item to the bottom of its parent, without overlap?将项目推到其父项的底部,没有重叠?
【发布时间】:2019-04-05 07:33:14
【问题描述】:

我有一个元素至少垂直延伸到整个视口(最小高度为 100vh)。这个元素有两个子元素,一个菜单项(基本上是一个格式化的链接列表)和另一个元素。

我想将后一个元素推送到父元素的底部,而不重叠菜单,跨越分辨率变化。

目前我正在使用位置绝对解决方案https://codepen.io/anon/pen/WGpyYa。但事实证明,“红色”元素可以在某些配置中与菜单重叠。

HTML & CSS 代码:

* {
  margin: 0;
}
.parentContainer {
  min-height: 100vh;
  background-color: yellow;
  position: relative;
}
.pushBottom {
  width: 200px;
  height: 300px;
  background-color: red;
  position: absolute;
  bottom: 0;
}
<div class="parentContainer">
  <ul>
    <li>foobar</li>
    <li>foobar</li>
    <li>foobar</li>
  </ul>
  <div class="pushBottom">
  </div>
</div>

如何将红色元素推到父“黄色”元素的底部,而不与菜单重叠?

谢谢!

【问题讨论】:

  • 看起来您想要一个可以包含不确定数量的项目的列表,但您希望红色框的大小固定。那是对的吗?如果视口足够短以至于它们重叠,您希望红色框发生什么?红框应该变小吗?列表是否应该与框重叠?这两个项目是否应该保持它们的高度并导致整个页面上的垂直滚动?

标签: html css alignment


【解决方案1】:

如果您在父容器上使用flexbox,这很容易:

  1. 删除red部分的绝对定位。

  2. display: flex 添加到pushBottom 并使用flex-direction: column 垂直弯曲。

  3. 使用justify-content: space-betweenpushBottom 保持在所有显示尺寸的底部。

* {
  margin: 0;
}
.parentContainer {
  min-height: 100vh;
  background-color: yellow;
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.pushBottom {
  width: 200px;
  height: 300px;
  background-color: red;
}
<div class="parentContainer">
  <ul>
    <li>foobar</li>
    <li>foobar</li>
    <li>foobar</li>
  </ul>
  <div class="pushBottom">
  </div>
</div>

检查一下,让我知道您对此的反馈。谢谢!

【讨论】:

  • @Phyks 如果这解决了您的问题,请告诉我...谢谢!
  • 是的,它完全符合我的用例!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-18
  • 1970-01-01
  • 2014-08-23
  • 2022-01-18
  • 1970-01-01
  • 2016-07-29
  • 2015-02-27
相关资源
最近更新 更多