Reputation: 2406
My problem is that the green child element does not stick the bottom even though it's positioned absolute with bottom:0.
The hello world text in the container is making the container scrollable and making the green element not able to stick to the bottom.
Edit: The container needs position: fixed. (Its a shopping cart modal).
.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>
Upvotes: 2
Views: 2369
Reputation: 21
use position:sticky
and also give some bottom margin so that the element won't hide.
Upvotes: 1
Reputation: 71
Maybe position: sticky
on the child works for you? This way it will not cover the bottom of the container which you would have to take care of with padding
when using position: absolute
.
.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>
Upvotes: 2
Reputation: 602
position: sticky
does the trick.
However, you also need to add bottom: 8px
to prevent it from being half hidden
.container {
background: red;
height: 100%;
width: 100%;
position: fixed;
overflow: auto;
}
.child {
background: green;
position: sticky;
bottom: 8px;
left: 0px;
right: 0px;
}
<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>
<div class='child'>
footer preview
</div>
<div style='background: green'>rest of footer<br>  </div>
</div>
Upvotes: 1