Bootstrap / Bootstrap Basics

Using Containers and Breakpoints

Learn how to use Bootstrap's containers and breakpoints to create responsive designs. This tutorial will guide you through the process with practical examples.

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Introduces the core concepts of Bootstrap, including its grid system, responsive design, and CSS components.

Introduction

This tutorial aims to guide you through the use of Bootstrap's containers and breakpoints to create responsive designs. By the end of this tutorial, you will learn how to effectively use containers to align your website's content, and breakpoints to adjust its layout based on the screen size.

Prerequisites

  • Familiarity with HTML and CSS
  • Basic understanding of the Bootstrap framework

Step-by-Step Guide

Bootstrap uses a system of containers and breakpoints to create a responsive and mobile-first front-end interface.

Containers

In Bootstrap, a container is a class that is used to set the content's margin and width. There are two types of containers:

  • .container: Provides a responsive fixed width container.
  • .container-fluid: Provides a full-width container that spans the entire width of the viewport.

Breakpoints

Breakpoints in Bootstrap are defined using media queries. They represent the different screen sizes to adapt your layout to different devices (mobile, tablet, desktop, etc.). Bootstrap has five breakpoints:

  • xs (Extra small devices, less than 576px)
  • sm (Small devices, 576px and up)
  • md (Medium devices, 768px and up)
  • lg (Large devices, 992px and up)
  • xl (Extra large devices, 1200px and up)

Code Examples

Example 1: Using a Container

<!-- Uses the 'container' class to center the content and give it a max-width -->
<div class="container">
  <h1>Hello, world!</h1>
</div>

In this example, the .container class centers the "Hello, world!" heading and gives it a maximum width based on the size of the user's screen.

Example 2: Using Breakpoints

/* Applies to extra small devices */
@media (max-width: 575.98px) { ... }

/* Applies to small devices */
@media (min-width: 576px) and (max-width: 767.98px) { ... }

/* Applies to medium devices */
@media (min-width: 768px) and (max-width: 991.98px) { ... }

In this example, we use CSS media queries to apply different styles to different devices based on their screen size.

Summary

In this tutorial, we've learned how to use Bootstrap's containers to center and align your content, and breakpoints to create a responsive design that adapts to different screen sizes.

For further learning, you can explore more about Bootstrap's grid system which works hand-in-hand with containers and breakpoints to create complex and responsive layouts.

Practice Exercises

  1. Exercise 1: Create a webpage with a container and some basic content.
  2. Exercise 2: Modify the webpage from Exercise 1 to display different background colors based on the device's screen size using Bootstrap's breakpoints.
  3. Exercise 3: Create a responsive navigation bar that collapses into a hamburger menu on small devices.

Exercise Solutions and Explanations

  1. Solution to Exercise 1:
<div class="container">
  <h1>Welcome to my website!</h1>
  <p>This is some text.</p>
</div>

In this exercise, we created a webpage with a container that contains a heading and a paragraph.

  1. Solution to Exercise 2:
/* Applies to extra small devices */
@media (max-width: 575.98px) { 
  body {
    background-color: red;
  }
}

/* Applies to small devices */
@media (min-width: 576px) and (max-width: 767.98px) { 
  body {
    background-color: blue;
  }
}

/* Applies to medium devices */
@media (min-width: 768px) and (max-width: 991.98px) { 
  body {
    background-color: green;
  }
}

In this exercise, we used CSS media queries to apply different background colors to the body of the webpage based on the device's screen size.

  1. Solution to Exercise 3: This is a more complex exercise that requires knowledge of Bootstrap's navigation bar and collapse plugin. You can find a solution in the Bootstrap documentation.

For further practice, try creating a responsive grid layout using Bootstrap's grid system, and experimenting with different container types and breakpoints.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

CSV to JSON Converter

Convert CSV files to JSON format and vice versa.

Use tool

Countdown Timer Generator

Create customizable countdown timers for websites.

Use tool

Random Name Generator

Generate realistic names with customizable options.

Use tool

URL Encoder/Decoder

Encode or decode URLs easily for web applications.

Use tool

WHOIS Lookup Tool

Get domain and IP details with WHOIS lookup.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help