为什么我的div重叠?

Pau*_*aul 17 html css

我正在尝试在列表元素中创建一个父div,其中包含左右两个子div.左侧将包含一个图像,右侧将包含两个包含一些文本和时间戳的附加div.

我不能让左右div显示而不重叠.

我的HTML看起来像这样:

<ul class="activity-comments">
<li>
<div style="border: solid 1px #ff0000;">
  <div style="float:left;border: solid 1px #0000ff;">
      <img src="http://localhost/new/img/sampleimg.png" class="thumb-small">
  </div>
  <div>
    <small>Some sample text here 1</small>
  </div>
  <div>
    <small>Posted 1 day ago</small>
  </div>
</div>
</li>
<li>
<div style="border: solid 1px #ff0000;">
  <div style="float:left;border: solid 1px #0000ff;">
      <img src="http://localhost/new/img/sampleimg.png" class="thumb-small">
  </div>
  <div>
    <small>Some sample text here 2</small>
  </div>
  <div>
    <small>Posted 2 days ago</small>
  </div>
</div>
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)

看看这个jsfiddle

我错过了什么?

Tra*_*ty3 13

它们是重叠的,因为你是浮动一个div,但没有清除浮动.

使用clearfix方法清除浮动.将一个类添加container到容器div并使用此CSS:

http://jsfiddle.net/57PQm/7/

.container {
    border: solid 1px #ff0000;
    zoom: 1; /* IE6&7 */
}

.container:before,
.container:after {
    content: "";
    display: table;
}

.container:after {
    clear: both;
}
Run Code Online (Sandbox Code Playgroud)

您还会注意到我删除了您的内联CSS.这使您的代码更易于维护.

  • 男人 CSS 太糟糕了。当我将其与原生 Android 和 iOS 开发进行比较时,CSS 非常缓慢和损坏。 (2认同)