Bootstrap Tutorial menu

Bootstrap Containers and Grid

Bootstrap's grid lets you build layouts in rows and columns that change with the screen size.

Containers

Every layout starts with a container. It holds your content and adds space on the sides.

ClassWidth
containerA fixed maximum width that grows at each breakpoint, centred
container-fluidAlways 100% of the screen width
container-md100% wide until the md breakpoint, then fixed

Rows and Columns

The grid has 12 columns. Inside a container, put a row, and inside the row put columns. A column's number says how many of the 12 parts it takes.

Example

html
<!DOCTYPE html>
<html lang="en">
<head>
<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, [class*="col-"] { background: #ede7f6; border: 1px solid #7952b3; padding: 10px; }
</style>
</head>
<body>

<div class="container mt-4">
  <div class="row">
    <div class="col-4">col-4</div>
    <div class="col-8">col-8</div>
  </div>
  <div class="row mt-2">
    <div class="col-6">col-6</div>
    <div class="col-3">col-3</div>
    <div class="col-3">col-3</div>
  </div>
  <div class="row mt-2">
    <div class="col">col</div>
    <div class="col">col</div>
    <div class="col">col</div>
  </div>
</div>

</body>
</html>
Try it Yourself »

The small <style> only colours the columns so you can see them. A plain col shares the space equally with the other columns in its row.

The numbers in a row should add up to 12. If they add up to more, the extra columns move down to a new line.

Breakpoints

Add a breakpoint to a column class to decide from which screen size it applies.

BreakpointClass prefixScreen width
Extra smallcol-Under 576px
Smallcol-sm-576px and up
Mediumcol-md-768px and up
Largecol-lg-992px and up
Extra largecol-xl-1200px and up
Extra extra largecol-xxl-1400px and up

col-md-6 means "half the width from 768px upwards". Below 768px, the column takes the full width, so columns stack on phones.

A Responsive Layout

Example

html
<!DOCTYPE html>
<html lang="en">
<head>
<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">
</head>
<body>

<div class="container my-4">
  <div class="row g-3">
    <div class="col-12 col-sm-6 col-lg-3">
      <div class="p-3 bg-light border rounded">Clifton</div>
    </div>
    <div class="col-12 col-sm-6 col-lg-3">
      <div class="p-3 bg-light border rounded">Gulshan</div>
    </div>
    <div class="col-12 col-sm-6 col-lg-3">
      <div class="p-3 bg-light border rounded">Orangi Town</div>
    </div>
    <div class="col-12 col-sm-6 col-lg-3">
      <div class="p-3 bg-light border rounded">Saddar</div>
    </div>
  </div>
</div>

</body>
</html>
Try it Yourself »
ScreenColumns per row
Phone1 (each is col-12)
576px and up2 (col-sm-6)
992px and up4 (col-lg-3)

Gutters

Gutters are the gaps between columns. Change them with g-0 to g-5 on the row. gx- changes only the horizontal gap and gy- only the vertical gap. In the example above, g-3 gives a medium gap in both directions.

Row Columns Shortcut

row-cols-* on the row sets how many columns appear per line, without classes on each column:

Example

html
<div class="row row-cols-1 row-cols-md-3 g-4">
  <div class="col">Card 1</div>
  <div class="col">Card 2</div>
  <div class="col">Card 3</div>
</div>

Offsets and Alignment

  • offset-md-2 pushes a column 2 columns to the right
  • justify-content-center on a row centres columns that do not fill all 12 parts
  • align-items-center on a row lines columns up in the middle vertically

Exercise

Exercise

A column has class="col-12 col-md-4". How wide is it on a tablet that is 800px wide?

Learn Bootstrap with a teacher

Join the Web Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp