应用 float 时的 css 显示属性

标签 css css-float

当元素 float 时,不同的显示属性如何影响布局?或者,这些类之间有什么区别(如果有的话):

div.foo {
    display: block;
    float: left;
}

div.foo2 {
    display: inline;
    float: left;
}

div.foo3 {
    display: inline-block;
    float: left;
}

编辑:

如果根据规范没有差异,那么某些过时版本的浏览器(咳咳,IE)是否以不同方式呈现它们?

最佳答案

如果我阅读 spec正确,并且实践证实了这一点,设置 float: leftright 无论如何都会覆盖 display 属性并强制 display: block 在元素上(尽管有特殊性,见下文),所以你的三个例子之间没有区别:

left The element generates a block box that is floated to the left. Content flows on the right side of the box, starting at the top (subject to the 'clear' property).

right Similar to 'left', except the box is floated to the right, and content flows on the left side of the box, starting at the top.

none The box is not floated.

与普通的 display: block 不同的是,设置 float 决定了它自己关于覆盖 display 属性的行为:如果没有宽度被显式指定, float 元素将占据它需要的宽度,这与自动占据 100% 宽度的标准 block 元素行为相反。

关于应用 float 时的 css 显示属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9774282/

相关文章:

css - 如何让图像在没有垂直空间的情况下环绕?

左侧CSS导航,右侧图片

javascript - 当元素被放入其中时,使可放置的 div 展开。使用可拖动的 jquery ui

html - 如何在已经居中对齐的元素周围添加元素?

html - 如何在 HTML/CSS 中设置百分比高度值

html - 内联 block 元素的问题

html - 侧 Pane 调整主要内容大小的 CSS float 错误

CSS 问题,无法在分区内居中图像

html - 即使正确链接也无法加载资源

javascript - 获取点击相对于最近亲属的位置