CSS grid

Bootstrap 5 CSS grid component

Responsive CSS Grid built with Bootstrap 5. Create flexible, intuitive layouts and enhance your web design with user-friendly, responsive grid systems.


Examples

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 .rows, .grids 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.

In the future, Bootstrap will likely shift to a hybrid solution as the gap property has achieved nearly full browser support for flexbox.

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.

.g-col-4
.g-col-4
.g-col-4
        
            
            <div class="grid">
              <div class="g-col-4">.g-col-4</div>
              <div class="g-col-4">.g-col-4</div>
              <div class="g-col-4">.g-col-4</div>
            </div>
          
        
    

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.

.g-col-6 .g-col-md-4
.g-col-6 .g-col-md-4
.g-col-6 .g-col-md-4
        
            
            <div class="grid">
              <div class="g-col-6 g-col-md-4">.g-col-6 .g-col-md-4</div>
              <div class="g-col-6 g-col-md-4">.g-col-6 .g-col-md-4</div>
              <div class="g-col-6 g-col-md-4">.g-col-6 .g-col-md-4</div>
            </div>
          
        
    

Compare that to this two column layout at all viewports.

        
            
          <div class="grid">
            <div class="g-col-6">.g-col-6</div>
            <div class="g-col-6">.g-col-6</div>
          </div>
        
        
    

Wrapping

Grid elements are automatically wrapped to the next row when there is no more horizontal space. Note that the gap applies to both horizontal and vertical gaps between grid elements.

.g-col-6
.g-col-6
.g-col-6
.g-col-6
        
            
              <div class="grid">
                <div class="g-col-6">.g-col-6</div>
                <div class="g-col-6">.g-col-6</div>
              
                <div class="g-col-6">.g-col-6</div>
                <div class="g-col-6">.g-col-6</div>
              </div>
            
        
    

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. 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.

.g-col-3 .g-start-2
.g-col-4 .g-start-6
        
            
            <div class="grid">
              <div class="g-col-3 g-start-2">.g-col-3 .g-start-2</div>
              <div class="g-col-4 g-start-6">.g-col-4 .g-start-6</div>
            </div>
          
        
    

Auto columns

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.

1
1
1
1
1
1
1
1
1
1
1
1
        
            
            <div class="grid">
              <div>1</div>
              <div>1</div>
              <div>1</div>
              <div>1</div>
              <div>1</div>
              <div>1</div>
              <div>1</div>
              <div>1</div>
              <div>1</div>
              <div>1</div>
              <div>1</div>
              <div>1</div>
            </div>
          
        
    

This behavior can be mixed with grid column classes.

.g-col-6
1
1
1
1
1
1
        
            
            <div class="grid">
              <div class="g-col-6">.g-col-6</div>
              <div>1</div>
              <div>1</div>
              <div>1</div>
              <div>1</div>
              <div>1</div>
              <div>1</div>
            </div>
          
        
    

Nesting

Similar to our default grid system, our CSS Grid allows for easy nesting of .grids. However, unlike the default, this grid inherits changes in the rows, columns, and gaps. Consider the example below:

  • 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-col- In the seumn has no nested content.

In practice this allows for more complex and custom layouts when compared to our default grid system.

First auto-column
Auto-column
Auto-column
Second auto-column
6 of 12
4 of 12
2 of 12
Third auto-column
        
            
            <div class="grid" style="--bs-columns: 3;">
              <div>
                First auto-column
                <div class="grid">
                  <div>Auto-column</div>
                  <div>Auto-column</div>
                </div>
              </div>
              <div>
                Second auto-column
                <div class="grid" style="--bs-columns: 12;">
                  <div class="g-col-6">6 of 12</div>
                  <div class="g-col-4">4 of 12</div>
                  <div class="g-col-2">2 of 12</div>
                </div>
              </div>
              <div>Third auto-column</div>
            </div>