← Learning Hub
Beginner

Containers and the Grid

A .container keeps content centred with space at the sides. Inside it, the grid places things in rows and columns.

  • A row is split into 12 equal parts.
  • col-6 takes 6 of 12 parts (half). col-4 takes a third.
  • Plain col shares the space equally.

Why 12? Because 12 divides nicely: into 2 halves, 3 thirds, 4 quarters or 6 sixths. So almost any layout you can imagine - a sidebar and main area (3 + 9), three equal cards (4 + 4 + 4) - can be made from 12 columns. Rows must be inside a container, and columns inside a row.

Example
<!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 class="p-3">
    <div class="container">
      <div class="row">
        <div class="col-8 bg-primary-subtle border p-2">col-8 (two thirds)</div>
        <div class="col-4 bg-warning-subtle border p-2">col-4</div>
      </div>
      <div class="row mt-2">
        <div class="col bg-success-subtle border p-2">col</div>
        <div class="col bg-success-subtle border p-2">col</div>
        <div class="col bg-success-subtle border p-2">col</div>
      </div>
    </div>
  </body>
</html>
▶ Try it yourself

Responsive columns

Add a screen size to change the layout on bigger screens: sm (576px+), md (768px+), lg (992px+), xl (1200px+).

Responsive classes let one page adapt to every screen without writing media queries. Read col-md-6 as "from medium screens upward, take 6 of 12 columns". Below that size, columns stack full width, which is usually what you want on a phone. Combine several, like col-12 col-md-6 col-lg-3, for different layouts at different sizes.

Example
<!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 class="p-3">
    <div class="container">
      <div class="row g-2">
        <div class="col-12 col-md-6 col-lg-3"><div class="border p-3">Product 1</div></div>
        <div class="col-12 col-md-6 col-lg-3"><div class="border p-3">Product 2</div></div>
        <div class="col-12 col-md-6 col-lg-3"><div class="border p-3">Product 3</div></div>
        <div class="col-12 col-md-6 col-lg-3"><div class="border p-3">Product 4</div></div>
      </div>
    </div>
  </body>
</html>
▶ Try it yourself
Above: one per row on phones, two per row on tablets (md), four per row on laptops (lg). g-2 adds a gap between columns.
Key points
  • container > row > col.
  • A row has 12 parts: col-6 is half, col-4 a third.
  • Breakpoints sm md lg xl change the layout by screen size.

📝 Test yourself

1. How many parts is a Bootstrap row split into?
2. Which class makes a column half the row width?
3. Fill in the class that holds columns:
<div class="">