CSS缩放聊天气泡的文字长度?

时间:2015-10-06 10:31:47

标签: html css

我无法根据聊天气泡中文本的大小创建可扩展的div,其大小会增加/减少。一个很好的例子是信使或Facebook聊天使用的聊天泡泡。



.left-chat-bubble{
  background-color:gray;
  padding:10px;
  border-radius:40px;
  max-width:300px;
 }

<div class="left-chat-bubble-wrap">
  <div class="left-chat-bubble">
    <p>hello</p>
  </div>
</div>
&#13;
&#13;
&#13;

我可以达到它的最大尺寸,但我不能让气泡环绕文本的相对大小。

4 个答案:

答案 0 :(得分:3)

您需要display设置inlineinline-block。看到这个。

.left-chat-bubble{
  display: inline-block;
  background-color:gray;
  padding:10px;
  border-radius:40px;
  max-width:300px;
 }
<div class="left-chat-bubble-wrap">
  <div class="left-chat-bubble">
    <p>hello</p>
  </div>
</div>

答案 1 :(得分:2)

为了更好的聊天式布局,您可以使用此代码段:

<强>更新

  

添加箭头对于更好的用户界面也很有用。

    .msg-list {
        width: 100%;
    }
    .messenger-container {
        padding: 8px 15px 8px 13px;
        margin: 0 0 25px 35px;
        float: left;
        max-width: 82%;
        background: #f2f2f2;
        border-radius: 10px;
        min-width: 20%;
        position: relative; box-sizing: border-box;
        box-shadow: 7px 10px 6px -5px #BBC0C7;
    }
    .messenger-container p {
       color: #3D3D3D;
        font-size: 12px;
        margin-bottom: 6px;
        line-height: 18px;
        word-wrap: break-word; margin: 0;
    }
    .sender .messenger-container {
        float: right;
        margin-right: 35px;
        width: auto;
        background: #D5DBFF;
        margin-left: 0px;
        padding: 8px 15px 8px 13px;
    }
    .clear {
        clear:both;
        width: 100%;
        padding: 0px !important;
        margin: 0px;
        height:0px;
    }
       .messenger-container::before {
           width: 0px;
           height: 0px;
           border-top: 8px solid transparent;
           border-bottom: 8px solid transparent; 

           border-right:15px solid #f2f2f2; 
           content: "";
           position: absolute;
           top: 9px;
           left: -15px;
       }
    .sender .messenger-container::before {
          width: 0px;
          height: 0px;
          border-top: 8px solid transparent;
          border-bottom: 8px solid transparent;
          border-left: 15px solid #D5DBFF;
          content: "";
          position: absolute;
          top: 9px;
          left: 99%;border-right: none;
   }
  <div class="msg-list">
    <div class="messenger-container">
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry</p>
    </div>
</div>
<div class="clear"></div>
<div class="msg-list sender">
    <div class="messenger-container">
        <p>Lorem Ipsum</p>
    </div>
</div>
<div class="clear"></div>
<div class="msg-list">
    <div class="messenger-container">
        <p>Lorem Ipsum is simply dummy text</p>
    </div>
</div>
<div class="clear"></div>
<div class="msg-list sender">
    <div class="messenger-container">
        <p>Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum</p>
    </div>
</div>
<div class="clear"></div>

答案 2 :(得分:1)

.left-chat-bubble{
  background-color:gray;
  padding:10px;
  border-radius:40px;
  display:table-cell;
 }
<div class="left-chat-bubble-wrap">
  <div class="left-chat-bubble">
    <p>hello with more text</p>
  </div>
</div>

答案 3 :(得分:1)

或者,如果您希望它们彼此重叠,则可以使用display:inline-blockfloat:leftclear:both

&#13;
&#13;
.left-chat-bubble{
  clear:both;
  float:left;
  background-color:gray;
  padding:10px;
  border-radius:40px;
  max-width:300px;
 }
&#13;
<div class="left-chat-bubble-wrap">
  <div class="left-chat-bubble">
    <p>hello, this is a chat bubble</p>
  </div>
  <div class="left-chat-bubble">
    <p>hello, this is a chat bubble with text that wraps onto multiple lines</p>
  </div>
</div>
&#13;
&#13;
&#13;