清除浮動(Clearfix)
透過增加clearfix通用類別,可以快速輕松地清除容器中的浮動內容。
On this page
透過將.clearfix添加到父元素之中可以輕松的清除float。也可以當作mixin來使用.
在HTML使用:
<div class="clearfix">...</div>
mixin 源代碼:
@mixin clearfix() {
&::after {
display: block;
clear: both;
content: "";
}
}
在mixin中搭配SCSS:
.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>