TExcelTAD
Home › Bootstrap › BS5 Grid Basic

Bootstrap 5 Grid Basics

Bootstrap's grid splits each row into 12 columns. You decide how many of those 12 each piece of content spans, and you can change that per screen size. The grid is built with Flexbox, so columns automatically share space, align and wrap.

The three building blocks

Class Role
.container (or -fluid) Outer box with padding
.row Horizontal group of columns; a flex container with negative margins that cancel the container padding
.col, .col-*, .col-{bp}-* The columns — direct children of .row

Content goes inside columns, never directly inside a row.

Equal-width columns

Plain .col columns share the row equally, however many there are.

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; }</style>
</head>
<body class="py-3">
  <div class="container">
    <div class="row mb-2">
      <div class="col">1 of 2</div>
      <div class="col">2 of 2</div>
    </div>
    <div class="row">
      <div class="col">1 of 4</div>
      <div class="col">2 of 4</div>
      <div class="col">3 of 4</div>
      <div class="col">4 of 4</div>
    </div>
  </div>
</body>
</html>

Choosing widths out of 12

.col-4 takes 4/12 (one third), .col-8 takes the remaining two thirds. If the numbers in a row add up to more than 12, the extra columns wrap onto a new line.

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: #d1ecf1; border: 1px solid #0aa2c0; padding: .5rem; }</style>
</head>
<body class="py-3">
  <div class="container">
    <div class="row mb-2">
      <div class="col-4">col-4</div>
      <div class="col-8">col-8</div>
    </div>
    <div class="row mb-2">
      <div class="col-3">col-3</div>
      <div class="col">col (fills the rest)</div>
    </div>
    <div class="row">
      <div class="col-9">col-9</div>
      <div class="col-6">col-6 wraps: 9 + 6 &gt; 12</div>
    </div>
  </div>
</body>
</html>

Responsive columns

Add a breakpoint to the class: .col-md-6 means "6 columns wide from 768px up; full width (stacked) below". You can combine several on one element — the largest matching one wins.

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 > div { background: #fff3cd; border: 1px solid #ffc107; padding: .75rem; }</style>
</head>
<body class="py-3">
  <div class="container">
    <p class="small text-muted">Stacked when narrow, 2 per row from sm, 4 per row from lg.</p>
    <div class="row g-2">
      <div class="col-sm-6 col-lg-3"><div>🍎 Apple</div></div>
      <div class="col-sm-6 col-lg-3"><div>🍌 Banana</div></div>
      <div class="col-sm-6 col-lg-3"><div>🍒 Cherry</div></div>
      <div class="col-sm-6 col-lg-3"><div>🍇 Grape</div></div>
    </div>
  </div>
</body>
</html>

Note: The preview frame is the width of the lesson column — usually between the sm and lg breakpoints, so you'll likely see two per row. Make your browser window narrower or wider to see the layout switch. Drag the frame's bottom-right corner if you need more height.

Column class reference

Pattern Meaning
.col Equal share of the row at all sizes
.col-{n} n of 12 columns at all sizes
.col-{bp} Equal share from breakpoint bp up, stacked below
.col-{bp}-{n} n of 12 columns from bp up, stacked below
.col-auto / .col-{bp}-auto Width fits the content

Breakpoints: sm (576px), md (768px), lg (992px), xl (1200px), xxl (1400px).

Gutters

The space between columns is the gutter (1.5rem by default). Change it on the row with g-0 … g-5, or only horizontally/vertically with gx-* and gy-*. In the example above, g-2 adds a smaller gap that also works between wrapped rows.

Tip: Don't add padding or margins to .col elements to space things out — put a box inside the column and use gutters on the row. That keeps the 12-column maths intact.

The full grid system, including offsets, ordering and row-cols-*, is covered in the BS5 Grid section later in the course.

Test yourself

How many columns does a Bootstrap row have?

  1. 10
  2. 12
  3. 16
  4. 24

What does col-md-4 do on a 600px wide screen?

  1. Takes 4/12 of the row
  2. Takes the full width (stacks)
  3. Is hidden
  4. Takes 4px

A row contains col-8 and col-6. What happens?

  1. Both shrink to fit
  2. The col-6 wraps to a new line
  3. The row scrolls horizontally
  4. The col-6 is hidden