html - 可变高度的 float 元素将兄弟向下推

标签 html css

我有 6 个元素,它们应该生成两行,每行 3 个元素,所以我将它们 float 。但是元素的内容变化很大,当一个更高的元素阻止后续的兄弟元素一直向左浮动时,布局就会中断:

Floated elements breaking layout

这是示例 CSS:

figure { width: 30%; float: left; margin-left: 1%; font-size: small; outline: solid #999 1px; }
img { max-width: 100%; }

和 HTML:

<figure>
  <img src="http://placekitten.com/150/200?image=1" alt="Kitten 1" />
  <figcaption>Bacon ipsum dolor sit amet short ribs pork chop pork belly spare ribs shoulder tri-tip beef ribs turkey brisket short loin tenderloin ground round. </figcaption>
</figure>
<figure>
  <img src="http://placekitten.com/150/200?image=2" alt="Kitten 2" />
  <figcaption>Short ribs cow corned beef, beef tenderloin swine biltong short loin. </figcaption>
</figure>
<figure>
  <img src="http://placekitten.com/150/200?image=3" alt="Kitten 3" />
  <figcaption>Boudin chuck ground round, pig pastrami salami turkey ham hock beef ribs tongue. </figcaption>
</figure>
<figure>
  <img src="http://placekitten.com/150/200?image=4" alt="Kitten 4" />
  <figcaption>Tri-tip pork loin tongue corned beef shankle ball tip. </figcaption>
</figure>
<figure>
  <img src="http://placekitten.com/150/200?image=5" alt="Kitten 5" />
  <figcaption>Turkey swine tenderloin spare ribs sausage filet mignon hamburger. Leberkas andouille prosciutto, bresaola tri-tip short loin meatloaf shank pig shoulder spare ribs ribeye. </figcaption>
</figure>
<figure>
  <img src="http://placekitten.com/150/200?image=6" alt="Kitten 6" />
  <figcaption>Pastrami andouille tongue tri-tip jerky.</figcaption>
</figure>

还有一个 JSFiddle 示例:http://jsfiddle.net/KatieK/5Upbt/

如何让第二行 figure 元素排在前 3 个元素的下方?

HTML/CSS 解决方案优于 JavaScript/jQuery 解决方案。

最佳答案

纯 CSS 解决方案怎么样?添加此规则:

figure:nth-of-type(3n+1) {
    clear:left;
}

jsFiddle example

关于html - 可变高度的 float 元素将兄弟向下推,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54171294/

相关文章:

javascript - 使用 CSS 类在 JS 中翻转页面上的随机元素

jquery - 单击时更改选择选项的背景颜色

android - HTML5 移动应用程序是否在服务器上运行并通过各种网络浏览器访问?

html - 窗口仍然有水平滚动

javascript - 从视频中删除 TextTrack

html - 取消 css 规则

css - 如何防止空格和连字符换行并以特定宽度换行?

javascript - 与其他浏览器相比,IE/Edge 样式的应用顺序不同

css - 打印时隐藏父元素但显示子元素

下拉列表菜单箭头的CSS