Working with Bootstrap Colors and Backgrounds

Tutorial 3 of 5

Introduction

This tutorial is designed to help you understand and effectively use Bootstrap's color and background utilities. By the end of this tutorial, you will be able to choose appropriate colors for your webpages, apply background colors, and create a consistent color scheme throughout your website using Bootstrap.

Prerequisites:
- Basic knowledge of HTML and CSS.
- Familiarity with Bootstrap framework.

Step-by-Step Guide

Bootstrap offers a wide range of color and background classes which can be used to change the text color, background color, link color, etc. It includes color classes for typography, background, borders, and other components.

Bootstrap Color Classes

Bootstrap includes classes for setting text and link colors. The classes .text-{color} and .text-{context} can be used to set the color of text, where {color} can be any of Bootstrap’s color names (like danger, success, dark, etc.) and {context} is for contextual classes (like primary, secondary, info, etc.)

Bootstrap Background Classes

Bootstrap also includes background color classes. The .bg-{color} and .bg-{context} classes are used to set the background color of an element, where {color} can be any of Bootstrap's color names and {context} is for contextual classes.

Code Examples

Changing Text Color

Here's how you can change the color of text in Bootstrap:

<!-- This will make the text color red -->
<p class="text-danger">This is a danger paragraph.</p>

<!-- This will make the text color green -->
<p class="text-success">This is a success paragraph.</p>

Changing Background Color

Here's how you can change the background color of an element in Bootstrap:

<!-- This will make the background color red -->
<div class="bg-danger">This is a danger div.</div>

<!-- This will make the background color green -->
<div class="bg-success">This is a success div.</div>

Summary

Congratulations, you've learned how to work with color and background utilities in Bootstrap! You can now use these utilities to enhance the look of your webpages and create a consistent color scheme across your website.

Next, you might want to dive deeper into Bootstrap and learn about its other components, like buttons, forms, and navbars.

Practice Exercises

  1. Create a webpage with a blue background and white text.
  2. Create a webpage with different sections, each with a different background color.
  3. Create a webpage with different paragraphs, each with a different text color.

Solutions:
1.

<div class="bg-primary text-white">This is a webpage with a blue background and white text.</div>
<div class="bg-primary">This is the first section with a blue background.</div>
<div class="bg-danger">This is the second section with a red background.</div>
<div class="bg-success">This is the third section with a green background.</div>
<p class="text-primary">This is a paragraph with blue text.</p>
<p class="text-danger">This is a paragraph with red text.</p>
<p class="text-success">This is a paragraph with green text.</p>