Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 58 ยท Layout

The Grid System

Learn the Bootstrap 5 grid: container, row and col, the 12-column layout, equal-width columns, col-auto, single column widths and how flexbox powers it.

The grid is the feature Bootstrap is best known for. It lets you divide a page into rows and columns with nothing but class names, and those columns can rearrange themselves on different screen sizes. A three-column page on a desktop, one column on a phone, with no media queries written by you: that is the grid at work.

In this lesson you will learn the basic structure (container, row, column), the idea of twelve columns, equal-width and automatic columns, and how the grid is built on CSS flexbox. Responsive columns come in the next lesson; here we stay with the fundamentals.

Container, row, column

A grid always has three levels, and the nesting order matters:

  1. A container limits the width and adds side padding.
  2. A .row is a horizontal band. It holds the columns and cancels the container's padding (with negative margins) so the columns align with the container edge.
  3. .col classes are the columns. Your actual content goes inside the columns, not directly in the row.
HTML
<div class="container">
  <div class="row">
    <div class="col">Column 1</div>
    <div class="col">Column 2</div>
    <div class="col">Column 3</div>
  </div>
</div>

A common beginner mistake is to put text or images directly inside .row (without a column) or to put a .col outside a .row. Both break the spacing, because the negative margins of rows and the padding of columns are designed as a pair.

Equal-width columns

.col with no number means "share the space equally". Two .col elements are 50% each, three are 33.33% each, and so on. The columns stay on one line (they never wrap on their own as long as they fit) and they have equal height by default because the row is a flex container. Try adding a fourth column in the next editor.

Equal-width columns

The inner div.box elements are only there so you can see each column's extent. The column itself has padding (the gutter), so a background applied directly to .col would also color the gutter. In real layouts, put the colored element, such as a card, inside the column.

The twelve-column layout

Bootstrap divides every row into 12 equal slices. A class like col-4 means "occupy 4 of the 12 slices", which is 33.33%. Twelve works well because it divides evenly by 2, 3, 4 and 6, so you can build halves, thirds, quarters and sixths easily. The numbers in one row should add up to 12 (or less, to leave empty space).

Class Width Class Width
col-1 8.33% col-7 58.33%
col-2 16.67% col-8 66.67%
col-3 25% col-9 75%
col-4 33.33% col-10 83.33%
col-5 41.67% col-11 91.67%
col-6 50% col-12 100%

You can mix numbered and unnumbered columns. A numbered column takes exactly its share, and the unnumbered ones split whatever is left. In col-3 + col + col, the first is a quarter and the other two share the remaining 75% equally (37.5% each).

Setting one column's width

The last row adds up to only 10, so the right 2/12 stays empty. If a row adds up to more than 12, the extra columns wrap onto a new line (more on that in Gutters, Alignment and Ordering).

flowchart TD
  C["div.container"] --> R["div.row"]
  R --> A["div.col-4"]
  R --> B["div.col-8"]
  A --> AC["your content"]
  B --> BC["your content"]

Content goes inside columns; columns go inside rows; rows go inside a container.

Automatic-width columns: col-auto

.col-auto sizes a column to its content instead of to a fraction of the row. It is ideal for buttons, icons or short labels that should not stretch. Neighbouring .col columns then fill the remaining space. The next example builds a typical search bar: a fixed-size label, a stretching field and a button that takes only the space it needs.

col-auto next to a flexible column

The label and the button are only as wide as their text, and the input stretches to fill the row. Shrink the preview and notice that the elements keep their sizes until the content can no longer fit.

How flexbox powers the grid

Under the hood, .row is simply a flex container with wrapping, and each column is a flex item. The essential CSS is short:

CSS
.row {
  display: flex;
  flex-wrap: wrap;
  margin-right: calc(-.5 * var(--bs-gutter-x));  /* cancels container padding */
  margin-left:  calc(-.5 * var(--bs-gutter-x));
}
.row > * {
  padding-right: calc(var(--bs-gutter-x) * .5);  /* the gutter */
  padding-left:  calc(var(--bs-gutter-x) * .5);
  flex-shrink: 0;
  width: 100%;
  max-width: 100%;
}
.col    { flex: 1 0 0%; }               /* grow equally */
.col-auto { flex: 0 0 auto; width: auto; }
.col-4  { flex: 0 0 auto; width: 33.33333333%; }

So .col means "grow to share the free space", .col-auto means "use the content width", and .col-4 means "a fixed 33.33% width". Knowing this helps you debug: a column that refuses to shrink has flex-shrink: 0, and a row that wraps does so because of flex-wrap: wrap. Flexbox itself is covered in the flex utilities lesson, and you can learn it from scratch in the CSS tutorial.

Do not put a .row directly inside another .row or directly inside a column without thinking about it. Rows inside columns are allowed (that is how nested grids work), but rows must always contain columns. See Nested Grids for the correct pattern.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where should your content (text, images, cards) be placed in the grid?

    Columns provide the gutter padding that offsets the row's negative margins. Content placed directly in a row breaks the alignment.
  2. 2. A row has col-3, col and col. How wide is each .col?

    col-3 takes 25%. The two remaining columns split the other 75% equally, 37.5% each.
  3. 3. What does .col-auto do?

    col-auto uses flex: 0 0 auto and width: auto, so the column is only as wide as its content.
  4. 4. What happens when the column numbers in one row add up to more than 12?

    Rows use flex-wrap: wrap, so columns that do not fit are placed on the next line.

Summary

  • The grid has three levels: container, .row, then .col-* columns that hold your content.
  • Each row has 12 slices; col-4 means 4 of 12 (33.33%).
  • .col shares free space equally, .col-auto fits its content, and numbered columns take a fixed share.
  • Extra columns beyond 12 wrap onto a new line.
  • The grid is flexbox: the row wraps, the columns grow, and gutters are padding plus negative margins.

Next: Responsive Columns.