简体   繁体   English

display: inline-block 和 float: left 有什么区别

[英]What is the difference between display: inline-block and float: left

I'm want to know why not just use display:inline-block all of the time INSTEAD of float:left.我想知道为什么不一直使用 display:inline-block INSTEAD of float:left。 Inline-block seems to be much easier to control in terms of layout and not having issues with having to clear floats etc. I'm trying to get my head around why use one instead of the other.内联块在布局方面似乎更容易控制,并且没有清除浮动等问题。我试图弄清楚为什么使用一个而不是另一个。

Many thanks,非常感谢,

Emily.艾米丽。

The purpose of float is to allow text to wrap around it. float 的目的是让文本环绕它。 So it's moved to the left or right side and taken out of the page flow.所以它被移到左侧或右侧并从页面流中取出。 Line boxes containing the other text then avoid overlapping with the floated element.包含其他文本的行框避免与浮动元素重叠。 It forms a block-level, block container.它形成了一个块级的块容器。 It is not vertically aligned with any other content.它不与任何其他内容垂直对齐。

 body { width:300px; } .float-example span { float:left; width: 100px; border:2px dashed red; }
 <section class="float-example">Lorem ipsum dolor sit amet, consectetur adipiscing elit, <span>I like to use float!</span> sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</section>

The purpose on inline-block is to be a block container that sits inside a line box. inline-block 的目的是成为一个位于行盒内的块容器。 It forms an inline-level, block container within the normal flow of the content.它在内容的正常流中形成了一个内联级别的块容器。 It's vertically aligned with the other content of the line box it is in.它与它所在的行框的其他内容垂直对齐。

 body { width:300px; } .inline-block-example span { display:inline-block; width: 100px; border:2px dashed red; }
 <section class="inline-block-example">Lorem ipsum dolor sit amet, consectetur adipiscing elit, <span>I like to use inline-block!</span> sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </section>

The biggest difference is that you dont need to clear inline elements like you do floats.最大的区别是你不需要像浮动一样清除内联元素。

Float removes an element from the normal DOM flow - allowing content to wrap it. Float 从正常的 DOM 流中删除一个元素 - 允许内容包装它。 This also means that you need to clear the float on the next object in the markup in order to break out of the float condition.这也意味着您需要清除标记中下一个对象上的浮动以打破浮动条件。

Inline-block does not require you to clear. Inline-block 不需要您清除。

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

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