繁体   English   中英

如何左对齐和右对齐我的聊天气泡?

[英]How can I left align and right align my chat bubbles?

我创建了两个聊天气泡,其中一个我需要左对齐,另一个我需要右对齐。截至目前,我的 css 一直增长到最后,如下图所示。

在此处输入图像描述

截至目前,我不想对宽度进行硬编码,而是希望气泡随着聊天文本而增长,两个都应该左对齐,另一个应该右对齐,如下所示:

在此处输入图像描述

CSS:

.speech-wrapper{
  padding: 5px 6px;

  }
.chatbox {
  padding: 5px;
  margin-left: 5px;
  margin-right: 5px;
  margin-top:0px;   
  background: #075698;
  color: #FFF;
  position: relative;
  border-radius: 10px;  
  overflow-wrap: break-word;
  word-wrap: break-word;
  hyphens: auto;
  box-shadow: 0 8px 6px -6px black;




}
.chatbox_other{
  height:auto;
  padding: 5px;
  margin-left: 5px;
  margin-right: 5px;
  margin-top:0px;   
  background: #DCDCDC;
  color: #000;
  position: relative;
  border-radius: 10px;  
  overflow-wrap: break-word;
  word-wrap: break-word;
  hyphens: auto;
  box-shadow: 0 8px 6px -6px black;


}

      .name_other{
         font-weight: 600;
        font-size: 12px;
        margin: 0px 0px  9px;
        color: #1970b0;
      }
       .name_other1{
         font-weight: 600;
        font-size: 12px;
        margin: 0px 0px  9px;
        color: #ba006e;
      }
       .name_other2{
         font-weight: 600;
        font-size: 12px;
        margin: 0px 0px  9px;
        color: #007670;
      }
       .name_other3{
         font-weight: 600;
        font-size: 12px;
        margin: 0px 0px  9px;
        color: #3b0256;
      }
       .name_other4{
         font-weight: 600;
        font-size: 12px;
        margin: 0px 0px  9px;
        color: #00512b;
      }
       .name_other5{
         font-weight: 600;
        font-size: 12px;
        margin: 0px 0px  9px;
        color: #a91024;
      }
         .name_other6{
         font-weight: 600;
        font-size: 12px;
        margin: 0px 0px  9px;
        color: #b8471b;
      }
         .name_other7{
         font-weight: 600;
        font-size: 12px;
        margin: 0px 0px  9px;
        color: #7f1c7d;
      }

.timestamp_other{
        font-size: 11px;
        position: absolute;
        bottom: 0px;
        right: 10px;
        text-transform: uppercase; color: #999
}       
.timestamp{
        font-size: 11px;
        position: absolute;
        bottom: 0px;
        right: 10px;
        text-transform: uppercase; color: #fff
      }
/* speech bubble 13 */
      .name{
        font-weight: 600;
        font-size: 12px;
        margin: 0px 0px  9px;
        color: #ffffff;
      }


.triangle.left-top:after {
  content: ' ';
  position: absolute;
  width: 0;
  height: 0;
  left: -10px;
  right: auto;
  top: 0px;
  bottom: auto;
  border: 22px solid;
  border-color: #DCDCDC transparent transparent transparent;
  z-index: -1;
}

.triangle.right-top:before {
    content: ' ';
    position: absolute;
    width: 0;
    height: 0;
    left: auto;
    right: -10px;
    top: 0;
    bottom: auto;
    border: 32px solid;
    border-color: #075698 transparent transparent 
      transparent;
      z-index: -1;
}   
   .alt{
      margin: 0 0 0 60px;
    }

HTML:

<div class="speech-wrapper">
  <div class="chatbox triangle right-top alt">
    <div class="txt">
      <p class="name">Apple TestUser1</p>Hi<span class="timestamp">10:20 pm</span></div>
  </div>

  <div class="speech-wrapper">
    <div class="chatbox_other triangle left-top">
      <div class="txt">
        <p class="name">Apple TestUser1</p>Hi<span class="timestamp">10:20 pm</span>
      </div>
    </div>
  </div>
</div>

我试过 uisng float left 但是在减小 windows 大小的同时聊天开始相互重叠

要进行测试,请尝试以下链接https://codepen.io/rajesh-kumar-dash/pen/KbvqQX

做到这一点的“肮脏”方法是在div之后添加a只是为了清除浮点数:

<div class="speech-wrapper"><div class="chatbox triangle right-top alt"><div class="txt"><p class="name">Apple TestUser1</p>Hi<span class="timestamp">10:20 pm</span></div></div>
<div style="clear:both"></div>
<div class="speech-wrapper"><div class="chatbox_other triangle left-top"><div class="txt"><p class="name">Apple TestUser1</p>Hi<span class="timestamp">10:20 pm</span></div></div>
<div style="clear:both"></div>

做到这一点的更干净的方法称为“ Clearfix”。 我建议您看一下以下两个链接以了解它并在代码中使用:

https://www.w3schools.com/css/css_float.asp

https://css-tricks.com/snippets/css/clear-fix/

我认为使用float: right; 右气泡和左气泡: float: left; width: auto; 应该管用

如果你想让你的盒子和里面的文字一样大,你必须设置“display: inline-block”; 为确保您的盒子不会变得比某个尺寸大,请使用“最大宽度”。 使用“overflow-wrap: break-word”,您的文本在达到最大宽度时会中断。

暂无
暂无

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

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