添加到@Jonas 和@KFunk 的答案:
要求使用所有 col-size 的潜在修复 (col-xs-6 col-sm-4 col-md-4 col-lg-4)。
创建的类:auto-clear-xs、auto-clear-sm、auto-clear-md 和 auto-clear-lg。
我的答案是移动优先。
注意:这仍然要求列的大小相同。
HTML
<div class="row auto-clear-xs auto-clear-lg">
<div class="col-xs-6 col-lg-3">
<p>Hey</p>
</div>
</div>
SCSS
@mixin row-first-child($col-type, $clear-type) {
.col-#{$col-type}- {
&1:nth-child(12n+1),
&2:nth-child(6n+1),
&3:nth-child(4n+1),
&4:nth-child(3n+1),
&6:nth-child(odd){
clear: $clear-type;
}
}
}
.auto-clear-xs{
@media (min-width: $screen-xs-min){
@include row-first-child(xs, both);
}
@media (max-width: $screen-xs-min){
@include row-first-child(xs, both);
}
}
.auto-clear-sm{
@media (min-width: $screen-sm){
@include row-first-child(xs, none);
@include row-first-child(sm, both);
}
}
.auto-clear-md{
@media (min-width: $screen-md){
@include row-first-child(xs, none);
@include row-first-child(sm, none);
@include row-first-child(md, both);
}
}
.auto-clear-lg{
@media (min-width: $screen-lg){
@include row-first-child(xs, none);
@include row-first-child(sm, none);
@include row-first-child(md, none);
@include row-first-child(lg, both);
}
}
CSS
@media (min-width: 480px) {
.auto-clear-xs .col-xs-1:nth-child(12n+1),
.auto-clear-xs .col-xs-2:nth-child(6n+1),
.auto-clear-xs .col-xs-3:nth-child(4n+1),
.auto-clear-xs .col-xs-4:nth-child(3n+1),
.auto-clear-xs .col-xs-6:nth-child(odd) {
clear: both;
}
}
@media (max-width: 480px) {
.auto-clear-xs .col-xs-1:nth-child(12n+1),
.auto-clear-xs .col-xs-2:nth-child(6n+1),
.auto-clear-xs .col-xs-3:nth-child(4n+1),
.auto-clear-xs .col-xs-4:nth-child(3n+1),
.auto-clear-xs .col-xs-6:nth-child(odd) {
clear: both;
}
}
@media (min-width: 768px) {
.auto-clear-sm .col-xs-1:nth-child(12n+1),
.auto-clear-sm .col-xs-2:nth-child(6n+1),
.auto-clear-sm .col-xs-3:nth-child(4n+1),
.auto-clear-sm .col-xs-4:nth-child(3n+1),
.auto-clear-sm .col-xs-6:nth-child(odd) {
clear: none;
}
.auto-clear-sm .col-sm-1:nth-child(12n+1),
.auto-clear-sm .col-sm-2:nth-child(6n+1),
.auto-clear-sm .col-sm-3:nth-child(4n+1),
.auto-clear-sm .col-sm-4:nth-child(3n+1),
.auto-clear-sm .col-sm-6:nth-child(odd) {
clear: both;
}
}
@media (min-width: 992px) {
.auto-clear-md .col-xs-1:nth-child(12n+1),
.auto-clear-md .col-xs-2:nth-child(6n+1),
.auto-clear-md .col-xs-3:nth-child(4n+1),
.auto-clear-md .col-xs-4:nth-child(3n+1),
.auto-clear-md .col-xs-6:nth-child(odd) {
clear: none;
}
.auto-clear-md .col-sm-1:nth-child(12n+1),
.auto-clear-md .col-sm-2:nth-child(6n+1),
.auto-clear-md .col-sm-3:nth-child(4n+1),
.auto-clear-md .col-sm-4:nth-child(3n+1),
.auto-clear-md .col-sm-6:nth-child(odd) {
clear: none;
}
.auto-clear-md .col-md-1:nth-child(12n+1),
.auto-clear-md .col-md-2:nth-child(6n+1),
.auto-clear-md .col-md-3:nth-child(4n+1),
.auto-clear-md .col-md-4:nth-child(3n+1),
.auto-clear-md .col-md-6:nth-child(odd) {
clear: both;
}
}
@media (min-width: 1200px) {
.auto-clear-lg .col-xs-1:nth-child(12n+1),
.auto-clear-lg .col-xs-2:nth-child(6n+1),
.auto-clear-lg .col-xs-3:nth-child(4n+1),
.auto-clear-lg .col-xs-4:nth-child(3n+1),
.auto-clear-lg .col-xs-6:nth-child(odd) {
clear: none;
}
.auto-clear-lg .col-sm-1:nth-child(12n+1),
.auto-clear-lg .col-sm-2:nth-child(6n+1),
.auto-clear-lg .col-sm-3:nth-child(4n+1),
.auto-clear-lg .col-sm-4:nth-child(3n+1),
.auto-clear-lg .col-sm-6:nth-child(odd) {
clear: none;
}
.auto-clear-lg .col-md-1:nth-child(12n+1),
.auto-clear-lg .col-md-2:nth-child(6n+1),
.auto-clear-lg .col-md-3:nth-child(4n+1),
.auto-clear-lg .col-md-4:nth-child(3n+1),
.auto-clear-lg .col-md-6:nth-child(odd) {
clear: none;
}
.auto-clear-lg .col-lg-1:nth-child(12n+1),
.auto-clear-lg .col-lg-2:nth-child(6n+1),
.auto-clear-lg .col-lg-3:nth-child(4n+1),
.auto-clear-lg .col-lg-4:nth-child(3n+1),
.auto-clear-lg .col-lg-6:nth-child(odd) {
clear: both;
}
}