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-6takes 6 of 12 parts (half).col-4takes a third.- Plain
colshares 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.
<!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 yourselfResponsive 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.
<!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 yourselfmd), four per row on laptops (lg). g-2 adds a gap between columns.- container > row > col.
- A row has 12 parts:
col-6is half,col-4a third. - Breakpoints
sm md lg xlchange the layout by screen size.
