By the end of this lesson, you'll be able to:
:information_source: Bootstrap Grid System is a responsive layout system that uses containers, rows, and columns to create flexible web page layouts. These layouts automatically adapt to different screen sizes and devices, making your website look great everywhere!
Bootstrap's grid system helps you:
To create layouts with Bootstrap grid, you need three building blocks:
:information_source: Info A Bootstrap container is the outermost wrapper of your grid system. It creates a responsive box that holds all your page content and ensures proper alignment and spacing.
Think of a container like a box that holds all your content. Your page structure looks like this:
<body>
tag (your whole HTML page)<div class="container">
or <div class="container-fluid">
The container in the body
Bootstrap gives you two container options:
.container
- Has a fixed maximum width
.container-fluid
- Always full width
:bulb: Tip Use
.container
for regular websites and.container-fluid
when you want your content to stretch edge-to-edge!
The difference between .container and .container-fluid
Bootstrap's grid system divides each row into 12 equal parts. You can use these parts individually or combine them to create different layouts.
Rows group your columns together horizontally. Here's what you need to know:
class="row"
to a divThe basic structure looks like this:
<div class="container">
<div class="row">
<!-- Columns go here -->
</div>
</div>
:memo: Note Why rows need containers: Rows have special negative margins that containers balance out with padding. Without a container, you'll get unwanted horizontal scrolling!
The illustration of the row output in the browser
Columns are the vertical divisions where you put your actual content. They're like cells in a table:
class="col"
to create a columnHere's a simple column:
<div class="container">
<div class="row">
<div class="col">
<!-- Your content goes here -->
</div>
</div>
</div>
To create multiple columns, just add more divs with the col
class:
<div class="container">
<div class="row">
<div class="col">
Column 1 content
</div>
<div class="col">
Column 2 content
</div>
<div class="col">
Column 3 content
</div>
</div>
</div>
When you use class="col"
, Bootstrap automatically calculates column widths:
Let's see how 4 equal columns work:
Example:
HTML Input:
<div class="container">
<div class="row">
<div class="col">
<div class="bg-primary text-white p-2 text-center">Column 1</div>
</div>
<div class="col">
<div class="bg-secondary text-white p-2 text-center">Column 2</div>
</div>
<div class="col">
<div class="bg-success text-white p-2 text-center">Column 3</div>
</div>
<div class="col">
<div class="bg-danger text-white p-2 text-center">Column 4</div>
</div>
</div>
</div>
Output:
[Column 1] [Column 2] [Column 3] [Column 4]
Each column takes up 25% of the container width (12 ÷ 4 = 3 columns each)
Bootstrap divides each row into 12 equal parts. You can specify how many parts each column should take:
.col-
followed by a number (1-12).col-4
(because 4 x 3 = 12)Here's an example with different column sizes:
Example:
HTML Input:
<div class="container">
<div class="row">
<div class="col-2">
<div class="bg-primary text-white p-2 text-center">col-2</div>
</div>
<div class="col-6">
<div class="bg-secondary text-white p-2 text-center">col-6</div>
</div>
<div class="col-4">
<div class="bg-success text-white p-2 text-center">col-4</div>
</div>
</div>
</div>
Output:
[small] [ large ] [ medium ]
col-2 col-6 col-4
(2+6+4 = 12 columns total)
:bulb: Tip Column Math Tips:
Try this interactive example to see columns in action: https://codepen.io/cristinaconacel/pen/xyaQNw
:information_source: Breakpoints are specific screen sizes where your layout changes. Think of them as "switching points" where your design adapts to different devices.
Bootstrap uses breakpoints to help your site look great on:
When you add a breakpoint to a column class, you're saying: "Apply this layout when the screen is at least this size."
The format is: col-[prefix]-[number]
Prefix | Grid breakpoints | Viewport Size | Device size |
---|---|---|---|
.col- |
extra small | <576px | Mobile (Portrait) |
.col-sm- |
small | >=576px | Mobile (Landscape) |
.col-md- |
medium | >=768px | Tablets |
.col-lg- |
large | >=992px | Laptops |
.col-xl- |
extra large | >=1200px | Laptops & Desktops |
Let's break down what class="col-md-4"
means:
col
= this is a columnmd
= applies to medium screens and larger4
= takes up 4 out of 12 columns:memo: Note Important Rule: When you set a breakpoint, it applies to that screen size and all larger sizes too! So
col-sm-6
works on small, medium, large, and extra-large screens.
Example:
HTML Input:
<div class="container">
<div class="row">
<div class="col-xl-3">
<div class="bg-primary text-white p-2 text-center">Column 1</div>
</div>
<div class="col-xl-3">
<div class="bg-secondary text-white p-2 text-center">Column 2</div>
</div>
<div class="col-xl-3">
<div class="bg-success text-white p-2 text-center">Column 3</div>
</div>
<div class="col-xl-3">
<div class="bg-danger text-white p-2 text-center">Column 4</div>
</div>
</div>
</div>
Output:
[Col 1] [Col 2] [Col 3] [Col 4]
(4 columns in one row)[Column 1]
[Column 2]
[Column 3]
[Column 4]
Want your website to look different on phones versus computers? You can combine multiple column classes! This lets you create layouts that adapt perfectly to each device.
Simply add multiple col-
classes to the same element:
Example:
HTML Input:
<div class="container">
<div class="row">
<div class="col-md-6 col-xl-3">
<div class="bg-primary text-white p-2 text-center">Column 1</div>
</div>
<div class="col-md-6 col-xl-3">
<div class="bg-secondary text-white p-2 text-center">Column 2</div>
</div>
<div class="col-md-6 col-xl-3">
<div class="bg-success text-white p-2 text-center">Column 3</div>
</div>
<div class="col-md-6 col-xl-3">
<div class="bg-danger text-white p-2 text-center">Column 4</div>
</div>
</div>
</div>
Output:
Viewport ``size >= 1200``px (Extra Large):
[Column 1] [Column 2] [Column 3] [Column 4]
Following .col-xl-3 - 4 columns in one row
Viewport ``size >= 768``px ``but < 1200``px (Medium):
[ Column 1 ] [ Column 2 ]
[ Column 3 ] [ Column 4 ]
Following .col-md-6 - 2 columns per row
Viewport `size768 < 7318`px (Small):
[Column 1]
[Column 2]
[Column 3]
[Column 4]
Default stacking - 1 column per row
Let's recap what you've learned about Bootstrap Grid:
Three Building Blocks:
.container
or .container-fluid
)class="row"
)class="col"
)The 12-Column System:
col-[number]
to specify column widthResponsive Breakpoints:
col-
-> Extra small (phones)col-sm-
-> Small (landscape phones)col-md-
-> Medium (tablets)col-lg-
-> Large (laptops)col-xl-
-> Extra large (desktops)Pro Tips:
Bootstrap Grid System (Part 1) Video Tutorial
Bootstrap Grid System (Part 2) Video Tutorial
Code with AI: Use AI to generate Bootstrap grid layouts.
Prompts:
Try these exercises to master Bootstrap Grid:
Basic Layout Challenge:
Responsive Challenge:
Mixed Sizes Challenge:
Real-World Project:
Remember: The best way to learn Bootstrap Grid is by building lots of layouts. Start simple and gradually make them more complex!