How to align <li> items from a <ul> list below?

Asked

Viewed 10,243 times

5

The idea is to create a chat similar to skype, Whatsapp, etc... Where messages start to appear from bottom to top until they reach ul height so the scroll bar appears.

    ul {
        list-style: none;
        height:380px;
        width:500px;
        overflow-y:auto;
        padding:5px;
        background-color:#ccc;
    }
    ul>li {
        height:80px;
        margin-bottom:5px;
        width:100%;
        background-color:#888;
    }
    <ul>
        <li>111111111111</li>
        <li>222222222222</li>
    </ul>

  • Do you need the read that represents a user’s message to be in the left margin of the current user with the right margin? Just like in Whatsapp? What kind of alignment exactly?

  • Hello @Guilhermenascimento ! What I need to do is just align the message blocks below, so that when the first chat message appears, it will appear below and then go up as new messages appear. As for the question of aligning the messages on the right and left, there is no problem.

  • you want to <li>222222222222</li> appear above <li>111111111111</li>?

4 answers

5

I do so using the properties display:table and display:table-Cell and vertical-align:bottom

display:table - Parent Element DIV

display:table-Cell - Child Element UL with vertical-align:bottom

div {
  display: table;
  width: 100%;
}
ul {
  list-style: none;
  height: 340px;
  width: 100%;
  padding: 5px;
  background-color: #ccc;
  display: table-cell;
  vertical-align: bottom;
}
ul>li {
  height: 80px;
  width: 100%;
  background-color: #888;
}
<div>
  <ul>
    <li>111111111111</li>
    <li>222222222222</li>
  </ul>
</div>

4

Do this, in case you need the container of ul to have the exact size of 380px as in the example, you will have to have a div on the outside with this measure for it:

ul {
    list-style: none;
   max-height:380px;
    width:500px;
    overflow-y:auto;
    padding:5px;
    background-color:#ccc;

     position: absolute;
    bottom:0;   
}
ul>li {
    height:80px;
    margin-bottom:5px;
    width:100%;
    background-color:#888;
}
<ul>
    <li>111111111111</li>
    <li>222222222222</li>
</ul>

4


You create a Javascript like this:

function updateScroll() {
    var element = document.getElementById("teste");
    element.scrollTop = element.scrollHeight;
}
updateScroll();

var timerId = window.setInterval(1, 1000);

setTimeout(function () {
    clearInterval(timerId)
}, 10000);

In CSS you don’t need to change anything, but you can use the following to hide the bar (but it will still work)

ul::-webkit-scrollbar{
    display: none;

}

Just put an id in ul and put the name of that id in Javascript instead.

Link Jsfiddle

function updateScroll() {
    var element = document.getElementById("teste");
    element.scrollTop = element.scrollHeight;
}
updateScroll();

var timerId = window.setInterval(1, 1000);

setTimeout(function () {
    clearInterval(timerId)
}, 10000);
ul {
    list-style: none;
   max-height:380px;
    width:500px;
    overflow-y:auto;
    padding:5px;
    background-color:#ccc;

     position: absolute;
    bottom:0;   
}
ul>li {
    height:80px;
    margin-bottom:5px;
    width:100%;
    background-color:#888;
}


ul::-webkit-scrollbar{
    display: none;

}
<ul id="teste">
    <li>111111111111</li>
    <li>222222222222</li>
    <li>222222222222</li>
    <li>222222222222</li>
    <li>222222222222</li>
    <li>222222222222</li>
    <li>222222222222</li>
    <li>222222222222</li>
    <li>222222222222</li>
    <li>222222sasa222222</li>
    <li>222222222222</li>
    <li>222222222222</li>
    <li>22asa</li>
    <li>222222sasa222222</li>
    <li>222222sasa222222</li>
    <li>222222sasa222222</li>
   <li>222222sasa222222</li>
<li>222222sasa222222</li>
    
    <li>FINAL</li>
    
</ul>

  • Hello @Leonardovilarinho ! Very good, it worked as expected... thanks

4

Alexander, for vc do this chat vc have to work with PHP type language and database like MYSQL, as they will be responsible for storing and show on the screen the other person’s response.

But the functional part where you send it looks something like this:

function sendMsg(){
  var send = $('.send_msg').val();
  if(send){
    $('.display_msg').append('<li>' + send + '</li>');
     $('.send_msg').val('');
     $('.display_msg').scrollTop($('.display_msg').height()); // FALTOU ESTE TRECHO
  }
}
ul {
        list-style: none;
        height:380px;
        width:500px;
        overflow-x:auto;
        padding:5px;
        border: 1px solid #333;
    }
    ul>li {
        margin-bottom:5px;
        background-color:#888;
        padding: 10px;
    }
    ul>li.receive{
        background-color:#ddd;        
    }
    .send_msg{
        border: 1px solid #333;
        height: 50px;
        width: 500px;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul class="display_msg">
    <li class="send">111111111111</li>
    <li class="receive">222222222222</li>
</ul>
<textarea class='send_msg'></textarea><br>
<button type="button" onclick="sendMsg()">Enviar</button>

  • This makes it work, but does not solve what he asked: how to align the items in the base of <ul>

  • Certainly would not.... since I forgot to put an excerpt of the code, thanks bfavaretto. Will now this is what you need Alexander???

  • I liked it a lot, I put a simpler (I’m new to the language), have some site/ channel to recommend to learn more about the language?

  • I only used CSS, HTML and Jquery... this has in the net lots.. on youtube even more... but if you need some join just call...

  • Hello @Valentimaraújo ! Thank you so much for your post... The language I’m using and ASP.NET C#, but that doesn’t matter. What matters and the commitment of all to help in the community... was worth the help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.