左右对齐消息气泡

时间:2015-10-24 19:16:44

标签: html css

我要做的是将每个消息气泡对齐到一侧,所以第一个在左侧,第二个在右侧。您可能在小提琴中看到的问题是,如果消息太短,则会显示在同一行上。

请展开“结果”窗口,以便在fiddle

上查看问题

以下是代码:

HTML:

<section id="chat-box" class="clearfix">
    <ul class="messages clearfix">
        <li>hefajgf jajgahg jahgjahgjahgj ahgjahgjaghajhg</li>
        <li>hefajgfagkjhajhgajhgajsghjasghajagkjagjka eieajgieagjaeigjaei jeagi</span></li>
        <li>hello</li>
        <li>hello</li>
    </ul>
</section>

CSS:

body {
    background: #e6e7e9;
    font-family: 'Lato', sans-serif;
    font-weight: 400;
}
.clearfix:after { 
  content: "."; 
  display: block; 
  height: 0; 
  clear: both; 
  visibility: hidden; 
}
.clearfix { 
  display: inline-block;  
}
* html .clearfix {  
  height: 1%;  
} 
.clearfix {  
  display: block;  
}
section#chat-box {
    background: #fff;
    padding: 10px;
    height: ;
    margin: 0 auto;
    max-width: 600px
}

ul.messages {
    padding: 10px;
    list-style-type: none;
}

ul.messages li {
    display: block;
    position: relative;
    float: left;
    min-width: 250px;
    background: #a6acba;
    border: 4px solid transparent;
    border-radius: 4px;
    padding: 5px;
    color: #fff;
    font-weight: 700;
    margin-bottom: 15px;
}

ul.messages li:after, ul.messages li:before {
    right: 100%;
    top: 50%;
    border: solid transparent;
    content: " ";
    height: 0;
    width: 0;
    position: absolute;
    pointer-events: none;
}

ul.messages li:after {
    border-color: rgba(166, 172, 186, 0);
    border-right-color: #a6acba;
    border-width: 12px;
    margin-top: -16px;
}
ul.messages li:before {
    border-color: rgba(0, 0, 0, 0);
    border-right-color: transparent;
    border-width: 26px;
    margin-top: -26px;
}

ul.messages li:nth-child(2n) {
    background: #76a0be;
    float: right;
}

ul.messages li:nth-child(2n):after, ul.messages li:nth-child(2n):before {
    border-right-color: transparent;
    left: 100%;
}

ul.messages li:nth-child(2n):after {
    border-left-color: #76a0be;
}
ul.messages li:nth-child(2n):before {
    border-left-color: transparent;
} 

1 个答案:

答案 0 :(得分:1)

双方都clear li个元素遗漏了你。

尝试添加此CSS规则:

li{
    display: block;
    clear: both;
}

您可以将这些规则放在更合适的地方,我这样做是为了向您展示您遗失的内容。

fiddle

中查看
相关问题