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

时间:2019-01-02 14:57:11

标签: html css

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

enter image description here

到目前为止,我不想对宽度进行硬编码,也不希望气泡随聊天文本一起增长,并且两个都应保持左对齐,而另一个则应保持右对齐,如下所示:

enter image description here

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向左浮动,但在减小窗口大小的同时,聊天开始彼此重叠

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

2 个答案:

答案 0 :(得分:0)

“肮脏”的方法是在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/

答案 1 :(得分:0)

我认为将import shutil import tempfile def gather_compressed_files(input_dir: pathlib.Path, output_file: str): with tarfile.open(output_file, 'w:gz') as tar: for input_file in input_dir.glob('*.gz'): with tempfile.TemporaryFile() as tf: # Could combine both in one with, but this way we close the gzip # file ASAP with gzip.open(input_file) as fd: shutil.copyfileobj(fd, tf) tar_info = tarfile.TarInfo(input_file.stem) tar_info.size = tf.tell() tf.seek(0) tar.addfile(tar_info, tf) 用于右气泡和左气泡:float: right;float: left;应该可以