bootstrap/site/content/docs/5.0/layout/css-grid.md
2020-11-20 20:20:51 +02:00

8.7 KiB

layout title description status group toc
docs CSS Grid Learn how to enable, use, and customize our alternate layout system built on CSS Grid with examples and code snippets. experimental layout true

Overview

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 so many of the modern CSS features and techniques that we're seeing in browsers—features like the new CSS Grid.

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.

How it works

  • Disable the default grid system by setting $enable-grid-classes: false.
  • Enable the CSS Grid by setting $enable-cssgrid: true and recompiling your Sass.
  • Replace instances of .row with .grid. .grids set display: grid and create a grid-template that you build on with your HTML.
  • The number of columns and the width of the gutters are set via CSS variables on the .grid, which means you can customize those values on the fly: --columns and --gap.

Key differences

Compared to the default grid system:

  • Flex utilities won't affect grid columns. Note that columns can still be flex containers.
  • There's no padding on columns as gutters function more like margins.
  • Unlike .rows, .grids have no negative margins.
  • Margin utilities cannot be used to change the grid gutters. See the customizing section.
  • Grid gutters are applied horizontally and vertically by default. See the customizing section.
  • Inline and custom styles should be viewed as replacements for modifier classes (e.g., style="--columns: 3;" vs class="row-cols-3").

Examples

Three columns

Three equal-width columns across all viewports and devices can be created by using the .g-col-4 classes.

{{< example class="bd-example-cssgrid" >}}

.g-col-4
.g-col-4
.g-col-4
{{< /example >}}

No column classes

When there are no classes on the grid items (the immediate children of a .grid), each one defaults to one column's width.

{{< example class="bd-example-cssgrid" >}}

...
...
...
...
...
...
...
...
...
...
...
...
{{< /example >}}

Responsive

Use responsive classes to adjust your layout across viewports. Here we start with two columns on the narrowest viewports, and then grow to three columns on medium viewports and above.

{{< example class="bd-example-cssgrid" >}}

.g-col-6 .g-col-md-4
.g-col-6 .g-col-md-4
.g-col-6 .g-col-md-4
{{< /example >}}

{{< example class="bd-example-cssgrid" >}}

.g-col-6
.g-col-6
{{< /example >}}

Wrapping

Grid items automatically wrap to the next line when there's no more room horizontally. Note that the grid-gap applies to horizontal and vertical gutters between grid items.

{{< example class="bd-example-cssgrid" >}}

.g-col-6
.g-col-6
.g-col-6
.g-col-6
{{< /example >}}

Starts

Start classes replace our default grid's offsets, but they're not the same. CSS Grid creates columns through styles that tell browsers to "start at this column" and "end at this column." Those properties are grid-column-start and grid-column-end. Start classes are shorthand for the former.

By comparison, our default grid's offsets use margin-left to nudge a column.

{{< example class="bd-example-cssgrid" >}}

.g-col-3 .g-start-2
.g-col-4 .g-start-6
{{< /example >}}

Auto columns

Any immediate child of .grid will automatically be sized to one column.

{{< example class="bd-example-cssgrid" >}}

1
1
1
1
1
1
1
1
1
1
1
1
{{< /example >}}

This behavior can be mixed with grid column classes.

{{< example class="bd-example-cssgrid" >}}

.g-col-6
1
1
1
1
1
1
{{< /example >}}

Nesting

Because we set the column count, row count, and gutter size on the .grid class, it's not automatically inherited by every nested instance of .grid. As such, you'll need to specifically tell the .grid class to inherit those options, or redeclare them.

{{< example class="bd-example-cssgrid" >}}

Auto-column
Auto-column
Auto-column
Auto-column
6 of 12
4 of 12
2 of 12
Auto-column
{{< /example >}}

Customizing

Customize the number of columns, the number of rows, and the width of the gutters with local CSS variables.

{{< bs-table "table" >}}

Variable Default value Description
--rows 1 The number of rows in your grid template
--columns 12 The number of columns in your grid template
--gap 1.5rem The size of the gap between columns (vertical and horizontal)
{{< /bs-table >}}

Because these CSS variables are local to the .grid class, you can override them whenever you like, as many times as you like, even across media queries.

No grid classes

Immediate children elements of .grid are grid items, so they'll be sized without explicitly adding a .g-col class.

{{< example class="bd-example-cssgrid" >}}

Auto-column
Auto-column
Auto-column
{{< /example >}}

Columns and gutters

Adjust the number of columns and the gap (gutters).

{{< example class="bd-example-cssgrid" >}}

.g-col-2
.g-col-2
{{< /example >}}

{{< example class="bd-example-cssgrid" >}}

.g-col-6
.g-col-4
{{< /example >}}

Adding rows

Adding more rows and changing the placement of columns:

{{< example class="bd-example-cssgrid" >}}

Auto-column
Auto-column
Auto-column
{{< /example >}}

Gutters

Change the vertical gutters only by modifying the grid-row-gap. Note that we use grid-gap on .grids, but grid-row-gap and grid-col-gap can be modified as needed.

{{< example class="bd-example-cssgrid" >}}

.g-col-6
.g-col-6
.g-col-6
.g-col-6
{{< /example >}}

Because of that, you can have different vertical and horizontal gutters with grid-gap, which can take a single value (all sides) or a pair of values (vertical and horizontal).

{{< example class="bd-example-cssgrid" >}}

.g-col-6
.g-col-6
.g-col-6
.g-col-6
{{< /example >}}

Sass

One limitation of the CSS Grid is that our default classes are still generated by two Sass variables, $grid-columns and $grid-gutter-width. You have two options here:

  • Modify those default Sass variables and recompile your CSS.
  • Or, use inline or custom styles to augment the provided classes.

For example, you can customize the column count and gutter, and then size your "columns" with inline styles.

{{< example class="bd-example-cssgrid" >}}

14 columns
.g-col-4
{{< /example >}}