I know this is a common question, but I haven’t seen anyone answer it without switching away from grid or hardcoding pixel sizes (which seems like bad practice), so wanted to see if it’s possible:
I am trying to create a simple grid with two cells. I want the top cell to dynamically size to what’s left of the parent grid container. I want the bottom cell to remain fixed at the bottom of the parent grid container.
The top container will contain all the messages to read in a chat app.
The bottom container will contain a compose bar to write and send a message.
To feel natural, the top container (message list) should scroll when it overflows.
The bottom container (compose bar) should remain fixed at the bottom of the screen.
The problem with my code is that the top container always expands to the full length of the stacked messages when the list is long enough to overflow the container. (I am mapping an array of messages to divs within chat-message-list
). It’s so aggressive, it not only pushed the bottom cell below the fold, it pushed elements outside the grid down as well.
#chat {
display: grid;
grid-template: 1fr 64px / 100%;
height: 100%;
/* This does nothing */
}
#chat-read {
grid-area: 1 / 1 / span 1 / span 1;
overflow: scroll;
margin-bottom: 0px;
}
#chat-compose {
grid-area: 2 / 1 / span 1 / span 1;
}
#chat-message-list {
display: flex;
}
<div id="chat">
<div id="chat-read">
<div id="chat-message-list">
... /* Variable amount of divs containing a chat message each */
</div>
</div>
<div id="chat-compose">
... /* Fixed-height compose bar */
</div>
</div>
Thanks in advance!
2
Answers
Use below code:
You need to specify a maximum height for, ideally, the
body
or the parent of your chat div for the 100% height to take effect.