TExcelTAD
Home › Bootstrap › BS5 Grid System

Bootstrap 5 Grid System

You met the grid in Grid Basics. This section goes deeper: the six breakpoint tiers, how classes cascade from small to large, and the tools for fine control — row-cols-*, offsets, ordering, alignment, nesting and gutters.

The six tiers

Every column class can carry a breakpoint infix. A class applies from its breakpoint upward, until a larger one overrides it.

xs sm md lg xl xxl
Viewport < 576px ≥ 576px ≥ 768px ≥ 992px ≥ 1200px ≥ 1400px
.container max-width 100% 540px 720px 960px 1140px 1320px
Class prefix .col- .col-sm- .col-md- .col-lg- .col-xl- .col-xxl-

So class="col-12 col-md-6 col-xl-4" reads: full width on phones, half from 768px, a third from 1200px.

Note: Breakpoints are measured on the preview frame, which is as wide as the lesson column (often around the md tier). To test tiers, resize your browser window and the frame follows. Each example in this section shows a badge with the active tier.

Mixing tiers

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <style>.row > * > div { background: #e9dffb; border: 1px solid #7952b3; padding: .5rem; font-size: .85rem; }</style>
</head>
<body class="py-2">
  <div class="container">
    <p class="small mb-2">Active tier:
      <span class="badge text-bg-secondary d-sm-none">xs</span><span class="badge text-bg-secondary d-none d-sm-inline d-md-none">sm</span><span class="badge text-bg-secondary d-none d-md-inline d-lg-none">md</span><span class="badge text-bg-secondary d-none d-lg-inline d-xl-none">lg</span><span class="badge text-bg-secondary d-none d-xl-inline d-xxl-none">xl</span><span class="badge text-bg-secondary d-none d-xxl-inline">xxl</span>
    </p>
    <div class="row g-2">
      <div class="col-12 col-md-8"><div>col-12 col-md-8</div></div>
      <div class="col-6 col-md-4"><div>col-6 col-md-4</div></div>
      <div class="col-6 col-md-4"><div>col-6 col-md-4</div></div>
      <div class="col-12 col-md-8"><div>col-12 col-md-8</div></div>
    </div>
  </div>
</body>
</html>

Row columns

Instead of sizing each column, row-cols-{n} on the row sets how many columns fit per line. It's ideal for card grids where all items are the same width.

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <style>.col > div { background: #d1e7dd; border-radius: .5rem; padding: .75rem; text-align: center; }</style>
</head>
<body class="py-2">
  <div class="container">
    <div class="row row-cols-2 row-cols-sm-3 row-cols-lg-6 g-2">
      <div class="col"><div>🍎</div></div><div class="col"><div>🍌</div></div>
      <div class="col"><div>🍒</div></div><div class="col"><div>🍇</div></div>
      <div class="col"><div>🍉</div></div><div class="col"><div>🍑</div></div>
    </div>
  </div>
</body>
</html>

Offsets, ordering and alignment

Class Effect
.offset-{n} / .offset-md-{n} Push a column right by n columns
.ms-auto / .me-auto Push to the far end / start
.order-{0-5}, .order-first, .order-last (responsive too) Change visual order
.justify-content-{start,center,end,between,around,evenly} on the row Horizontal alignment of columns
.align-items-{start,center,end} on the row Vertical alignment of all columns
.align-self-{start,center,end} on a column Vertical alignment of one column
html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <style>.row { background: #f8f9fa; margin-bottom: .5rem; } .row > div { background: #cfe2ff; border: 1px solid #0d6efd; padding: .35rem; font-size: .8rem; }</style>
</head>
<body class="py-2">
  <div class="container">
    <div class="row"><div class="col-4 offset-4">col-4 offset-4</div></div>
    <div class="row justify-content-between"><div class="col-3">start</div><div class="col-3">between</div><div class="col-3">end</div></div>
    <div class="row">
      <div class="col order-last">First in HTML (order-last)</div>
      <div class="col">Second</div>
      <div class="col order-first">Third (order-first)</div>
    </div>
    <div class="row align-items-center" style="height: 70px;">
      <div class="col">centred</div><div class="col align-self-end">align-self-end</div>
    </div>
  </div>
</body>
</html>

Nesting and gutters

Put a new .row inside a column to nest a grid — the inner row again has 12 columns relative to its parent column. Gutters are controlled on the row: g-* (both directions), gx-* (horizontal), gy-* (vertical), from 0 to 5.

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <style>.box { background: #fff3cd; border: 1px solid #ffc107; padding: .4rem; font-size: .8rem; }</style>
</head>
<body class="py-2">
  <div class="container">
    <div class="row gx-4">
      <div class="col-8">
        <div class="box mb-2">Outer col-8 containing a nested row:</div>
        <div class="row g-1">
          <div class="col-6"><div class="box">nested col-6</div></div>
          <div class="col-6"><div class="box">nested col-6</div></div>
        </div>
      </div>
      <div class="col-4"><div class="box h-100">col-4</div></div>
    </div>
    <div class="row g-0 mt-3">
      <div class="col"><div class="box">g-0:</div></div><div class="col"><div class="box">no</div></div><div class="col"><div class="box">gutters</div></div>
    </div>
  </div>
</body>
</html>

Tip: The rule of thumb: containers hold rows, rows hold columns, content goes in columns. Breaking it (content directly in a row, a row without a container) causes stray horizontal scrollbars or misaligned edges.

Test yourself

What width does col-12 col-md-6 col-xl-4 have at 1000px?

  1. 12 columns
  2. 6 columns
  3. 4 columns
  4. Auto

What does row-cols-sm-3 do?

  1. Sets each column to 3 of 12 from sm up
  2. Puts 3 columns per line from sm up
  3. Adds 3 rows
  4. Adds gutters of size 3

Which class moves a column 2 columns to the right?

  1. .ms-2
  2. .offset-2
  3. .push-2
  4. .order-2