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.
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.
$enable-grid-classes: false
and enable the CSS Grid by setting $enable-cssgrid: true
. Then, recompile your Sass.row
with grid
. The grid
attributes sets display: grid
and creates a grid-template
that you build on with your HTML.col="*"
classes with GridCol
components. This is because our CSS Grid columns use the GridCol
component instead of width
.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.
Three equal-width columns across all viewports and devices can be created by using the size="4"
atttibutes. Add responsive classes to change the layout by viewport size.
<template>
<Grid>
<GridCol :size="4">
.g-col-4
</GridCol>
<GridCol :size="4">
.g-col-4
</GridCol>
<GridCol :size="4">
.g-col-4
</GridCol>
</Grid>
</template>
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.
<template>
<Grid>
<GridCol size="6 md-4">
.g-col-6 .g-col-md-4
</GridCol>
<GridCol size="6 md-4">
.g-col-6 .g-col-md-4
</GridCol>
<GridCol size="6 md-4">
.g-col-6 .g-col-md-4
</GridCol>
</Grid>
</template>
Compare that to this two column layout at all viewports.
<template>
<Grid>
<GridCol size="6">
.g-col-6
</GridCol>
<GridCol size="6">
.g-col-6
</GridCol>
</Grid>
</template>
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.
<template>
<Grid>
<GridCol :size="6">
.g-col-6
</GridCol>
<GridCol :size="6">
.g-col-6
</GridCol>
<GridCol :size="6">
.g-col-6
</GridCol>
<GridCol :size="6">
.g-col-6
</GridCol>
</Grid>
</template>
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 column-start
and column-end
. Start classes are shorthand for the former. Pair them with the column classes to size and align your columns however you need. Start classes begin at 1
as 0
is an invalid value for these properties.
<template>
<Grid>
<GridCol
:size="3"
:colmun-start="2"
>
.g-col-3 .g-start-2
</GridCol>
<GridCol
:size="4"
:colmun-start="6"
>
>.g-col-4 .g-start-6
</GridCol>
</Grid>
</template>
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.
<template>
<Grid>
<b-div>1</b-div>
<b-div>1</b-div>
<b-div>1</b-div>
<b-div>1</b-div>
<b-div>1</b-div>
<b-div>1</b-div>
<b-div>1</b-div>
<b-div>1</b-div>
<b-div>1</b-div>
<b-div>1</b-div>
<b-div>1</b-div>
<b-div>1</b-div>
</Grid>
</template>
This behavior can be mixed with grid column classes.
<template>
<Grid>
<GridCol :size="6">
.g-col-6
</GridCol>
<b-div>1</b-div>
<b-div>1</b-div>
<b-div>1</b-div>
<b-div>1</b-div>
<b-div>1</b-div>
<b-div>1</b-div>
</Grid>
</template>
Similar to our default grid system, our CSS Grid allows for easy nesting of Grid
s. However, unlike the default, this grid inherits changes in the rows, columns, and gaps. Consider the example below:
--bs-columns: 3
.Grid
to 12 (our default).In practice this allows for more complex and custom layouts when compared to our default grid system.
<template>
<Grid :columns="3">
<GridCol>
First auto-column
<Grid>
<GridCol>Auto-column</GridCol>
<GridCol>Auto-column</GridCol>
</Grid>
</GridCol>
<GridCol>
Second auto-column
<Grid :columns="12">
<GridCol :size="6">
6 of 12
</GridCol>
<GridCol :size="4">
4 of 12
</GridCol>
<GridCol :size="2">
2 of 12
</GridCol>
</Grid>
</GridCol>
<GridCol>Third auto-column</GridCol>
</Grid>
</template>
Customize the number of columns, the number of rows, and the width of the gaps with local CSS variables.
Variable | Fallback value | Description |
---|---|---|
--bs-rows | 1 | The number of rows in your grid template |
--bs-columns | 12 | The number of columns in your grid template |
--bs-gap | 1.5rem | The size of the gap between columns (vertical and horizontal) |
These CSS variables have no default value; instead, they apply fallback values that are used _until_ a local instance is provided. For example, we use var(--bs-rows, 1)
for our CSS Grid rows, which ignores --bs-rows
because that hasn't been set anywhere yet. Once it is, the Grid
instance will use that value instead of the fallback value of 1
.
Immediate children elements of Grid
are grid items, so they'll be sized without explicitly adding a GridCol
class.
<template>
<Grid :columns="3">
<GridCol>Auto-column</GridCol>
<GridCol>Auto-column</GridCol>
<GridCol>Auto-column</GridCol>
</Grid>
</template>
Adjust the number of columns and the gap.
<template>
<Grid
:columns="4"
gap="5rem"
>
<GridCol :size="2">
.g-col-2
</GridCol>
<GridCol :size="2">
.g-col-2
</GridCol>
</Grid>
</template>
<template>
<Grid
:columns="10"
gap="1rem"
>
<GridCol :size="2">
.g-col-6
</GridCol>
<GridCol :size="2">
.g-col-4
</GridCol>
</Grid>
</template>
Adding more rows and changing the placement of columns:
<template>
<Grid
:rows="3"
:columns="3"
>
<GridCol>Auto-column</GridCol>
<GridCol
:colmun-start="2"
:row="2"
>
Auto-column
</GridCol>
<GridCol
:colmun-start="3"
:row="3"
>
Auto-column
</GridCol>
</Grid>
</template>
Change the vertical gaps only by modifying the row-gap
. Note that we use gap
on .grid
s, but row-gap
and column-gap
can be modified as needed.
<template>
<Grid :row-gap="0">
<GridCol :size="6">
.g-col-6
</GridCol>
<GridCol :size="6">
.g-col-6
</GridCol>
<GridCol :size="6">
.g-col-6
</GridCol>
<GridCol :size="6">
.g-col-6
</GridCol>
</Grid>
</template>
Because of that, you can have different vertical and horizontal gap
s, which can take a single value (all sides) or a pair of values (vertical and horizontal). This can be applied with an inline style for gap
, or with our --bs-gap
CSS variable.
<template>
<Grid gap=".25rem 1rem">
<GridCol :size="6">
.g-col-6
</GridCol>
<GridCol :size="6">
.g-col-6
</GridCol>
<GridCol :size="6">
.g-col-6
</GridCol>
<GridCol :size="6">
.g-col-6
</GridCol>
</Grid>
</template>