diff --git a/.bundlewatch.config.json b/.bundlewatch.config.json index 988accd7f9..10c0d2e571 100644 --- a/.bundlewatch.config.json +++ b/.bundlewatch.config.json @@ -2,15 +2,15 @@ "files": [ { "path": "./dist/css/bootstrap-grid.css", - "maxSize": "7 kB" + "maxSize": "7.25 kB" }, { "path": "./dist/css/bootstrap-grid.min.css", - "maxSize": "6 kB" + "maxSize": "6.5 kB" }, { "path": "./dist/css/bootstrap-reboot.css", - "maxSize": "2.25 kB" + "maxSize": "2 kB" }, { "path": "./dist/css/bootstrap-reboot.min.css", @@ -18,19 +18,19 @@ }, { "path": "./dist/css/bootstrap-utilities.css", - "maxSize": "7.5 kB" + "maxSize": "7.2 kB" }, { "path": "./dist/css/bootstrap-utilities.min.css", - "maxSize": "6.75 kB" + "maxSize": "6.6 kB" }, { "path": "./dist/css/bootstrap.css", - "maxSize": "24.25 kB" + "maxSize": "25 kB" }, { "path": "./dist/css/bootstrap.min.css", - "maxSize": "22.25 kB" + "maxSize": "22.75 kB" }, { "path": "./dist/js/bootstrap.bundle.js", diff --git a/scss/_grid.scss b/scss/_grid.scss index 5686f31fe0..13f21a3ff7 100644 --- a/scss/_grid.scss +++ b/scss/_grid.scss @@ -12,6 +12,40 @@ } } +@mixin make-cssgrid($columns: $grid-columns, $breakpoints: $grid-breakpoints) { + @each $breakpoint in map-keys($breakpoints) { + $infix: breakpoint-infix($breakpoint, $breakpoints); + + @include media-breakpoint-up($breakpoint, $breakpoints) { + @if $columns > 0 { + @for $i from 1 through $columns { + .g-col#{$infix}-#{$i} { + grid-column: auto / span $i; + } + } + + // `$columns - 1` because offsetting by the width of an entire row isn't possible + @for $i from 0 through ($columns - 1) { + .g-start#{$infix}-#{$i} { + grid-column-start: $i; + } + } + } + } + } +} + +@if $enable-cssgrid { + .grid { + display: grid; + grid-template-rows: repeat(var(--#{$variable-prefix}rows, 1), 1fr); + grid-template-columns: repeat(var(--#{$variable-prefix}columns, #{$grid-columns}), 1fr); + gap: var(--#{$variable-prefix}gap, #{$grid-gutter-width}); + + @include make-cssgrid(); + } +} + // Columns // diff --git a/scss/_variables.scss b/scss/_variables.scss index 06dfa4a4dd..8abd750329 100644 --- a/scss/_variables.scss +++ b/scss/_variables.scss @@ -221,6 +221,7 @@ $enable-transitions: true !default; $enable-reduced-motion: true !default; $enable-smooth-scroll: true !default; $enable-grid-classes: true !default; +$enable-cssgrid: false !default; $enable-button-pointers: true !default; $enable-rfs: true !default; $enable-validation-icons: true !default; diff --git a/site/assets/scss/_component-examples.scss b/site/assets/scss/_component-examples.scss index e831b16095..8fbfc06c5c 100644 --- a/site/assets/scss/_component-examples.scss +++ b/site/assets/scss/_component-examples.scss @@ -23,6 +23,21 @@ background-color: rgba(255, 0, 0, .1); } +.bd-example-cssgrid { + text-align: center; + + .grid + .grid { + margin-top: 1rem; + } + + .grid > * { + padding-top: .75rem; + padding-bottom: .75rem; + background-color: rgba(255, 0, 255, .1); + border: 1px solid rgba(255, 0, 255, .25); + } +} + .bd-highlight { background-color: rgba($bd-purple, .15); border: 1px solid rgba($bd-purple, .15); diff --git a/site/assets/scss/docs.scss b/site/assets/scss/docs.scss index e3066ed2e2..8b7bc99a15 100644 --- a/site/assets/scss/docs.scss +++ b/site/assets/scss/docs.scss @@ -28,6 +28,12 @@ @import "../../../scss/variables"; @import "../../../scss/mixins"; +// fusv-disable +$enable-grid-classes: false; // stylelint-disable-line scss/dollar-variable-default +$enable-cssgrid: true; // stylelint-disable-line scss/dollar-variable-default +// fusv-enable +@import "../../../scss/grid"; + // Load docs components @import "variables"; @import "navbar"; diff --git a/site/content/docs/5.0/layout/css-grid.md b/site/content/docs/5.0/layout/css-grid.md new file mode 100644 index 0000000000..e6165fc561 --- /dev/null +++ b/site/content/docs/5.0/layout/css-grid.md @@ -0,0 +1,266 @@ +--- +layout: docs +title: CSS Grid +description: Learn how to enable, use, and customize our alternate layout system built on CSS Grid with examples and code snippets. +group: layout +toc: true +--- + +Bootstrap's default grid system represents the culmination of over a decade of CSS layout techniques, tried and tested by millions of people. But, it was also created without many of the modern CSS features and techniques we're seeing in browsers like the new CSS Grid. + +{{< callout warning >}} +**Heads up—our CSS Grid system is experimental and opt-in for v5.1.0!** We included it in our documentation's CSS to demonstrate it for you, but it's disabled by default. Keep reading to learn how to enable it in your projects. +{{< /callout >}} + +## How it works + +With Bootstrap 5, we've added the option to enable a separate grid system that's built on CSS Grid, but with a Bootstrap twist. You still get classes you can apply on a whim to build responsive layouts, but with a different approach under the hood. + +- **CSS Grid is opt-in.** Disable the default grid system by setting `$enable-grid-classes: false` and enable the CSS Grid by setting `$enable-cssgrid: true`. Then, recompile your Sass. + +- **Replace instances of `.row` with `.grid`.** The `.grid` class sets `display: grid` and creates a `grid-template` that you build on with your HTML. + +- **Replace `.col-*` classes with `.g-col-*` classes.** This is because our CSS Grid columns use the `grid-column` property instead of `width`. + +- **Columns and gutter sizes are set via CSS variables.** Set these on the parent `.grid` and customize however you want, inline or in a stylesheet, with `--bs-columns` and `--bs-gap`. + +In the future, Bootstrap will likely shift to a hybrid solution as the `gap` property has achieved nearly full browser support for flexbox. + +## Key differences + +Compared to the default grid system: + +- Flex utilities don't affect the CSS Grid columns in the same way. + +- Gaps replaces gutters. The `gap` property replaces the horizontal `padding` from our default grid system and functions more like `margin`. + +- As such, unlike `.row`s, `.grid`s have no negative margins and margin utilities cannot be used to change the grid gutters. Grid gaps are applied horizontally and vertically by default. See the [customizing section](#customizing) for more details. + +- Inline and custom styles should be viewed as replacements for modifier classes (e.g., `style="--bs-columns: 3;"` vs `class="row-cols-3"`). + +- Nesting works similarly, but may require you to reset your column counts on each instance of a nested `.grid`. See the [nesting section](#nesting) for details. + +## Examples + +### Three columns + +Three equal-width columns across all viewports and devices can be created by using the `.g-col-4` classes. Add [responsive classes](#responsive) to change the layout by viewport size. + +{{< example class="bd-example-cssgrid" >}} +