Lesson 7: Building Responsive Layouts with Columns

📌 Objective: Learn how to use Bootstrap’s column system to create responsive page layouts.


🔹 1. Introduction to Bootstrap’s Column System

Bootstrap’s grid system divides a row into 12 equal columns. You can adjust column widths based on screen sizes using breakpoints.

💡 Why Use Bootstrap Columns?
✔️ Creates flexible layouts that adjust to different screen sizes.
✔️ Eliminates the need for custom CSS media queries.
✔️ Easy-to-use column classes (.col-, .col-sm-*, .col-md-*).


🔹 2. Creating Basic Column Layouts

1️⃣ Equal-Width Columns

If you don’t specify a column width, Bootstrap automatically distributes the space equally.

Example: Equal-Width Columns

html
<div class="container">
<div class="row">
<div class="col bg-primary text-white">Column 1</div>
<div class="col bg-secondary text-white">Column 2</div>
<div class="col bg-success text-white">Column 3</div>
</div>
</div>

🔹 What happens?

  • All columns take equal space inside the row.

2️⃣ Defining Custom Column Widths

You can set column sizes by using .col-{size}-{number} classes.

Example: Custom Column Widths

html
<div class="container">
<div class="row">
<div class="col-md-4 bg-info text-white">40% Width</div>
<div class="col-md-8 bg-warning text-white">60% Width</div>
</div>
</div>

🔹 What happens?

  • On medium (md) screens and above, the first column takes 4/12 (33%) and the second column takes 8/12 (66%).
  • On small screens, the columns stack on top of each other.

🔹 3. Creating Responsive Column Layouts

Use breakpoints to define different column widths at different screen sizes.

Example: Responsive Columns

html
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-6 col-lg-4 bg-danger text-white">Column 1</div>
<div class="col-sm-12 col-md-6 col-lg-4 bg-dark text-white">Column 2</div>
<div class="col-sm-12 col-md-12 col-lg-4 bg-primary text-white">Column 3</div>
</div>
</div>

🔹 What happens?

  • On small screens (sm), all columns take full width (col-sm-12) and stack.
  • On medium screens (md), columns take half-width (col-md-6).
  • On large screens (lg), they take one-third width (col-lg-4).

🔹 4. Advanced Column Layouts

1️⃣ Auto-Sizing Columns (.col-auto)

Auto-sized columns adjust to the content size instead of using equal spacing.

Example: Auto-Sized Columns

html
<div class="container">
<div class="row">
<div class="col-auto bg-secondary text-white p-3">Auto Column 1</div>
<div class="col bg-primary text-white p-3">Expands to Fill</div>
</div>
</div>

🔹 What happens?

  • The first column takes only the necessary space for its content.
  • The second column expands to fill the remaining space.

2️⃣ Offset Columns (.offset-*)

Offsets add empty space before a column, useful for centering content.

Example: Centering a Column Using Offsets

html
<div class="container">
<div class="row">
<div class="col-md-6 offset-md-3 bg-success text-white p-3">Centered Column</div>
</div>
</div>

🔹 What happens?

  • The column takes 6/12 width and is pushed 3 columns to the right, centering it.

3️⃣ Reordering Columns (.order-*)

Change the display order of columns using .order-* classes.

Example: Changing Column Order

html
<div class="container">
<div class="row">
<div class="col-md-4 order-md-2 bg-danger text-white">Second</div>
<div class="col-md-4 order-md-1 bg-primary text-white">First</div>
<div class="col-md-4 order-md-3 bg-dark text-white">Third</div>
</div>
</div>

🔹 What happens?

  • On medium screens (md), the second column appears first, the first column appears second, and the third column stays last.

4️⃣ Nesting Columns

You can place rows inside columns to create nested layouts.

Example: Nested Columns

html
<div class="container">
<div class="row">
<div class="col-md-6 bg-light">
<h4>Parent Column</h4>
<div class="row">
<div class="col bg-warning text-white">Nested 1</div>
<div class="col bg-secondary text-white">Nested 2</div>
</div>
</div>
<div class="col-md-6 bg-info text-white">
<h4>Another Column</h4>
</div>
</div>
</div>

🔹 What happens?

  • The first column contains another row with two nested columns inside.

📝 Lesson Recap Quiz

🎯 Test your knowledge of Bootstrap columns.

📌 Multiple-Choice Questions (MCQs)

1️⃣ How many columns does Bootstrap’s grid system have?
a) 8
b) 10
c) 12
d) 16

Correct Answer: c) 12


2️⃣ What happens when you use .col-md-6?
a) The column takes full width on all screens
b) The column takes 6/12 width on medium screens and larger
c) The column stacks on large screens
d) The column disappears on medium screens

Correct Answer: b) The column takes 6/12 width on medium screens and larger


3️⃣ Which class allows a column to automatically adjust its size based on content?
a) .col-12
b) .col-md-6
c) .col-auto
d) .col-fluid

Correct Answer: c) .col-auto


📌 True or False

4️⃣ Bootstrap’s columns always take equal width unless specified.
Answer: True

5️⃣ The .offset-* class moves a column to the left.
Answer: False (It moves the column to the right.)


🎯 Practical Challenge

✅ Create a 3-column layout where:

  • The first column is auto-sized.
  • The second column takes 6/12 width.
  • The third column fills remaining space.
    ✅ Try reordering the columns using .order-* classes.
    ✅ Post your solution in the discussion forum for feedback!

🎓 Lesson Summary

Bootstrap columns (.col-*) create responsive layouts based on a 12-column grid.
.col-auto makes a column adjust its size based on content.
.offset-* adds empty space before a column.
.order-* rearranges columns dynamically.
Nesting columns allows for more complex layouts.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *