Skip to main content Skip to docs navigation

清除修复

通过添加 clearfix 工具,快速轻松地清除容器内的浮动内容。

通过将 .clearfix 添加到父元素可以轻松清除 float。也可以用作 mixin。

¥Easily clear floats by adding .clearfix to the parent element. Can also be used as a mixin.

在 HTML 中使用:

¥Use in HTML:

<div class="clearfix">...</div>

mixin 源代码:

¥The mixin source code:

@mixin clearfix() {
  &::after {
    display: block;
    clear: both;
    content: "";
  }
}

在 SCSS 中使用 mixin:

¥Use the mixin in SCSS:

.element {
  @include clearfix;
}

以下示例显示了如何使用 clearfix。如果没有 clearfix,封装 div 将不会跨越按钮,这会导致布局损坏。

¥The following example shows how the clearfix can be used. Without the clearfix the wrapping div would not span around the buttons which would cause a broken layout.

html
<div class="bg-info clearfix">
  <button type="button" class="btn btn-secondary float-start">Example Button floated left</button>
  <button type="button" class="btn btn-secondary float-end">Example Button floated right</button>
</div>