1

如果div下面的任何嵌套 s 的长度比相邻的 s 长div,则divs 不会“正确”换行到下一行。

如果不将每一行划分在它自己的 div 中,有没有办法强制 div #5(在下面的示例中)落在 div 下面,#1即使div #1它比其他的高?

<style type="text/css">
  .video-item {
    margin: 10px 29px 20px 0px;
    width: 208px;
    float: left;
    overflow: hidden;
  }
</style>

<div style="width: 948px;">
  <div class="video-item">1<br>This is what happens when Description is too long... </div>
  <div class="video-item">2<br>Description</div>
  <div class="video-item">3<br>Description</div>
  <div class="video-item">4<br>Description</div>
  <div class="video-item">5<br>Description</div>
  <div class="video-item">6<br>Description</div>
  <div class="video-item">7<br>Description</div>
  <div class="video-item">8<br>Description</div>
  <div class="video-item">9<br>Description</div>
  <div class="video-item">10<br>Description</div>
</div>

带有偶数描述的示例:
带有偶数描述的示例

不均匀描述的示例:
描述不均的示例

4

2 回答 2

1

您需要clear在 div 上设置 CSS 样式,以使其恢复正常流程:

<style type="text/css">
    .video-item {
        margin: 10px 29px 20px 0px; 
        width: 208px; 
        float: left;
        overflow: hidden; 
    }
    .clear {
        clear: left;
    }
</style>

<div style="width: 948px;">
    <div class="video-item">1<br>This is what happens when Description is too long...         </div>
    <div class="video-item">2<br>Description</div>
    <div class="video-item">3<br>Description</div>
    <div class="video-item">4<br>Description</div>
    <div class="video-item clear">5<br>Description</div>
    <div class="video-item">6<br>Description</div>
    <div class="video-item">7<br>Description</div>
    <div class="video-item">8<br>Description</div>
    <div class="video-item clear">9<br>Description</div>
    <div class="video-item">10<br>Description</div>
</div>

使用 CSS3,您可以:nth-child更动态地实现这一点:

.video-item:nth-child(4n+1) {
    clear: left;
}
于 2012-04-03T22:38:45.370 回答
1

另一种解决方案是像这样强制元素的高度:(但@Pete 解决方案更优雅)

.video-item {
    margin: 10px 29px 20px 0px; 
    width: 208px; 
    float: left;
    overflow: hidden; 
    height: 70px;
}

jsFiddle在这里

于 2012-04-03T22:42:26.433 回答