html - 对齐灵活容器中的 float DIV

标签 html css alignment

我在灵活容器(第 1 部分)中有一组左浮动的 DIV,当容器以无法在一行中包含所有 DIV 而不是默认行为的方式调整大小时 (2) I希望行完美对齐,在需要的地方保持空间(类似表格的行为),如第 3 部分所示。

是否可以单独使用 CSS 和 HTML 来完成此操作? alt text

最佳答案

您的#2 图片不是元素左浮动时发生的情况。您可以查看在 this fiddle发生什么的示例.

如果您对 the reason 感兴趣:

A floated box is shifted to the left or right until its outer edge touches the containing block edge or the outer edge of another float. If there is a line box, the outer top of the floated box is aligned with the top of the current line box.

如果您事先知道容器的宽度,您可以将每个 :nth-child() 设置为 clear:left 显示在一个行。

在 fiddle 中,您可以添加:

div:nth-child(3n)
{
  clear: left;
}

在 css 中,所有内容都将正确对齐。问题在于您是否想要具有自动调整行的流畅布局。您需要使用 JavaScript 来调整调整大小时元素的样式。

关于html - 对齐灵活容器中的 float DIV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4761859/

相关文章:

html - Bootstrap 导航栏通知徽章

html - 如何将我的下拉菜单对齐到页面中心?

html - 如何根据菜单项的位置在 CSS 下拉框中对齐我的子菜单

javascript - 如何在所有浏览器中使用 PDFObject.js

html - 需要 css 重叠内容

html - Bootstrap 3 水平导航添加 anchor

python - PyQt 小部件与其他元素对齐

html - 在 HTML 中显示 SVG

javascript - 将鼠标悬停在一个元素上以将类添加到另一个元素?

html - Navtab 的问题