Bird
Raised Fist0
Bootsrapmarkup~5 mins

Color utilities (text, background) in Bootsrap

Choose your learning style10 modes available
Introduction

Color utilities help you quickly change text and background colors without writing custom CSS. They make your website look nice and clear.

You want to highlight important text in red to show an error.
You need to change the background color of a button to match your brand.
You want to make some text stand out with a different color on a card.
You want to quickly set a light background behind a paragraph for contrast.
You want to use consistent colors across your site using Bootstrap's predefined classes.
Syntax
Bootsrap
text-{color}
bg-{color}

Replace {color} with Bootstrap color names like primary, success, danger, warning, info, light, dark, or white.

Use text- classes to change text color and bg- classes to change background color.

Examples
This makes the text color blue using Bootstrap's primary color.
Bootsrap
<p class="text-primary">This text is blue.</p>
This sets a green background and white text with some padding.
Bootsrap
<div class="bg-success text-white p-3">Green background with white text.</div>
This colors the text red to show a warning.
Bootsrap
<span class="text-danger">Warning message in red.</span>
This uses a light background and dark text for easy reading.
Bootsrap
<section class="bg-light text-dark p-4">Light background with dark text.</section>
Sample Program

This example shows different Bootstrap color utility classes applied to text and backgrounds. It uses text-primary for blue text, text-success for green text, bg-warning for a yellow background, bg-danger for a red button background, and bg-info for a light blue section background with white text. Padding and margin classes add spacing.

Bootsrap
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Bootstrap Color Utilities Example</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
  <main class="container my-5">
    <h1 class="text-primary">Hello, Bootstrap Colors!</h1>
    <p class="text-success">This text is green to show success.</p>
    <div class="bg-warning text-dark p-3 mb-3">Warning background with dark text.</div>
    <button class="btn bg-danger text-white">Danger Button</button>
    <section class="bg-info text-white p-4 mt-4 rounded">
      <p>This section has an info background and white text.</p>
    </section>
  </main>
</body>
</html>
OutputSuccess
Important Notes

Bootstrap color utilities are responsive and accessible by default.

Use padding (p-*) and margin (m-*) classes to add space around colored elements.

For better accessibility, ensure sufficient contrast between text and background colors.

Summary

Use text-{color} to change text color quickly.

Use bg-{color} to change background color easily.

Bootstrap provides many color options to keep your design consistent and simple.

Practice

(1/5)
1. Which Bootstrap class would you use to make text color red?
easy
A. bg-red
B. text-danger
C. text-red
D. bg-danger

Solution

  1. Step 1: Understand Bootstrap text color classes

    Bootstrap uses text-{color} classes to change text color, where {color} is a predefined color name.
  2. Step 2: Identify the correct class for red text

    The class text-danger applies a red color to text. The other options either apply background color or use incorrect class names.
  3. Final Answer:

    text-danger -> Option B
  4. Quick Check:

    Text color red = text-danger [OK]
Hint: Text color uses text-{color}, background uses bg-{color} [OK]
Common Mistakes:
  • Confusing text and background classes
  • Using non-existent classes like text-red
  • Using bg-danger for text color
2. Which of the following is the correct syntax to set a blue background using Bootstrap?
easy
A. bg-blue
B. text-primary
C. background-blue
D. bg-primary

Solution

  1. Step 1: Recall Bootstrap background color classes

    Bootstrap uses bg-{color} classes to set background colors, where {color} is a predefined color name.
  2. Step 2: Identify the correct class for blue background

    The class bg-primary sets a blue background. The other options either set text color or use invalid class names.
  3. Final Answer:

    bg-primary -> Option D
  4. Quick Check:

    Background blue = bg-primary [OK]
Hint: Background color classes start with bg- [OK]
Common Mistakes:
  • Using text-primary for background
  • Using non-Bootstrap class names like bg-blue
  • Confusing text and background prefixes
3. What color will the text have with this HTML snippet?
<p class="text-success">Success message</p>
medium
A. Green
B. Blue
C. Red
D. Yellow

Solution

  1. Step 1: Identify the Bootstrap text color class

    The class text-success is used on the paragraph tag.
  2. Step 2: Recall the color meaning of text-success

    In Bootstrap, text-success applies a green color to the text.
  3. Final Answer:

    Green -> Option A
  4. Quick Check:

    text-success = Green [OK]
Hint: Success means green in Bootstrap colors [OK]
Common Mistakes:
  • Confusing success with danger (red)
  • Mixing text and background colors
  • Guessing blue or yellow incorrectly
4. You want to set a yellow background but wrote bg-warningg. What will happen?
medium
A. The text color will change to yellow
B. The background will be yellow as expected
C. The background will be transparent (no color)
D. The page will show an error

Solution

  1. Step 1: Check the class name spelling

    The correct class for yellow background is bg-warning, but bg-warningg has an extra 'g' and is invalid.
  2. Step 2: Understand how browsers handle invalid classes

    Invalid classes are ignored by the browser, so no background color will be applied, resulting in transparent background.
  3. Final Answer:

    The background will be transparent (no color) -> Option C
  4. Quick Check:

    Invalid class = no style applied [OK]
Hint: Check spelling carefully; invalid classes do nothing [OK]
Common Mistakes:
  • Assuming a typo still applies the color
  • Expecting an error message in browser
  • Confusing background and text color effects
5. You want to create a button with white text on a red background using Bootstrap classes. Which combination is correct?
hard
A. class="btn bg-danger text-white"
B. class="btn text-danger bg-white"
C. class="btn bg-white text-danger"
D. class="btn bg-danger text-danger"

Solution

  1. Step 1: Identify the class for red background

    The class bg-danger sets a red background color in Bootstrap.
  2. Step 2: Identify the class for white text

    The class text-white sets the text color to white.
  3. Step 3: Combine classes for desired effect

    Using btn bg-danger text-white applies a red background and white text on the button.
  4. Final Answer:

    class="btn bg-danger text-white" -> Option A
  5. Quick Check:

    Red background + white text = bg-danger + text-white [OK]
Hint: Background red = bg-danger, white text = text-white [OK]
Common Mistakes:
  • Mixing text-danger (red text) with bg-danger (red background)
  • Using bg-white for background when red is needed
  • Using same color for text and background causing unreadable text
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard