Clearfix
透過加入 clearfix 通用類別,快速輕鬆地清除容器內的浮動內容。
透過將 .clearfix 加入父元素,輕鬆清除 float。也可以作為 mixin 使用。
在 HTML 中使用:
<div class="clearfix">...</div>
mixin 原始碼:
@mixin clearfix() {
&::after {
display: block;
clear: both;
content: "";
}
}
在 SCSS 中使用 mixin:
.element {
@include clearfix;
}
以下範例展示如何使用 clearfix。如果沒有 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>