Interesting one this. I tried some tests below, but not using a <div>
as it's not a semantic approaching putting a div inside a list.
The problem positioning something absolute inside a scrollable list, is that the positioning sticks to the loaded scroll position. Annoyingly.
I can't see a way round this unless you move your message outside the list. I may be wrong, someone else might find a solution for you.
I'm assuming you cant use position fixed as it will slam your message to the bottom of the window.
UL {
position: relative;
padding: 0 0 30px 0;
background: gainsboro;
list-style: none;
height: 100px;
overflow-y: scroll;
overflow-x: hidden;
}
LI {
display: block;
height: 30px;
padding: 0 5px;
line-height: 30px;
border-bottom: 1px solid white;
}
.pseudo-option::after {
content: 'Message always shown at the bottom';
display: block;
position: absolute;
background: yellow;
bottom: 0;
height: 30px;
padding: 0 5px;
line-height: 30px;
left: 0;
right: 0;
overflow: hidden;
}
.last-child-option LI:last-child {
display: block;
position: absolute;
background: yellow;
bottom: 0;
height: 30px;
padding: 0 5px;
line-height: 30px;
left: 0;
right: 0;
overflow: hidden;
}
DIV {
padding: 5px;
width: calc(50% - 10px);
float: left;
}
H4 {
margin-top: 0;
margin-bottom: 5px;
}
<div>
<h4>Pseudo Option</h4>
<ul class="pseudo-option">
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
</ul>
</div>
<div>
<h4>Last Child Option</h4>
<ul class="last-child-option">
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>Message always shown at the bottom</li>
</ul>
</div>
If you using jQuery you could do something like this, is by having a custom message in a data
attribute and adding it after the list and using css to target the message div. Just an idea as cant see away to do this with pure css.
$(document).ready(function() {
// each desired list to add message
$('UL').each(function(index) {
// insert div imediately after with message from data attribute
$('<div>' + $(this).data('message') + '</div>').insertAfter(this);
// add show message class to list to update list bottom margin and style message
$(this).addClass('show-message');
});
});
UL {
position: relative;
padding: 0;
background: gainsboro;
list-style: none;
height: 100px;
overflow-y: scroll;
overflow-x: hidden;
margin: 0;
}
LI {
display: block;
height: 30px;
padding: 0 5px;
line-height: 30px;
border-bottom: 1px solid white;
}
/* hide immediate div after list */
UL + DIV {
display: none;
}
/* when show message class is added remove list bottom margin */
UL.show-message {
margin-bottom: 0;
}
/* show message list class for immediate following div */
UL.show-message + DIV {
position: relative;
display: block;
background: yellow;
height: 30px;
padding: 0 5px;
line-height: 30px;
overflow: hidden;
margin-bottom: 15px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<h4>jQuery Option</h4>
<ul data-message="Message always shown at the bottom">
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
</ul>
<ul data-message="Another list Message always shown at the bottom">
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
<li>some contents</li>
</ul>
</div>