简体   繁体   English

flexbox不会溢出justify-content:flex-end

[英]flexbox won't overflow with justify-content: flex-end

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 我正在创建一个与css flexbox的聊天,因为我希望我的消息是一个容器的底层,我使用了justify-content: flex-end但是当我需要很多消息时,div不能用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. 我已经读过一个解决方案,它可以反转我的.messages div和我的.write-zone div并替换flex-direction: column-reverse但这对我来说不是一个好的解决方案,因为我的.messages div的内容是动态的,消息将被添加,滚动级别将不会跟随,我想避免在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> 

If I understand correctly, you're trying to have the messages align from the bottom and move up if new ones appear. 如果我理解正确的话,你会尝试让消息从底部对齐,如果出现新消息则向上移动。

Using justify-content: flex-end on the container is correct, but if .messages is taking all available height, that still has no effect. 使用justify-content: flex-end容器上的justify-content: flex-end是正确的,但如果.messages占用所有可用高度,那仍然没有效果。

From .messages , remove the height: 100% and replace its justify-content: flex-end (which seems unnecessary) with margin-top: auto and I think you'll be a step further. .messages中删除height: 100%并用margin-top: auto替换其justify-content: flex-end (这似乎是不必要的),我认为你会更进一步。

.messages {
    display: flex;
    flex-direction: column;
    margin-top: auto;
    overflow-y: auto;
}

So the 'trick' you're looking for is an auto margin: https://www.w3.org/TR/css-flexbox-1/#auto-margins . 因此,您正在寻找的“技巧”是汽车保证金: https//www.w3.org/TR/css-flexbox-1/#auto-margins

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. 我发现通过从.messages中删除justify-content flex-end,我可以滚动浏览消息。

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> 

You were almost there! 你快到了!

Two things that you can do to get you closer: 你可以做两件事来让你更接近:

  1. To prevent the flex items from collapsing ( dividing itself among the space of the container it's placed in ) is to set an explicit min-height instead of just a height 为了防止弹性物品折叠(将其自身分隔放置在容器的空间中),设置一个明确的最小高度而不是高度
  2. Create a container div that wraps your messages: .messages-container and give this an explicit max-height and overflow auto. 创建一个包装你的消息的容器div: .messages-container并给它一个明确的max-height和overflow auto。

HTML: HTML:

<div class="messages">
    <div class="messages-container"> <!-- New Wrapping Container -->
       <div class="message user">

CSS: CSS:

    .message {
      min-height: 80px;
    }

   .messages-container {
      height: 100%;
      max-height: 300px;
      overflow: auto;
    }
   .messages{
     display: flex;
     flex-direction: column;
     justify-content: flex-end;
      overflow: auto;
    }

Working Example: 工作实例:

 .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; min-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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM