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.
How it works
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.
Bootstrap CSS Grid Three columns
Three equal-width columns across all viewports and devices can be created by using the .g-col-4 classes. Add responsive classes to change the layout by viewport size.
Sample Code
Output
Bootstrap CSS Grid Responsive Example 1
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.
Sample Code
Output
Bootstrap CSS Grid Responsive Example 2
Sample Code
Output
Bootstrap CSS Grid Wrapping
Grid items automatically wrap to the next line when there’s no more room horizontally. Note that the gap applies to horizontal and vertical gaps between grid items.
Sample Code
Output
Bootstrap CSS Grid Starts
Start classes aim to replace our default grid’s offset classes, but they’re not entirely the same. CSS Grid creates a grid template 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. Those properties are grid-column-start and grid-column-end.
Sample Code
Output
Bootstrap CSS Grid Auto columns Example 1
When there are no classes on the grid items (the immediate children of a .grid), each grid item will automatically be sized to one column.
Sample Code
Output
Bootstrap CSS Grid Auto columns Example 2
Sample Code
Output
Bootstrap CSS Grid Nesting
We override the default number of columns with a local CSS variable: --bs-columns: 3.
In the first auto-column, the column count is inherited and each column is one-third of the available width.
In the second auto-column, we’ve reset the column count on the nested .grid to 12 (our default).
The third auto-column has no nested content.
Sample Code
Output
Bootstrap CSS No grid classes
Immediate children elements of .grid are grid items, so they’ll be sized without explicitly adding a .g-col class.
Sample Code
Output
Bootstrap CSS Grid Columns and Gaps Example 1
Adjust the number of columns and the gap.
Sample Code
Output
Bootstrap CSS Grid Columns and Gaps Example 2
Sample Code
Output
Bootstrap CSS Grid Adding Rows
Adding more rows and changing the placement of columns.
Sample Code
Output
Bootstrap CSS Grid Gaps Example 1
Change the vertical gaps only by modifying the row-gap. Note that we use gap on .grids, but row-gap and column-gap can be modified as needed.
Sample Code
Output
Bootstrap CSS Grid Gaps Example 2
Sample Code
Output
Bootstrap CSS Grid 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. This effectively predetermines the number of classes generated in our compiled CSS.
For example, you can increase the column count and change the gap size, and then size your “columns” with a mix of inline styles and predefined CSS Grid column classes (e.g., .g-col-4).