跳到主要内容 跳到文档导航

通过添加 clearfix 实用程序,快速轻松地清除容器中的浮动内容。

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

在 HTML 中使用:

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

混合源代码:

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

在 SCSS 中使用 mixin:

.element {
  @include clearfix;
}

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

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>