清除浮動
透過新增清除浮動的實用程式,快速且輕鬆地清除容器中的浮動內容。
透過新增 .clearfix
至父元素,輕鬆清除 float
。也可以用作混合。
在 HTML 中使用
<div class="clearfix">...</div>
混合原始碼
@mixin clearfix() {
&::after {
display: block;
clear: both;
content: "";
}
}
在 SCSS 中使用混合
.element {
@include clearfix;
}
以下範例顯示如何使用清除浮動。如果沒有清除浮動,包裝 div 將不會跨越按鈕,這將導致版面中斷。
<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>