I'm creating a chat with css flexbox, and as I want my messages to be diplay bottom of a container, I used justify-content: flex-end
but when I have to many messages the div isn't scrollable with overflow: auto
I've read about a solution which is to reverse my .messages
div and my .write-zone
div and to replace flex-direction: column-reverse
but this isn't a good solution for me because the content of my .messages
div is dynamic, messages will be added and the scroll level won't follow, and I'd like to avoid controlling the scroll in javascript.
Does someone know any solution or trick to do this ?
Here's a fiddle showing skeleton of my code :
.container {
width: 60%;
height: 300px;
display: flex;
flex-direction: column;
justify-content: flex-end;
}
.messages {
height: 100%;
display: flex;
flex-direction: column;
justify-content: flex-end;
overflow-y: auto;
}
.message {
margin: 5px;
height: 80px;
display: flex;
flex-direction: row;
}
.message.user {
justify-content: flex-end;
background-color: #2bf;
}
.message.other {
justify-content: flex-start;
background-color: #bbb;
}
.write-zone {
width: 100%;
height: 7%;
display: flex;
flex-direction: row;
justify-content: space-around;
}
input {
width: 80%;
}
<div class="container">
<div class="messages">
<div class="message user">
Toto
</div>
<div class="message other">
Titi
</div><div class="message user">
Toto
</div>
<div class="message other">
Titi
</div>
<div class="message user">
Toto
</div>
<div class="message other">
Titi
</div>
<div class="message user">
Toto
</div>
<div class="message other">
Titi
</div>
<div class="message user">
Toto
</div>
<div class="message other">
Titi
</div>
<div class="message user">
Toto
</div>
<div class="message other">
Titi
</div>
</div>
<div class="write-zone">
<input type="text" />
<button>
send
</button>
</div>
</div>
To fix this problem use flexbox auto margins, instead of justify-content . With auto margins, an overflowing flex item can be vertically and horizontally centered without losing access to any part of it.
If you are using flexbox and want the content to wrap, you must specify flex-wrap: wrap . By default flex items don't wrap. To have the images be three-across, you should specify flex-basis: 33.33333% .
The justify-content property is a sub-property of the Flexible Box Layout module. It defines the alignment along the main axis. It helps distribute extra free space leftover when either all the flex items on a line are inflexible, or are flexible but have reached their maximum size.
It sounds like your trying to make the chatbox scrollable when there are a lot of messages.
I found that by removing the justify-content flex-end from the .messages I'm able to scroll through the messages.
Hope this helps.
.container {
width: 60%;
height: 300px;
display: flex;
flex-direction: column;
justify-content: flex-end;
}
.messages-container {
height: 100%;
max-height: 300px;
overflow: auto;
}
.messages{
display: flex;
flex-direction: column;
/* justify-content: flex-end; */
overflow: auto;
}
.message {
margin: 5px;
height: 80px;
display: flex;
flex-direction: row;
}
.message.user {
justify-content: flex-end;
background-color: #2bf;
}
.message.other {
justify-content: flex-start;
background-color: #bbb;
}
.write-zone {
width: 100%;
height: 7%;
display: flex;
flex-direction: row;
justify-content: space-around;
}
input {
width: 80%;
}
<div class="container">
<div class="messages">
<div class="messages-container">
<div class="message user">
Toto
</div>
<div class="message other">
Titi
</div><div class="message user">
Toto
</div>
<div class="message other">
Titi
</div>
<div class="message user">
Toto
</div>
<div class="message other">
Titi
</div>
<div class="message user">
Toto
</div>
<div class="message other">
Titi
</div>
<div class="message user">
Toto
</div>
<div class="message other">
Titi
</div>
<div class="message user">
Toto
</div>
<div class="message other">
Titi
</div>
</div>
</div>
<div class="write-zone">
<input type="text" />
<button>
send
</button>
</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With