简体   繁体   English

为什么我的div重叠?

[英]Why are my divs overlapping?

I'm trying to create a parent div inside a list element that contains two children div for the left and right. 我正在尝试在列表元素中创建一个父div,其中包含左右两个子div。 The left will contain an image and the right will contain two additional divs that contain some text and a timestamp. 左侧将包含一个图像,右侧将包含两个包含一些文本和时间戳的附加div。

I can't get the left and right divs to display without overlapping. 我不能让左右div显示而不重叠。

My HTML Looks like this: 我的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>

Take a look at this jsfiddle 看看这个jsfiddle

What am I missing? 我错过了什么?

They are overlapping because you are floating a div, but aren't clearing the float. 它们是重叠的,因为你是浮动一个div,但没有清除浮动。

Use the clearfix method to clear the float. 使用clearfix方法清除浮动。 Add a class container to your container divs and use this CSS: 将一个类container添加到容器div并使用此CSS:

http://jsfiddle.net/57PQm/7/ 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;
}

You'll also notice that I've removed your inline CSS. 您还会注意到我删除了您的内联CSS。 This makes your code easier to maintain. 这使您的代码更易于维护。

I wonder if this is what you are looking for: jsfiddle 我想知道这是不是你想要的: jsfiddle

I added in img{display:block;} as images come with some hidden padding that shoves elemets down. 我添加了img{display:block;}因为图像带有一些隐藏的填充,可以将元素向下推。

your thumbnail is setting the picture to a specific height of 41px. 您的缩略图将图片设置为41px的特定高度。 Either change the picture size, or change the div size or set the overflow of the outer div. 更改图片大小,或更改div大小或设置外部div的溢出。

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

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