html - 如果容器可滚动,如何将元素粘贴到其容器底部?

标签 html css css-position fixed absolute

我的问题是绿色子元素不粘在底部,即使它是绝对定位的:0。

容器中的 hello world 文本使容器可滚动,并使绿色元素无法粘在底部。

编辑:容器需要位置:固定。 (它是一个购物车模式)。

.container {
 background: red;
 height: 100%;
 width: 200px;
 position: fixed;
 overflow: auto;
}

.child {
 background: green;
 position: absolute;
 bottom: 0;
 left: 0;
 right: 0;
}
<div class='container'>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>

v
<div class='child'>
asdf
</div>

</div>

最佳答案

也许 child 身上的position:sticky对你有用?这样,它就不会覆盖容器的底部,而在使用 position:absolute 时,您必须使用 padding 来处理容器的底部。

.container {
 background: red;
 display: flex;
 flex-direction: column;
 height: 100%;
 width: 200px;
 position: fixed;
 overflow: auto;
}

.container__inner {
  flex: 1
}

.child {
 background: green;
 position: sticky;
 bottom: 0;
 left: 0;
 right: 0;
}
<div class='container'>
<div class='container__inner'>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>
<div>hello world </div>

v
</div>
<div class='child'>
asdf
</div>

</div>

关于html - 如果容器可滚动,如何将元素粘贴到其容器底部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68190109/

相关文章:

javascript - 多选元素并删除元素和文档单击中的属性?

html - 如何强制分隔线的位置,并将 Logo 放置在特定位置?

html - 绝对定位元素在 Chrome 中的位置与其他浏览器不同

css - IE6 和 IE7 Z-INDEX 错误

javascript - 尝试让两张图片根据页面宽度调整大小

javascript - 使用 Jquery 弹出滑动菜单

html - 为表数据创建样式 css 到 tr

html - Ul , Ol 不显示数字和圆圈

html - 在没有强制分页符的情况下打印 HTML

css - 使用 Bootstrap block 重排