简体   繁体   English

如何使div填充绝对底部div的剩余空间

[英]How to make a div fill the remaining space of an absolute bottom div

I'm actually having 2 divs that are superimposed in a parent container. 我实际上有2个div叠加在父容器中。 The second div is absolute and is set to the bottom of the container. 第二个div是绝对的,设置在容器的底部。 I want now to make the first div's height fill the remaining upper space left by the absolute one. 我现在想让第一个div的高度填充绝对值剩下的剩余上部空间。 I've looked it up but only found solutions to when the absolute div is at the top and not at the bottom. 我查了一下,但只找到了绝对div在顶部而不是底部的解决方案。 How should I do it ? 我该怎么办?

 .container { height: 100px; position: relative; background-color: red; } #row1 { background-color: blue; overflow: auto; } #row2 { background-color: yellow; position: absolute; width: 100%; bottom: 0; } 
 <div class="container"> <div id="row1"> Row 1 content </div> <div id="row2"> Row 2 content </div> </div> 

Here is my code snippet for more clarity: https://jsfiddle.net/ekm7y1nz/ 这是我的代码片段,以便更清晰: https//jsfiddle.net/ekm7y1nz/

EDIT: I'm afraid the row2 has to be absolute because I shouldn't have to scroll down to see it if the row1's content is too long for example. 编辑:我担心row2必须是绝对的,因为如果row1的内容太长,我不应该向下滚动查看它。 Only row1 should be scrollable if it's too long. 如果row1太长,则只能滚动它。

Personally I'd just go the flex route instead. 就个人而言,我只是去灵活路线。 Cheers 干杯

 .container { height: 100px; display: flex; flex-direction: column; background-color: red; } #row1 { background-color: blue; flex: 1 0 auto; } #row2 { background-color: yellow; } 
 <div class="container"> <div id="row1"> Row 1 content </div> <div id="row2"> Row 2 content </div> </div> 

if your problem is just scrolling you can change row1's css to this : 如果您的问题只是滚动,您可以将row1的css更改为:

#row1 {
  background-color: blue;
  height: 100%;
  width:100%;
  overflow:auto;
}

the page will scroll when text gets bigger than it's div parent. 当文本变得大于它的div父级时,页面将滚动。

alse you can set row2's div height based on row1 with js or css frameworks like sass if your interested in using them. 另外,如果您对使用它们感兴趣,可以使用js或css框架(如sass)基于row1设置row2的div高度。

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

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