Switch to custom properties to control grid gutter widths (#30475)

This commit is contained in:
Martijn Cuppens 2020-03-30 15:12:36 +02:00 committed by GitHub
parent e1f5d819c7
commit 8414126266
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 38 deletions

View File

@ -12,40 +12,6 @@
}
}
// Gutters
//
// Make use of `.g-*`, `.gx-*` or `.gy-*` utilities to change spacing between the columns.
@if $enable-grid-classes {
@each $breakpoint in map-keys($grid-breakpoints) {
$infix: breakpoint-infix($breakpoint, $grid-breakpoints);
@include media-breakpoint-up($breakpoint, $grid-breakpoints) {
@each $key, $value in $gutters {
.g#{$infix}-#{$key},
.gx#{$infix}-#{$key} {
margin-right: -$value / 2;
margin-left: -$value / 2;
> * {
padding-right: $value / 2;
padding-left: $value / 2;
}
}
.g#{$infix}-#{$key},
.gy#{$infix}-#{$key} {
margin-top: -$value;
> * {
margin-top: $value;
}
}
}
}
}
}
// Columns
//

View File

@ -3,10 +3,13 @@
// Generate semantic grid columns with these mixins.
@mixin make-row($gutter: $grid-gutter-width) {
--grid-gutter-x: #{$gutter};
--grid-gutter-y: 0;
display: flex;
flex-wrap: wrap;
margin-right: -$gutter / 2;
margin-left: -$gutter / 2;
margin-top: calc(var(--grid-gutter-y) * -1); // stylelint-disable-line function-blacklist
margin-right: calc(var(--grid-gutter-x) / -2); // stylelint-disable-line function-blacklist
margin-left: calc(var(--grid-gutter-x) / -2); // stylelint-disable-line function-blacklist
}
@mixin make-col-ready($gutter: $grid-gutter-width) {
@ -18,8 +21,9 @@
flex-shrink: 0;
width: 100%;
max-width: 100%; // Prevent `.col-auto`, `.col` (& responsive variants) from breaking out the grid
padding-right: $gutter / 2;
padding-left: $gutter / 2;
padding-right: calc(var(--grid-gutter-x) / 2); // stylelint-disable-line function-blacklist
padding-left: calc(var(--grid-gutter-x) / 2); // stylelint-disable-line function-blacklist
margin-top: var(--grid-gutter-y);
}
@mixin make-col($size, $columns: $grid-columns) {
@ -93,6 +97,21 @@
}
}
}
// Gutters
//
// Make use of `.g-*`, `.gx-*` or `.gy-*` utilities to change spacing between the columns.
@each $key, $value in $gutters {
.g#{$infix}-#{$key},
.gx#{$infix}-#{$key} {
--grid-gutter-x: #{$value};
}
.g#{$infix}-#{$key},
.gy#{$infix}-#{$key} {
--grid-gutter-y: #{$value};
}
}
}
}
}

View File

@ -45,6 +45,7 @@ Breaking it down, here's how it works:
- The horizontal gutter width can be changed with `.gx-*` classes like `.gx-2` (smaller horizontal gutters) or `.gx-xl-4` (larger horizontal gutters on viewports larger than the `xl` breakpoint).
- The vertical gutter width can be changed with `.gy-*` classes like `.gy-2` (smaller vertical gutters) or `.gy-xl-4` (larger vertical gutters on viewports larger than the `xl` breakpoint). To achieve vertical gutters, additional margin is added to the top of each column. The `.row` counteracts this margin to the top with a negative margin.
- The gutter width in both directions can be changed with `.g-*` classes like `.g-2` (smaller gutters) or `.g-xl-4` (larger gutters on viewports larger than the `xl` breakpoint)
- CSS custom properties (`--grid-gutter-x` and `--grid-gutter-y`) are used to calculate the gutter widths.
Be aware of the limitations and [bugs around flexbox](https://github.com/philipwalton/flexbugs), like the [inability to use some HTML elements as flex containers](https://github.com/philipwalton/flexbugs#flexbug-9).