Я лично предпочел бы создать flexbox самостоятельно, а не использовать систему сетки Bootstrap, потому что тогда я могу добавить свои собственные отступы к элементам и рассчитать равномерное расстояние между ними.
<section class="items">
<a href="#" class="item">
<figure />
</a>
<a href="#" class="item">
<figure />
</a>
...
</section>
Прежде всего, вы хотите сделать .items
флексбоксом, отображаемым в виде переносимой строки:
@import "../node_modules/bootstrap/scss/functions";
@import "../node_modules/bootstrap/scss/variables";
@import "../node_modules/bootstrap/scss/mixins";
.items {
display: flex;
flex-flow: row wrap;
}
Здесь я использую SASS, но вы можете увидеть сгенерированный CSS в демо.
Я также видел, что у вас есть классы .col-6
, .col-sm-4
и т. д. в каждом столбце. Вы можете воспроизвести это, установив ширину .item
для каждой точки останова:
@import "../node_modules/bootstrap/scss/functions";
@import "../node_modules/bootstrap/scss/variables";
@import "../node_modules/bootstrap/scss/mixins";
.items {
display: flex;
flex-flow: row wrap;
.item {
width: calc(100%/2); // 2 per row
@include media-breakpoint-up(sm) {
width: calc(100%/3); // 3 per row
}
@include media-breakpoint-up(md) {
width: calc(100%/4); // 4 per row
}
@include media-breakpoint-up(lg) {
width: calc(100%/6); // 6 per row
}
}
}
Теперь пришло время рассчитать желоб между двумя .item
s. Действительно, если вы добавите отступы к каждому .item
, интервал между двумя из них будет удвоен. Но вы можете легко приспособиться к этому, добавив половину желоба/промежутка в их родительском флексбоксе, .items
.
Было бы еще проще, если бы вы использовали SASS, потому что вы можете объявить переменную для желаемого расстояния между желобами и выполнять вычисления на ее основе:
@import "../node_modules/bootstrap/scss/functions";
@import "../node_modules/bootstrap/scss/variables";
@import "../node_modules/bootstrap/scss/mixins";
$items-gutter: 2rem;
.items {
display: flex;
flex-flow: row wrap;
padding: $items-gutter/2;
.item {
padding: $items-gutter/2;
width: calc(100%/2); // 2 per row
@include media-breakpoint-up(sm) {
width: calc(100%/3); // 3 per row
}
@include media-breakpoint-up(md) {
width: calc(100%/4); // 4 per row
}
@include media-breakpoint-up(lg) {
width: calc(100%/6); // 6 per row
}
}
}
Это добавит 1rem padding к flexbox .items
и 1rem padding к каждому .item
. Это составит всего 2rem отступов вокруг каждого элемента.
демонстрация: https://jsfiddle.net/davidliang2008/rv0knh8b/15/
person
David Liang
schedule
16.08.2020