清除后,margin-top不起作用

时间:2015-07-10 13:09:03

标签: html css

您好我正在创建一个如下所示的聊天用户界面:

img http://gyazo.com/96a27ffed5203d3b435fe41d8832e1ca.png

正如您所看到的,每条消息都是li元素,现在是虚拟角色,消息div是左侧浮动。现在,如果我不在clear: both;元素上添加li,则元素将在一行中一个接一个地前进。因此,在清除了两者之后,当我执行margin-top时,在消息元素之间创建一些填充,除非我添加500px或其他内容的边距,否则它不会执行任何操作。

<section id="chat-area">
  <section id="users-in-chat">
    <ul>
      <li><img src="images/avatar-big.png"/></li>
    </ul>
  </section>
  <section id="chat-messages">
    <ul>
      <li><img src="images/avatar-big.png" class="message-avatar"/>
        <div class="message"><span>So yeah I am good how are you? I am currently in this shit and i should finish it asap so ill t alk to you laterSo yeah I am good how are you? I am currently in this shit and i should finish it asap so ill t alk to you later</span></div>
      </li>
      <li><img src="images/avatar-big.png" class="message-avatar"/>
        <div class="message"><span>So yeah I am good how are yoly in this shit and i shou later</span></div>
      </li>
    </ul>
  </section>
</section>

和css:

#chat-messages ul {
    list-style: none;
    padding: 20px;
}

#chat-messages ul li {
    clear: both;
    margin-top: 15px;
}

#chat-messages ul li:first-child {
    margin-top: 0;
}

.message-avatar {
    -webkit-border-radius: 20px 20px 20px 20px;
    border-radius: 20px 20px 20px 20px;

    float: left;
}

.message {

    margin-left: 15px;

    padding: 15px;
    max-width: 70%;
    background-color: rgba(144, 195, 245, 0.3);
    -webkit-border-radius: 0 20px 20px 20px;
    border-radius: 0 20px 20px 20px;

    float: left;
}

我在那里做错了什么?

2 个答案:

答案 0 :(得分:4)

试试这个:

&#13;
&#13;
#chat-messages ul li {
    clear: both;
    margin-top: 15px;
    float: left;
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

#chat-messages ul li {
clear: both;
margin-top: 15px;
overflow:hidden;
}