If you want to do only with CSS you can use these techniques.
Option 1:
The technique with flexbox
is to reverse the direction of the content of the div
using flex-direction: column-reverse
then the content always starts down and gets pushed up.
See the example to better understand:
.container {
height: 100px;
overflow: auto;
display: flex;
flex-direction: column-reverse;
border: 1px solid;
}
<div class="container">
<div> <!-- essa div na verdade começa de baixo pra cima -->
<div class="inner">Primeira mensagem</div>
<div class="inner">oi</div>
<div class="inner">oi</div>
<div class="inner">oi</div>
<div class="inner">oi</div>
<div class="inner">oi</div>
<div class="inner">oi</div>
<div class="inner">oi</div>
<div class="inner">oi</div>
<div class="inner">oi</div>
<div class="inner">oi</div>
<div class="inner">Última menssagem</div>
</div>
</div>
OBS: This solution does not work in IE...
Option 2:
This is a rotated div to stay upside down, then use direction:rtl
to return the bar of scroll
right and text-align:left
to adjust the text. Then the content of the messages is rotated upside down.
The newest message always ends at the bottom of the scroll.
.scrollable {
transform-origin: 50% 50%;
transform: rotate(180deg);
border: 1px solid;
width: 120px;
height: 120px;
overflow-y: auto;
direction: rtl;
text-align: left;
}
.message {
padding: 0.5em;
}
.messages_wrap {
float: left;
width: 100%;
transform: rotate(180deg);
}
<div class="scrollable">
<div class="messages_wrap">
<div class="message">Your message1</div>
<div class="message">Your message2</div>
<div class="message">Your message3</div>
<div class="message">Your message4</div>
<div class="message">Your message5</div>
<div class="message">Your message6</div>
<div class="message">Your message7</div>
<div class="message">Your message8</div>
<div class="message">Your message9</div>
</div>
</div>
If I understand your doubt, edit and add the tag "javascript" and "jquery".
– MagicHat